当前位置:   article > 正文

写一段html烟花代码

烟火html代码
  1. <p>这是一段简单的 HTML 烟花代码:</p>
  2. <p>```html
  3. <!DOCTYPE html> </p>
  4. <html>
  5. <head>
  6. <title>烟花效果</title>
  7. </head>
  8. <body>
  9. <div id="fireworks"></div>
  10. <script>
  11. var c = document.getElementById("fireworks");
  12. var ctx = c.getContext("2d");
  13. var particles = [];
  14. var amount = 0;
  15. var mouse = {x:0,y:0};
  16. var colors = ["#f44336","#e91e63","#9c27b0","#3f51b5","#03a9f4","#009688","#8bc34a","#cddc39","#ffeb3b","#ffc107","#ff9800","#ff5722"];
  17. c.width = window.innerWidth;
  18. c.height = window.innerHeight;
  19. document.onmousemove = function(e){
  20. mouse.x = e.clientX || e.pageX;
  21. mouse.y = e.clientY || e.pageY;
  22. }
  23. function Particle(){
  24. this.x = mouse.x;
  25. this.y = mouse.y;
  26. this.vx = Math.random()*10-5;
  27. this.vy = Math.random()*10-5;
  28. this.radius = Math.random()*20+10;
  29. this.color = colors[Math.floor(Math.random()*colors.length)];
  30. this.life = 0;
  31. this.maxLife = Math.random()*80+20;
  32. }
  33. Particle.prototype.draw = function(){
  34. this.x += this.vx;
  35. this.y += this.vy;
  36. if(Math.random() < 0.1){
  37. this.vx = Math.random()*10-5;
  38. this.vy = Math.random()*10-5;
  39. }
  40. this.life++;
  41. if(this.life >= this.maxLife){
  42. particles[particles.indexOf(this)] = new Particle();
  43. }
  44. ctx.beginPath();
  45. ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
  46. ctx.fillStyle = this.color;
  47. ctx.fill();
  48. }
  49. function loop(){
  50. requestAnimationFrame(loop);
  51. ctx.clearRect(0,0,c.width,c.height);
  52. for(var i = 0; i < amount; i++){
  53. particles[i].draw();
  54. }
  55. }
  56. function init(){
  57. for(var i = 0; i < 150; i++){
  58. particles.push(new Particle());
  59. }
  60. amount = particles.length;
  61. }
  62. init();
  63. loop();
  64. </script>
  65. </
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/116295
推荐阅读
相关标签
  

闽ICP备14008679号