canvas鼠标跟随彩色粒子线条交互动画效果代码

代码语言:html

所属分类:粒子

代码描述:canvas鼠标跟随彩色粒子线条交互动画效果代码

代码标签: canvas 彩色 粒子 线条

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        #canvas1 {
        position: absolute;
        background-color: black;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
    }
    </style>
</head>

<body>
    <canvas id="canvas1"></canvas>

    <script>
        const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particles = [];

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

let hueCol = 0;

const mouse = {
    x: undefined,
    y: undefined,
}

canvas.addEventListener('click', function (e) {
    mouse.x = e.x;
    mouse.y = e.y;
    for (let i = 0; i < 5; i++) {
        particles.push(new Particle);
    }
})

canvas.addEventListener('mousemove', function (e) {
    mouse.x = e.x;
    mouse.y = e.y;
    for (let i = 0; i < 5; i++) {
        particles.push(new Particle);
    }
})

class Particle {
    constructor() {
        this.x = mouse.x;
        this.y = mouse.y;
        this.speedX = Math.random() * 3 - 1.5;
        this.speedY = Math.random() * 3 - 1.5;
        this.col.........完整代码请登录后点击上方下载按钮下载查看

网友评论0