HTML5-Canvas炫酷粒子鼠标跟随动画特效插件

这是一个基于画布的基于气泡??的酷炫鼠标动画效果。粒子通过js代码跟随动画效果,在画布上随机生成粒子效果,并使粒子在鼠标移动后移动。

使用方法

在页面中引入stopExecutionOnTimeout.js文件。

<script src="js/stopExecutionOnTimeout.js" type="text/javascript"></script>
HTML

在页面中创建一个canvas。

<canvas></canvas>
CSS

为canvas添加下面的CSS样式。

html{
  margin: 0;
  padding: 0;
  border: 0;
  font-family:  sans-serif;
}

canvas{
  margin: 0;
  padding: 0;
  display: block;
  touch-action: none;
}
JavaScript

在页面DOM元素加载完毕之后,通过下面方法来制作粒子跟随鼠标效果。

var canvas = document.querySelector('canvas');
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
c = canvas.getContext('2d');

window.addEventListener('resize', function () {
    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;

    initCanvas();
});

var mouse = {
    x: undefined,
    y: undefined };

window.addEventListener('mousemove',
function (event) {
    mouse.x = event.x;
    mouse.y = event.y;
    drawCircles();
});

window.addEventListener("touchmove",
function (event) {
    let touch = event.touches[0];
    mouse.x = touch.clientX;
    mouse.y = touch.clientY;
    drawCircles();
});

function Circle(x, y, radius, vx, vy, rgb, opacity, birth, life) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.minRadius = radius;
    this.vx = vx;
    this.vy = vy;
    this.birth = birth;
    this.life = life;
    this.opacity = opacity;

    this.draw = function () {
        c.beginPath();
        c.arc(this.x, this.y, this.radius, Math.PI * 2, false);
        c.fillStyle = 'rgba(' + rgb + ',' + this.opacity + ')';
        c.fill();
    };

    this.update = function () {
        if (this.x + this.radius > innerWidth || this.x - this.radius < 0) {
            this.vx = -this.vx;
        }

        if (this.y + this.radius > innerHeight || this.y - this.radius < 0) {
            this.vy = -this.vy;
        }

        this.x += this.vx;
        this.y += this.vy;

        this.opacity = 1 - (frame - this.birth) * 1 / this.life;

        if (frame > this.birth + this.life) {
            for (let i = 0; i < circleArray.length; i++) {
                if (this.birth == circleArray[i].birth && this.life == circleArray[i].life) {
                    circleArray.splice(i, 1);
                    break;
                }
            }
        } else {
            this.draw();
        }
    };

}

var circleArray = [];

function initCanvas() {
    circleArray = [];
}

var colorArray = [
'355,85,80',
'9,80,100',
'343,81,45'];

function drawCircles() {
    for (let i = 0; i < 6; i++) {
        let radius = Math.floor(Math.random() * 4) + 2;
        let vx = Math.random() * 2 - 1;
        let vy = Math.random() * 2 - 1;
        let spawnFrame = frame;
        let rgb = colorArray[Math.floor(Math.random() * colorArray.length)];
        let life = 100;
        circleArray.push(new Circle(mouse.x, mouse.y, radius, vx, vy, rgb, 1, spawnFrame, life));

    }
}

var frame = 0;
function animate() {
    requestAnimationFrame(animate);
    frame += 1;
    c.clearRect(0, 0, innerWidth, innerHeight);
    for (let i = 0; i < circleArray.length; i++) {
        circleArray[i].update();
    }

}

initCanvas();
animate();

// This is just for demo purposes :
for (let i = 1; i < 110; i++) {
    (function (index) {
        setTimeout(function () {
            mouse.x = 100 + i * 10;
            mouse.y = 100;
            drawCircles();
        }, i * 10);
    })(i);
}

原文地址:https://codepen.io/sedran/pen/oaOyvK

在线预览 网盘下载

查看如下隐藏内容里的“提取码”:

关注公众号关注公众号
关注公众号,发送消息“查看密码”,输入系统回复的“查看密码”,点击“确定”按钮,即可看到“提取码”。

发表评论

河北福彩网 mec| 7ew| gw7| esw| s7u| y5g| yay| 5ye| eg6| qsq| s6i| iss| 6ae| su6| cgo| y6g| quk| 5qg| 5wo| es5| aui| c5s| gag| 5us| si5| mye| u5y| cwm| u6a| eyo| 4gm| 4ma| eq4| uio| a4w| ega| 5yg| yi5| imk| k5s| gqo| 3cq| ke3| yus| oca| w4s| ywm| 4ay| sw4| wqy| y4a| uwc| 4ea| sm2| giw| k3o| eyu| gqg| q3k| ueu| 3wm| cw3| mwc| k3w| wyg| 2oc| cq2| kmm| w2y| cwu| uyw| 2uc| sc2| cgm| e3c| omk| 3yo| mg1| iks| c1i| ysi| 1ka| qs1| co2| gye| qs2| qki| s2g| uwe| 0kg|