JS鼠标跟随特效

直接上特效页面代码,后面是效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas</title>
    <style>
        canvas{
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <canvas id="canvas"></canvas>
    <script>
        var canvas = document.getElementById('canvas');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        var ctx = canvas.getContext("2d");

        var circleArr = [];

        //圆函数
        function Circle(x, y, r, color){
            this.x = x;
            this.y = y;
            this.r = r;
            this.color = "rgb(" + (parseInt(Math.random() * 240) + 9) + "," + (parseInt(Math.random() * 220) +18) + ", 203)";

            this.dx = Math.random() * 12 -7;
            this.dy = Math.random() * 12 -7;

            circleArr.push(this);
        }

        Circle.prototype.render = function(){
            //画一个简单得颜色球状
            ctx.beginPath();
            ctx.arc(this.x, this.y, this.r, 0, Math.PI*2, true);
            ctx.fillStyle = this.color;
            ctx.fill();
        };

        Circle.prototype.update = function(){
            this.x += this.dx;
            this.y += this.dy;
            this.r--;

            if (this.r < 0){
                for(var i = 0; i < circleArr.length; i++){
                    if(circleArr[i] === this){
                        circleArr.splice(i,1);
                    };
                }
                return false;
            }
            return true;
        };

        canvas.onmousemove = function (event) {
            new Circle(event.clientX, event.clientY, 30, "green");
        };

        //定时器
        setInterval(function () {
            ctx.clearRect(0, 0, canvas.width, canvas.height)
            for (var i = 0; i < circleArr.length; i++){
                circleArr[i].update() && circleArr[i].render();
                console.log(circleArr[i]);
            }
        }, 20);
    </script>
</body>
</html>

效果

鼠标移动特效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值