canvas鼠标跟随效果(原生js实现)

效果展示:

源码展示:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>canvas鼠标跟随效果(原生js实现)</title>
    <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
    <style>
        * {
            margin:0;
            padding:0;
        }
        body {
            overflow:hidden;
        }
        #myCanvas {
            background-color:#000;
        }
    </style>
</head>
<body>
<canvas id="myCanvas"></canvas>

<script>
    var myCanvas = document.getElementById('myCanvas');
    var ctx = myCanvas.getContext("2d");
    var starlist = [];

    function init() {
        // 设置canvas区域的范围为整个页面
        myCanvas.width = window.innerWidth;
        myCanvas.height = window.innerHeight;
    };
    init();
    // 监听屏幕大小改变 重新为canvas大小赋值
    window.onresize = init;

    // 当鼠标移动时 将鼠标坐标传入构造函数 同时创建一个对象
    myCanvas.addEventListener('mousemove', function(e) {
        // 将对象push到数组中,画出来的彩色小点可以看作每一个对象中记录着信息 然后存在数组中
        starlist.push(new Star(e.offsetX, e.offsetY));
    });

    // 随机数函数
    function random(min, max) {
        // 设置生成随机数公式
        return Math.floor((max - min) * Math.random() + min);
    };


    // 构造函数
    function Star(x, y) {
        // 将坐标存在每一个点的对象中
        this.x = x;
        this.y = y;
        // 设置随机偏移量
        this.vx = (Math.random() - 0.5) * 3;
        this.vy = (Math.random() - 0.5) * 3;
        this.color = 'rgb(' + random(0, 256) + ',' + random(0, 256) + ',' + random(0, 256) + ')';
        // 初始透明度
        this.a = 1;
        // 开始画
        this.draw();
    }

    // 再star对象原型上封装方法
    Star.prototype = {
        // canvas根据数组中存在的每一个对象的小点信息开始画
        draw: function() {
            ctx.beginPath();
            ctx.fillStyle = this.color;
            // 图像覆盖  显示方式 lighter 会将覆盖部分的颜色重叠显示出来
            ctx.globalCompositeOperation = 'lighter'
            ctx.globalAlpha = this.a;
            ctx.arc(this.x, this.y, 30, 0, Math.PI * 2, false);
            ctx.fill();
            this.updata();
        },
        updata: function() {
            // 根据偏移量更新每一个小点的位置
            this.x += this.vx;
            this.y += this.vy;
            // 透明度越来越小
            this.a *= 0.98;
        }
    }
    // 渲染
    function render() {
        // 每一次根据改变后数组中的元素进行画圆圈  把原来的内容区域清除掉
        ctx.clearRect(0, 0, myCanvas.width, myCanvas.height)

        // 根据存在数组中的每一位对象中的信息画圆
        starlist.forEach(function(ele, i) {
            ele.draw();
            // 如果数组中存在透明度小的对象 ,给他去掉 效果展示逐渐消失
            if (ele.a < 0.05) {
                starlist.splice(i, 1);
            }
        });
        requestAnimationFrame(render);
    }
    render();
</script>
<pre style="color:red">
 感:  最近贡献一下我在教学中的小案例可以能给你一些帮助 ,希望继续关注我的博客

                                                                               --王
</pre>



</body>
</html>

 

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是一个简单的js代码实现跟随鼠标粒子效果: HTML: ```html <canvas id="canvas"></canvas> ``` CSS: ```css #canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } ``` JS: ```javascript var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var particles = []; var mouse = { x: undefined, y: undefined } window.addEventListener("mousemove", function(event) { mouse.x = event.x; mouse.y = event.y; }); function Particle(x, y, radius, color) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.radians = Math.random() * Math.PI * 2; this.velocity = 0.05; this.distanceFromCenter = Math.random() * 50 + 100; this.lastMouse = {x: x, y: y}; this.draw = function(lastPoint) { ctx.beginPath(); ctx.strokeStyle = this.color; ctx.lineWidth = this.radius; ctx.moveTo(lastPoint.x, lastPoint.y); ctx.lineTo(this.x, this.y); ctx.stroke(); ctx.closePath(); } this.update = function() { // 记录上一帧的鼠标位置 this.lastMouse.x += (mouse.x - this.lastMouse.x) * 0.05; this.lastMouse.y += (mouse.y - this.lastMouse.y) * 0.05; // 运动轨迹 this.radians += this.velocity; this.x = this.lastMouse.x + Math.cos(this.radians) * this.distanceFromCenter; this.y = this.lastMouse.y + Math.sin(this.radians) * this.distanceFromCenter; this.draw(this.lastMouse); } } function init() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; for (var i = 0; i < 50; i++) { var radius = Math.random() * 3 + 1; var color = "rgba(255, 255, 255, 0.5)"; particles.push(new Particle(canvas.width / 2, canvas.height / 2, radius, color)); } } function animate() { requestAnimationFrame(animate); ctx.fillStyle = "rgba(0, 0, 0, 0.1)"; ctx.fillRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < particles.length; i++) { particles[i].update(); } } init(); animate(); ``` 这段代码创建了一个画布,并且在鼠标移动时跟随鼠标移动的粒子效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值