HTML5庆祝生日蛋糕烟花特效

这篇具有很好参考价值的文章主要介绍了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庆祝生日蛋糕烟花特效文章来源地址https://www.toymoban.com/news/detail-408249.html

到了这里,关于HTML5庆祝生日蛋糕烟花特效的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • HTML5七夕情人节表白网页制作【抖音3D立方体图像库】HTML+CSS+JavaScript html生日快乐祝福网页制作

    这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看。 此波共有100个表白网站,可以任意修改和使用,很多人会希望向心爱的男孩女孩告白,生性腼腆的人即使那个TA站在眼前都不敢向前表白。说不出口的话就用短视频告诉

    2024年02月02日
    浏览(36)
  • 情人节定制:HTML5 Canvas全屏七夕爱心表白特效

    “这个世界乱糟糟的而你干干净净可以悬在我心上做太阳和月亮。”,七夕节表白日,你要错过吗?如果你言辞不善,羞于开口的话,可以使用 html5 canvas 制作浪漫的七夕爱心表白动画特效,全屏的爱心和表白语,了解一下!  🌹 最后,祝天下有情人终成眷属 🌹

    2024年02月11日
    浏览(30)
  • HTML5+CSS3小实例:3D翻转Tab选项卡切换特效

    实例:3D翻转Tab选项卡切换特效 技术栈:HTML+CSS 效果: 源码: 【HTML】

    2024年02月04日
    浏览(48)
  • HTML5+CSS3+JS小实例:快捷菜单图标按钮交互特效

    实例:快捷菜单图标按钮交互特效 技术栈:HTML+CSS+JS 字体图标库:font-awesome 效果: 源码: 【html】

    2024年02月17日
    浏览(38)
  • 高端大气自适应全屏酷炫渐变卡片html源码图片切换特效html5源码导航引导网站源码

    源码特点: 1:手工书写DIV+CSS、代码精简无冗余。 2:自适应结构,全球先进技术,高端视觉体验。 3:SEO框架布局,栏目及文章页均可独立设置标题//描述。 4:附带测试数据、安装教程、入门教程、安全及备份教程。 5:后台直接修酷炫渐变卡片html源码,可以做个人

    2024年04月13日
    浏览(28)
  • 情人节程序员用HTML网页表白【可爱的节日贺卡ui动画特效】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript

    这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看。 此波共有100个表白网站,可以任意修改和使用,很多人会希望向心爱的男孩女孩告白,生性腼腆的人即使那个TA站在眼前都不敢向前表白。说不出口的话就用网页告诉

    2024年02月02日
    浏览(43)
  • Python烟花代码,用Python制作一个烟花特效

    Python实现浪漫的烟花特效 现在很多地方都不能放烟花了,既然看不到, 那作为程序猿的我们还不能自己用代码做一个吗? 今天就带大家用代码做一个烟花特效吧。 pygame介绍 关于Pygame的基本信息,pygame是什么,谁会被Pygame吸引,并且在哪里找到它。 Pygame是被设计用来写游戏

    2024年02月08日
    浏览(36)
  • Unity烟花特效实现(附源码)

    朋友过生,不知道送什么礼物,就想着用自己所学知识做个特效当礼物吧,嘿。 主要参考了 这位up的视频 ,感谢 https://github.com/hahahappyboy/UnityProjects/tree/main/%E7%83%9F%E8%8A%B1(%E7%B2%92%E5%AD%90%E7%B3%BB%E7%BB%9F) 主要就是1个主烟花粒子系统带3个子粒子系统,这三个小的粒子系统分别是拖

    2024年02月08日
    浏览(43)
  • python和pygame实现烟花特效

    新年来临之际,来一个欢庆新年烟花祝贺,需要安装使用第三方库pygame,关于Python中pygame游戏模块的安装使用可见 https://blog.csdn.net/cnds123/article/details/119514520 效果图及源码 先看效果图: 源码如下: pygame在屏幕上显示字体的方法说明 使用 pygame.font.Font 函数来设置字体和大小,

    2024年02月04日
    浏览(61)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包