cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <canvas id="canvas" width="900" height="500"></canvas>
            
          
!
            
              //鼠标移动到画布小球运动,移开小球静止
var canvas = document.getElementById('canvas')
var ctx = canvas.getContext('2d')
var raf
var ball = {
  x: 100,
  y: 100,
  vx: 5,//速度矢量
  vy: 2,
  running:false,
  radius: 25,
  color: 'blue',
  draw:function(){
    ctx.beginPath()
    ctx.arc(this.x,this.y,this.radius,0,Math.PI*2,true)
    ctx.closePath()
    ctx.fillStyle = this.color
    ctx.fill()
  }
}
function clear(){
  //用clear 清理掉之前帧里旧的圆形。
  //ctx.clearRect(0,0,canvas.width,canvas.height) 
  
  //用一个半透明的fillRect函数取代clearRect函数,产生长尾效果
  ctx.fillStyle = 'rgba(255,255,255,0.3)';
  ctx.fillRect(0,0,canvas.width,canvas.height);
}
function draw(){
  clear()
  ball.draw()
  if (ball.y + ball.vy > canvas.height || ball.y + ball.vy < 0) {
    ball.vy = -ball.vy;
  }
  if (ball.x + ball.vx > canvas.width || ball.x + ball.vx < 0) {
    ball.vx = -ball.vx;
  } 
  ball.x+=ball.vx //球依靠添加速率矢量进行移动
  ball.y+=ball.vy
  raf = window.requestAnimationFrame(draw)
}
canvas.addEventListener("mouseover",function(e){
  raf = window.requestAnimationFrame(draw)
 
})
canvas.addEventListener("mouseout",function(e){
  window.cancelAnimationFrame(raf);
});
ball.draw()
            
          
!
999px
Loading ..................

Console