大数跨境
0
0

好看的五彩纸条!(可复制源代码)

好看的五彩纸条!(可复制源代码) 趣聊科技圈
2024-09-19
0

效果演示

html 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Confetti Explosion</title>
    <style>
        bodyhtml {margin0;padding0;height100%;overflow: hidden;}
        #confettiCanvas {background-color#1a202c;position: absolute;top0;left0;}
        .instruction {position: absolute;top50%;left50%;transformtranslate(-50%, -50%);color: white;font-family: Arial, sans-serif;font-size24px;font-weight: bold;text-align: center;pointer-events: none;}
    
</style>
</head>
<body>
    <canvas id="confettiCanvas"></canvas>
    <p class="instruction">移动鼠标创造五彩纸条哦!</p>
    <script>
const canvas = document.getElementById("confettiCanvas");const ctx = canvas.getContext("2d");const colors = ["#FF3F8E""#04C2C9""#2E55C1""#F9D423"];let particles = [];let mousePos = { x0y0 };
class Particle {constructor(x, y, size, color, speedX, speedY) {this.x = x;this.y = y;this.size = size;this.color = color;this.speedX = speedX;this.speedY = speedY;}update() {this.x += this.speedX;this.y += this.speedY;this.size *= 0.98; }draw() {ctx.fillStyle = this.color;ctx.beginPath();ctx.arc(this.x, this.y, this.size, 0Math.PI * 2);ctx.closePath();ctx.fill();}}
function resizeCanvas({canvas.width = window.innerWidth;canvas.height = window.innerHeight;}
function createExplosion(x, y{const particleCount = 50;for (let i = 0; i < particleCount; i++) {const size = Math.random() * 5 + 2;const color = colors[Math.floor(Math.random() * colors.length)];const speedX = (Math.random() * 2 - 1) * 2;const speedY = (Math.random() * 2 - 1) * 2;particles.push(new Particle(x, y, size, color, speedX, speedY));}}
function animate({ctx.clearRect(00, canvas.width, canvas.height);
particles = particles.filter((particle) => {particle.update();particle.draw();return particle.size > 0.5;});requestAnimationFrame(animate);}
function handleMouseMove(event{mousePos.x = event.clientX;mousePos.y = event.clientY;}resizeCanvas();animate();window.addEventListener("resize", resizeCanvas);canvas.addEventListener("mousemove", handleMouseMove);setInterval(() => {createExplosion(mousePos.x, mousePos.y);}, 50);
    
</script>
</body>
</html>


【声明】内容源于网络
0
0
趣聊科技圈
🧐探索科技,发现乐趣。🤩带你玩遍科技好物!
内容 511
粉丝 0
趣聊科技圈 🧐探索科技,发现乐趣。🤩带你玩遍科技好物!
总阅读748
粉丝0
内容511