Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
        <canvas id="canvas" width="300" height="300"></canvas>
</div>
            
          
!
            
              .container {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        width: 500px;
        height: 300px;
        background-color: blue;
        text-align: center;
        border-radius: 10px;
        box-shadow: 0 13px 25px 0 rgba(0, 0, 0, 0.75), 0 11px 7px 0 rgba(0, 0, 0, 0.5);
    }
            
          
!
            
               var can = document.getElementById('canvas');
 var ctx = can.getContext("2d");
 var mWidth = canvas.width;
 var mHeight = canvas.height;
 var mMaxOffset = 100;
 var mMaxRadius = 30;
 var mColor = '#fff';
 var mOffset = 0;
 var mDegrees = 0;
 setInterval(function() {
     drawCircle();
     rotate();
     var present = mDegrees / 360;
     if (present < 0.5) {
         mOffset = mMaxOffset * present; //改变圆心位置,使其靠近中心点
     } else {
         mOffset = mMaxOffset * (1 - present);
     }
     if (present <= 0.375 || present >= 0.625) drawPath(present); //当两圆接触时绘制粘合动画
 }, 30)

 function rotate() {
     canvas.style.transform = `rotate(${mDegrees}deg)`; //使canvas旋转
     mDegrees += 2;
     if (mDegrees == 360) {
         mDegrees = 0;
     }
 }

 function drawCircle() {
     ctx.clearRect(0, 0, mWidth, mHeight);
     ctx.beginPath();
     ctx.arc(mWidth / 2, mHeight / 2 - mOffset, mMaxRadius, 0, Math.PI * 2); //在中心点上方mOffset处画圆
     ctx.closePath();
     ctx.fillStyle = mColor;
     ctx.fill();
     ctx.beginPath();
     ctx.arc(mWidth / 2, mHeight / 2 + mOffset, mMaxRadius, 0, Math.PI * 2); //在中心点下方mOffset处画圆
     ctx.closePath();
     ctx.fill();
 }
function drawPath(present) {
     ctx.beginPath();
     ctx.moveTo(mWidth / 2 - mMaxRadius, mHeight / 2 - mOffset); //从上圆最左点开始画线
     ctx.lineTo(mWidth / 2 + mMaxRadius, mHeight / 2 - mOffset); //到上圆最右点

     var suppoetOffset = -mMaxRadius;
     if (present < 0.25 || present > 0.75) { //当两球相交
         suppoetOffset = mMaxRadius;
     } else if (present <= 0.375) { //当两球刚分离时
         suppoetOffset = -(480 * present - 150);
     } else if (present >= 0.625) { //当两球快要接触时
         suppoetOffset = 480 * present - 330;
     }
     // 贝塞尔选点以中心点便宜30以内距离为控制点,另一圆边点为结束点
     ctx.quadraticCurveTo(mWidth / 2 + suppoetOffset, mHeight / 2, mWidth / 2 + mMaxRadius, mHeight / 2 + mOffset);
     ctx.lineTo(mWidth / 2 - mMaxRadius, mHeight / 2 + mOffset);
     ctx.quadraticCurveTo(mWidth / 2 - suppoetOffset, mHeight / 2, mWidth / 2 - mMaxRadius, mHeight / 2 - mOffset);
     ctx.closePath();
     ctx.fill();
 }
 

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console