canvas(九)动画、补间动画

本文探讨了使用HTML5 canvas实现动画的基础步骤,包括使用setTimeout和setInterval,以及requestAnimationFrame来驱动动画,重点介绍了弹性小球的案例,并对比了不同方法的优缺点。此外,文章还涉及补间动画的应用,如Tween.js实现悠悠球效果和颜色过渡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动画

动画实现步骤

  • 清理画布: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;
    //记录时间 time
    let time=new Date();
    //重力 gravity
    const gravity=0.01;
    //弹力
    const bounding=-0.8;
    //速度/毫秒 vy
    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);
        //递归调用render 函数
        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;
    //实例化Tween 对象,为其添加目标对象
    const tween=new TWEEN.Tween(ball);
    //设置目标对象一段时间后的状态to({ key:val }, 时间长度)
    tween.to({y:700},2000);
    //开始计时 start()
    tween.start();
    //重复 repeat(Infinity)
    tween.repeat(Infinity)
    //悠悠球 yoyo(true)
    tween.yoyo(true);
    //设置插值 easing()
    //Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back,Bounce
    tween.easing(TWEEN.Easing.Bounce.Out)
    //事件监听
    //onStart, onStop, onUpdate, onComplete
    tween.onUpdate(function(){
        console.log(this.y);
    })
    //渲染方法
    !(function render(){
        //TWEEN 更新目标目标对象的状态
        TWEEN.update();
        //擦除
        ctx.clearRect(0,0,width,height);
        //绘图
        ball.draw(ctx);
        //递归调用render 函数
        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/Tween.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;
    //实例化Tween 对象,为其添加目标对象
    let tween = new TWEEN.Tween(ball);
    //设置目标对象一段时间后的状态to({ key:val }, 时间长度)
    tween.to({color:'orange'}, 2000);
    //开始计时 start()
    tween.start();
    //重复 repeat(Infinity)
    tween.repeat(Infinity);
    //悠悠球 yoyo(true)
    tween.yoyo(true);
    //设置插值
    //Linear, Quadratic, Cubic, Quartic, Quintic, Sinusoidal, Exponential, Circular, Elastic, Back, Bounce
    tween.easing(TWEEN.Easing.Bounce.Out);
    //事件监听
    //onUpdate,onStart,onStop,onComplete
    tween.onUpdate(function(){
        //console.log(this.x)
    });
    //渲染方法
    !(function render(){
        //更新目标目标对象的状态
        TWEEN.update();
        //擦除
        ctx.clearRect(0,0,width,height);
        //绘图
        ball.draw(ctx);
        //递归调用render 函数
        window.requestAnimationFrame(render);
    })()
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值