动画
动画实现步骤
- 清理画布:
ctx.clearRect(0,0,canvas.width,canvas.height)
- 保存 canvas 上下文对象的状态:
ctx.save()
- 绘制动画图形:…
- 恢复 canvas 上下文对象的状态:
ctx.restore()

驱动动画的方法
- setTimeOut(fn,time) 和setInterval(fn,time)
- 优点:使用方便,动画的时间间隔可以自定义。
- 缺点:隐藏浏览器标签后,会依旧运行,造成资源浪费。与浏览器刷新频率不同步。
- requestAnimationFrame(fn)
- 优点:性能更优良。隐藏浏览器标签后,便不会运行。与浏览器刷新频率同步。
- 缺点:动画的时间间隔无法自定义
动画实现弹性小球
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>弹性运动</title>
<style>
body{margin: 0;overflow: hidden}
#canvas{background: antiquewhite;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas=document.getElementById('canvas');
const [width,height]=[window.innerWidth,window.innerHeight];
canvas.width=width;
canvas.height=height;
const ctx=canvas.getContext('2d');
class Ball{
constructor(r,color='#000'){
this.color=color;
this.r=r;
this.x=0;
this.y=0;
}
draw(ctx){
ctx.save();
ctx.beginPath();
ctx.fillStyle=this.color;
ctx.arc(this.x,this.y,this.r,0,Math.PI*2);
ctx.fill();
ctx.restore();
}
}
let ball=new Ball(15);
ball.y=50;
ball.x=width/2;
let time=new Date();
const gravity=0.01;
const bounding=-0.8;
let vy=0.3;
let vx=0.3;
function animate(){
let now=new Date();
let diff=now-time;
time=now;
vy+=gravity;
ball.y+=vy*diff;
ball.x+=vx*diff;
if(ball.y+ball.r>height){
ball.y=height-ball.r;
vy*=bounding;
}
if(ball.x-ball.r<0){
ball.x=ball.r;
vx*=bounding;
}
if(ball.x+ball.r>width){
ball.x=width-ball.r;
vx*=bounding;
}
}
!(function render(){
animate();
ctx.clearRect(0,0,width,height);
ball.draw(ctx);
window.requestAnimationFrame(render);
})()
</script>
</body>
</html>
补间动画
- 补间动画是在两个关键帧之间,以某种算法自动计算物体运动的插值,从而形成一种过度效果。

用tween.js 做补间动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>补间动画</title>
<style>
body{margin: 0;overflow: hidden}
#canvas{background: antiquewhite;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="./js/Tween.js"></script>
<script>
const [width,height]=[window.innerWidth,window.innerHeight];
const canvas=document.getElementById('canvas');
canvas.width=width;
canvas.height=height;
const ctx=canvas.getContext('2d');
class Ball{
constructor(r,color='#000'){
this.color=color;
this.r=r;
this.x=0;
this.y=0;
}
draw(ctx){
ctx.save();
ctx.beginPath();
ctx.fillStyle=this.color;
ctx.arc(this.x,this.y,this.r,0,Math.PI*2);
ctx.fill();
ctx.restore();
}
}
const ball=new Ball(15);
ball.x=width/2;
ball.y=50;
const tween=new TWEEN.Tween(ball);
tween.to({y:700},2000);
tween.start();
tween.repeat(Infinity)
tween.yoyo(true);
tween.easing(TWEEN.Easing.Bounce.Out)
tween.onUpdate(function(){
console.log(this.y);
})
!(function render(){
TWEEN.update();
ctx.clearRect(0,0,width,height);
ball.draw(ctx);
window.requestAnimationFrame(render);
})()
</script>
</body>
</html>
颜色也可以做补件动画
- tween.js 并没有提供颜色的过度方法,所以我把d3 的color 插件拼装到了tween.js 中去,形成了一个SupTween.js,使用之前需要引入d3-color.js
- 补间动画实现颜色变化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>补间动画</title>
<style>
body{margin: 0;overflow: hidden}
#canvas{background: antiquewhite;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="./js/d3-color.js"></script>
<script src="./js/SupTween.js"></script>
<script>
const [width,height]=[window.innerWidth,window.innerHeight];
const canvas=document.getElementById('canvas');
canvas.width=width;
canvas.height=height;
const ctx=canvas.getContext('2d');
class Ball{
constructor(r,color='#000'){
this.color=color;
this.r=r;
this.x=0;
this.y=0;
}
draw(ctx){
ctx.save();
ctx.beginPath();
ctx.fillStyle=this.color;
ctx.arc(this.x,this.y,this.r,0,Math.PI*2);
ctx.fill();
ctx.restore();
}
}
const ball=new Ball(100,'green');
ball.x=width/2;
ball.y=200;
let tween = new TWEEN.Tween(ball);
tween.to({color:'orange'}, 2000);
tween.start();
tween.repeat(Infinity);
tween.yoyo(true);
tween.easing(TWEEN.Easing.Bounce.Out);
tween.onUpdate(function(){
});
!(function render(){
TWEEN.update();
ctx.clearRect(0,0,width,height);
ball.draw(ctx);
window.requestAnimationFrame(render);
})()
</script>
</body>
</html>