HTML5庆祝生日蛋糕烟花特效

HTML5庆祝生日蛋糕烟花特效

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML5 Birthday Cake Fireworks</title>
    <style>
        canvas {
            position: absolute;
            top: 0;
            left: 0;
            z-index: -1;
        }
    </style>
</head>
<body>
    <canvas id="fireworks"></canvas>
    <script>
        // 烟花特效
        (function () {
            var canvas = document.getElementById('fireworks'),
                ctx = canvas.getContext('2d'),
                fireworks = [],
                particles = [],
                hue = 120,
                limiterTotal = 5,
                limiterTick = 0,
                timerTotal = 80,
                timerTick = 0,
                mousedown = false,
                mx,
                my;
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            function random(min, max) {
                return Math.random() * (max - min) + min;
            }
            function calculateDistance(p1x, p1y, p2x, p2y) {
                var xDistance = p1x - p2x,
                    yDistance = p1y - p2y;
                return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
            }
            function Firework(sx, sy, tx, ty) {
                this.x = sx;
                this.y = sy;
                this.sx = sx;
                this.sy = sy;
                this.tx = tx;
                this.ty = ty;
                this.distanceToTarget = calculateDistance(sx, sy, tx, ty);
                this.distanceTraveled = 0;
                this.coordinates = [];
                this.coordinateCount = 3;
                while (this.coordinateCount--) {
                    this.coordinates.push([this.x, this.y]);
                }
                this.angle = Math.atan2(ty - sy, tx - sx);
                this.speed = 2;
                this.acceleration = 1.05;
                this.brightness = random(50, 70);
                this.targetRadius = 1;
            }
            Firework.prototype.update = function (index) {
                this.coordinates.pop();
                this.coordinates.unshift([this.x, this.y]);
                if (this.targetRadius < 8) {
                    this.targetRadius += 0.3;
                } else {
                    this.targetRadius = 1;
                }
                this.speed *= this.acceleration;
                var vx = Math.cos(this.angle) * this.speed,
                    vy = Math.sin(this.angle) * this.speed;
                this.distanceTraveled = calculateDistance(this.sx, this.sy, this.x + vx, this.y + vy);
                if (this.distanceTraveled >= this.distanceToTarget) {
                    createParticles(this.tx, this.ty);
                    fireworks.splice(index, 1);
                } else {
                    this.x += vx;
                    this.y += vy;
                }
            }
            Firework.prototype.draw = function () {
                ctx.beginPath();
                ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
                ctx.lineTo(this.x, this.y);
                ctx.strokeStyle = 'hsl(' + hue + ', 100%, ' + this.brightness + '%)';
                ctx.stroke();
                ctx.beginPath();
                ctx.arc(this.tx, this.ty, this.targetRadius, 0, Math.PI * 2);
                ctx.stroke();
            }
            function createParticles(x, y) {
                var particleCount = 30;
                while (particleCount--) {
                    particles.push(new Particle(x, y));
                }
            }
            function Particle(x, y) {
                this.x = x;
                this.y = y;
                this.coordinates = [];
                this.coordinateCount = 5;
                while (this.coordinateCount--) {
                    this.coordinates.push([this.x, this.y]);
                }
                this.angle = random(0, Math.PI * 2);
                this.speed = random(1, 10);
                this.friction = 0.95;
                this.gravity = 1;
                this.hue = random(hue - 20, hue + 20);
                this.brightness = random(50, 80);
                this.alpha = 1;
                this.decay = random(0.015, 0.03);
            }
            Particle.prototype.update = function (index) {
                this.coordinates.pop();
                this.coordinates.unshift([this.x, this.y]);
                this.speed *= this.friction;
                this.x += Math.cos(this.angle) * this.speed;
                this.y += Math.sin(this.angle) * this.speed + this.gravity;
                this.alpha -= this.decay;
                if (this.alpha <= this.decay) {
                    particles.splice(index, 1);
                }
            }
            Particle.prototype.draw = function () {
                ctx.beginPath();
                ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
                ctx.lineTo(this.x, this.y);
                ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + this.alpha + ')';
                ctx.stroke();
            }
            function loop() {
                ctx.globalCompositeOperation = 'destination-out';
                ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                ctx.globalCompositeOperation = 'lighter';
                var i = fireworks.length;
                while (i--) {
                    fireworks[i].draw();
                    fireworks[i].update(i);
                }
                var i = particles.length;
                while (i--) {
                    particles[i].draw();
                    particles[i].update(i);
                }
                if (timerTick >= timerTotal) {
                    if (!mousedown) {
                        fireworks.push(new Firework(canvas.width / 2, canvas.height, random(0, canvas.width), random(0, canvas.height / 2)));
                        timerTick = 0;
                    }
                } else {
                    timerTick++;
                }
                if (limiterTick >= limiterTotal) {
                    if (mousedown) {
                        fireworks.push(new Firework(canvas.width / 2, canvas.height, mx, my));
                        limiterTick = 0;
                    }
                } else {
                    limiterTick++;
                }
                requestAnimationFrame(loop);
            }
            window.onload = function () {
                canvas.addEventListener('mousemove', function (e) {
                    mx = e.pageX - canvas.offsetLeft;
                    my = e.pageY - canvas.offsetTop;
                });
                canvas.addEventListener('mousedown', function (e) {
                    e.preventDefault();
                    mousedown = true;
                });
                canvas.addEventListener('mouseup', function (e) {
                    e.preventDefault();
                    mousedown = false;
                });
                loop();
            };
        })();
    </script>
</body>
</html>

以上代码使用HTML5的元素和JavaScript实现了一个简单的生日蛋糕烟花特效。在页面加载完成后,会自动启动动画,当鼠标点击页面时,会在鼠标位置发射一个烟花。

效果展示

HTML5庆祝生日蛋糕烟花特效

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要用HTML5制作烟花生日蛋糕代码,可以通过canvas元素和JavaScript实现。下面是一个简单的实例代码: HTML部分: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>烟花生日蛋糕</title> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="fireworks" width="800" height="600"></canvas> <script src="fireworks.js"></script> </body> </html> ``` JavaScript部分(fireworks.js): ```javascript var fireworks = document.getElementById('fireworks'); var ctx = fireworks.getContext('2d'); // 绘制烟花 function drawFirework(x, y) { var colors = ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff', '#00ffff']; var numParticles = 100; var particles = []; for(var i=0; i<numParticles; i++) { var particle = { x: x, y: y, radius: Math.random() * 6 + 2, color: colors[Math.floor(Math.random() * colors.length)], velocityX: Math.random() * 4 - 2, velocityY: Math.random() * 4 - 2.5, gravity: 0.1, fade: 0.01 }; particles.push(particle); } function update() { ctx.clearRect(0, 0, fireworks.width, fireworks.height); for(var i=0; i<numParticles; i++) { var particle = particles[i]; particle.velocityY += particle.gravity; particle.x += particle.velocityX; particle.y += particle.velocityY; particle.radius -= particle.fade; ctx.beginPath(); ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2); ctx.fillStyle = particle.color; ctx.fill(); ctx.closePath(); if(particle.radius <= 0) { particles.splice(i, 1); i--; numParticles--; } } if(numParticles === 0) { clearInterval(animation); } } var animation = setInterval(update, 1000/60); } // 在点击位置绘制烟花 fireworks.addEventListener('click', function(event) { var rect = fireworks.getBoundingClientRect(); var x = event.clientX - rect.left; var y = event.clientY - rect.top; drawFirework(x, y); }); ``` 通过运行上述代码,你可以在网页上点击鼠标,然后会在点击位置绘制出烟花效果,那就好像在生日蛋糕上点燃了一颗引线一样。每次点击都会绘制一个新的烟花效果。希望这个简单的示例对你有所帮助! ### 回答2: HTML5是一种适用于网页设计和开发的标准技术,包含了HTML、CSS和JavaScript等语言。要用HTML5制作烟花生日蛋糕代码,可以结合CSS和JavaScript实现一些特效和动效果。 首先,在HTML中创建一个div容器,用于表示蛋糕的形状和位置。可以使用CSS设置蛋糕的外观,例如颜色、大小和形状。 接下来,在HTML中嵌入一段JavaScript代码,用于添加烟花特效。可以使用JavaScript中的canvas元素和相关函数来创建烟花。 具体步骤如下: 1. 在HTML文件中创建一个div容器,并设置其样式为蛋糕的外观和位置。 ```html <div id="cake" style="background-color: lightpink; width: 200px; height: 300px;"></div> ``` 2. 在JavaScript代码中,获取到这个div容器,并创建一个canvas元素,用于绘制烟花。 ```javascript var cake = document.getElementById("cake"); var canvas = document.createElement("canvas"); canvas.width = cake.clientWidth; canvas.height = cake.clientHeight; cake.appendChild(canvas); var ctx = canvas.getContext("2d"); ``` 3. 编写用于绘制烟花的函数。可以使用JavaScriptcanvas API绘制出烟花的形状和颜色。 ```javascript function drawFirework(x, y, color) { // 绘制烟花的具体形状和颜色 //例如:ctx.beginPath(); //ctx.arc(x, y, 5, 0, 2 * Math.PI); //ctx.fillStyle = color; //ctx.fill(); } ``` 4. 在JavaScript代码中,添加触发绘制烟花的事件。可以使用JavaScript的定时器或者点击事件等来触发绘制烟花的函数,并设置随机的位置和颜色。 ```javascript setInterval(function() { var x = Math.random() * canvas.width; var y = Math.random() * canvas.height; var color = "rgb(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ")"; drawFirework(x, y, color); }, 1000); ``` 以上是一个简单的示例,在实际制作过程中,可以根据需要进行更加复杂的调整和美化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值