canvas实现烟花特效

短命女 2022-03-17 08:27 591阅读 0赞

看过我之前博客的人一定都知道我对canvas赞不绝口,没看过?那就看看下面这篇吧,总结一下对canvas的使用心得就是盘它!

实现效果:

2019022310275268.gif

代码如下,注释已写:

  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. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>烟花爆炸动画</title>
  8. <style>
  9. body {
  10. padding: 0;
  11. margin: 0;
  12. overflow: hidden;
  13. background-color: black;
  14. }
  15. html,
  16. body {
  17. width: 100%;
  18. height: 100%;
  19. }
  20. </style>
  21. </head>
  22. <canvas id="canvas"></canvas>
  23. <script>
  24. var canvas = document.getElementById('canvas'); //获取canvas
  25. canvas.width = window.innerWidth; //设置画布大小
  26. canvas.height = window.innerHeight;
  27. var content2d = canvas.getContext('2d'); //转化为2d模式
  28. var balls = []; //建立数组用于存放小球
  29. function ball() { //构造函数方法开发
  30. this.color = null; //分别为小球的颜色,x y坐标,半径,角度,角度移动x,y坐标
  31. this.x = null;
  32. this.y = null;
  33. this.r = null;
  34. this.Angle = null;
  35. this.Anglex = null;
  36. this.Angley = null;
  37. this.init = function (x, y) {
  38. // 初始化状态
  39. this.x = x;
  40. this.y = y;
  41. this.r = this.randomNum(15, 25);
  42. this.color = this.randomColor();
  43. this.Angle = Math.random() * Math.PI * 2;
  44. this.Anglex = this.randomNum(10, 12) * Math.cos(this.Angle); //利用三角函数来控制小球的移动方向
  45. this.Angley = this.randomNum(10, 12) * Math.sin(this.Angle);
  46. };
  47. this.randomColor = function () { //随机颜色
  48. return "#" + parseInt(Math.random() * 16777216).toString(16);
  49. };
  50. this.randomNum = function (max, min) { //随机坐标**/*-*-+**
  51. return Math.random() * max + min;
  52. };
  53. this.move = function () {//利用坐标的偏移和半径的减少和重绘来实现小球的移动
  54. this.x += this.Anglex * 1.1;
  55. this.y += this.Angley * 1.1;
  56. this.r -= 0.35;
  57. this.Anglex *= 0.92;
  58. this.Angley *= 0.92;
  59. }
  60. }
  61. function createBall(x, y) {
  62. var count = parseInt(Math.random() * 15 + 15);//随机小球数量
  63. for (var i = 0; i < count; i++) {
  64. var b = new ball();
  65. b.init(x, y);
  66. balls.push(b);
  67. }
  68. }
  69. function Draw() {
  70. for (var i = 0; i < balls.length; i++) {
  71. var b = balls[i];
  72. b.move();
  73. content2d.beginPath();
  74. content2d.fillStyle = b.color;
  75. content2d.arc(b.x, b.y, b.r, 0, Math.PI * 2);
  76. content2d.fill();
  77. content2d.closePath();
  78. // console.log(1);
  79. }
  80. }
  81. function removeBall() {//如果小球的半径小于等值,移除小球
  82. for (var i = 0; i < balls.length; i++) {
  83. var b = balls[i];
  84. if (b.r < 0.35) {
  85. balls.splice(i, 1);
  86. i--;
  87. }
  88. }
  89. }
  90. loop();
  91. function loop() {//(重绘)h5 计时器,强大好用
  92. // 清除整个canvas
  93. content2d.clearRect(0, 0, canvas.width, canvas.height);
  94. Draw();
  95. removeBall();
  96. // console.log(1);
  97. window.requestAnimationFrame(loop);
  98. }
  99. canvas.onclick = function (e) {
  100. var x = e.pageX;
  101. var y = e.pageY;
  102. // console.log(x);
  103. createBall(x, y);
  104. }
  105. </script>
  106. <body>
  107. </body>
  108. </html>

发表评论

表情:
评论列表 (有 0 条评论,591人围观)

还没有评论,来说两句吧...

相关阅读

    相关 canvas实现烟花特效

    > 看过我之前博客的人一定都知道我对canvas赞不绝口,没看过?那就看看下面这篇吧,总结一下对canvas的使用心得就是盘它! 实现效果: ![2019022310275