当前位置:   article > 正文

烟花HTML代码(20231203)

烟花HTML代码(20231203)

最近烟花代码比较火,大家在网上找的一些烟花不太好看,所以我用chatgpt经过不断改良使用量子写了一个满意的html代码!

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <!-- Copyright by Zhangshao -->
  7. <title>烟花</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. overflow: hidden;
  12. background-color: #000;
  13. }
  14. canvas {
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="fireworksCanvas"></canvas>
  21. <script>
  22. document.addEventListener('DOMContentLoaded', function () {
  23. const canvas = document.getElementById('fireworksCanvas');
  24. const ctx = canvas.getContext('2d');
  25. canvas.width = window.innerWidth;
  26. canvas.height = window.innerHeight;
  27. function Particle(x, y, color) {
  28. this.x = x;
  29. this.y = y;
  30. this.color = color;
  31. this.radius = Math.random() * 2 + 1; // 更细的粒子
  32. this.velocity = {
  33. x: (Math.random() - 0.5) * 20, // 更快的速度
  34. y: (Math.random() - 0.5) * 20 // 更快的速度
  35. };
  36. this.opacity = 1;
  37. }
  38. Particle.prototype.draw = function () {
  39. ctx.beginPath();
  40. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
  41. ctx.fillStyle = this.color;
  42. ctx.globalAlpha = this.opacity;
  43. ctx.fill();
  44. ctx.globalAlpha = 1;
  45. };
  46. const particles = [];
  47. function createParticles(x, y, color) {
  48. for (let i = 0; i < 200; i++) { // 更多的粒子
  49. const particle = new Particle(x, y, color);
  50. particles.push(particle);
  51. }
  52. }
  53. function autoCreateFireworks() {
  54. const numberOfFireworks = 10; // 同时绽放的烟花数量
  55. for (let i = 0; i < numberOfFireworks; i++) {
  56. const x = Math.random() * canvas.width;
  57. const y = Math.random() * (canvas.height / 2); // 降低初始高度
  58. const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
  59. createParticles(x, y, color);
  60. }
  61. }
  62. // 自动创建烟花,每隔0.5秒触发一次
  63. setInterval(autoCreateFireworks, 500);
  64. function animateParticles() {
  65. for (let i = particles.length - 1; i >= 0; i--) {
  66. particles[i].x += particles[i].velocity.x;
  67. particles[i].y += particles[i].velocity.y;
  68. particles[i].velocity.y += 0.1;
  69. particles[i].opacity -= 0.03; // 调整透明度的减小速度
  70. if (
  71. particles[i].x < 0 ||
  72. particles[i].x > canvas.width ||
  73. particles[i].y < 0 ||
  74. particles[i].y > canvas.height
  75. ) {
  76. // 移出画布的粒子将被删除
  77. particles.splice(i, 1);
  78. } else {
  79. particles[i].draw();
  80. }
  81. }
  82. }
  83. function animate() {
  84. ctx.clearRect(0, 0, canvas.width, canvas.height);
  85. animateParticles();
  86. requestAnimationFrame(animate);
  87. }
  88. animate();
  89. // Resize canvas when the window is resized
  90. window.addEventListener('resize', function () {
  91. canvas.width = window.innerWidth;
  92. canvas.height = window.innerHeight;
  93. });
  94. });
  95. </script>
  96. </body>
  97. </html>

解决了很多问题,但是由于达到效果,所以往后运行可能比较吃力,运行一会儿就可以了!

浏览效果请至:http://zzj666.club/yanhua

抖音:CALL.ME.ZZ感谢观看!我们下次见!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/90916
推荐阅读
相关标签
  

闽ICP备14008679号