canvas:圆周运动

demo

 

 

原理

HTML结构

<body>
    <canvas id="canvas" width="1300" height="750"></canvas>
</body>

JS定义canvas绘制行为

    <script>
        (()=>{
            document.addEventListener('DOMContentLoaded',function(){
                //业务
                let canvas = document.querySelector('#canvas');
                let ctx = canvas.getContext('2d');
                let circles = [];
                setInterval(function () {
                    genareteCircle();
                },100);

                setInterval(function () {
                    circleRun2();
                },16);

                //生成圆
                function genareteCircle() {
                    let circle = {};
                    circle.x = 400+Math.floor(Math.random()*(canvas.width-800));
                    circle.y = Math.floor(Math.random()*(canvas.height -300));
                    
                    
                    circle.r = Math.ceil(Math.random()* 20);
                    
                    circle.deltaAngle = 0;
                    circle.deltay = 0;
                    
                    circle.color = {};
                    circle.color.r = Math.floor(Math.random()*256);
                    circle.color.g = Math.floor(Math.random()*256);
                    circle.color.b = Math.floor(Math.random()*256);
                    circle.color.a = 1;
                    circles.push(circle);
                }

                /**
                 * 使气泡围绕圆形运动
                 */
                function circleRun2() {
                    ctx.clearRect(0,0,canvas.width,canvas.height);
                    

                    //中心
                    let runr = {
                        x:canvas.width/2,
                        y:canvas.height/2
                    };


                    if (circles.length > 155){
                        circles.shift();
                    }
                    // console.log(circles.length);

                    circles.forEach(function (circle,index) {

                        //根据中心点和生成圆的圆心位置计算半径
                        cr = Math.pow(Math.pow((runr.x - circle.x),2)+Math.pow((runr.y - circle.y),2),0.5);

                        if (circle.color.a < 0){
                            circles.splice(index,1);
                        }
                        console.log(cr);

                        // 圆心
                        circle.deltaAngle++;
                        let x = cr * Math.cos(circle.deltaAngle/180*Math.PI);
                        let y = cr * Math.sin(circle.deltaAngle/180*Math.PI);


                        ctx.beginPath();
                        ctx.arc((runr.x+x),(runr.y+y),circle.r,0,Math.PI*2);
                        ctx.fillStyle = 'rgba('+circle.color.r+','+circle.color.g+','+circle.color.b+','+(circle.color.a)+')';
                        ctx.fill();

                    });

                }
</script>

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值