前面做《四女神Online》模板的时候参照code pen上的一则飘雪效果改的。

首先引入HTML并想好canvas的ID:

  1. <canvas id='canvasbg' class="star_effect"></canvas>

然后引入JS:

  1. (function() {
  2.  
  3. var Base, Particle, canvas, colors, context, drawables, i, mouseX, mouseY, mouseVX, mouseVY, rand, click, min, max, colors, particles, Cwidth, Cheight, GradientColors, CanvasEl;
  4.  
  5. CanvasEl = "canvasbg"; //Canvas的ID
  6. min = 1; //最小光点直径
  7. max = 4; //最大光点直径
  8. particles = 200; //光点数量
  9. Cwidth = 800; //画布宽度
  10. Cheight = 600; //画布高度
  11. colors = ["255, 255, 255", "250, 150, 20", "255, 100, 255"]; //光点中间的颜色注意和晕开颜色顺序配对
  12. GradientColors = ["84,204,243", "250,150,20", "255, 100, 255"]; //光点晕开的颜色色注意和中间颜色顺序配对
  13. rand = function(a, b) {
  14. return Math.random() * (b - a) + a;
  15. };
  16.  
  17. Particle = (function() {
  18. function Particle() {
  19. this.reset();
  20. }
  21.  
  22. Particle.prototype.reset = function() {
  23. var colorSel = Math.random();
  24. this.color = colors[~~ (colorSel * colors.length)];
  25. this.GradientColors = GradientColors[~~ (colorSel * GradientColors.length)];
  26. this.radius = rand(min, max);
  27. this.x = rand(0, canvas.width);
  28. this.y = rand(60, canvas.height);
  29. this.vx = -5 + Math.random() * 10;
  30. this.vy = (Math.random() - 0.5) * this.radius;
  31. this.valpha = rand(0.02, 0.09);
  32. this.opacity = 0;
  33. this.life = 0;
  34. this.onupdate = undefined;
  35. this.type = "dust";
  36. };
  37.  
  38. Particle.prototype.update = function() {
  39. this.x = (this.x + this.vx / 6);
  40. this.y = (this.y + this.vy / 6);
  41.  
  42. if (this.opacity >= 1 && this.valpha > 0) this.valpha *= -1;
  43. this.opacity += this.valpha / 8;
  44. this.life += this.valpha / 8;
  45.  
  46. if (this.type === "dust") this.opacity = Math.min(1, Math.max(0, this.opacity));
  47. else this.opacity = 1;
  48.  
  49. if (this.onupdate) this.onupdate();
  50. if (this.life < 0 || this.radius <= 0 || this.y > canvas.height) {
  51. this.onupdate = undefined;
  52. this.reset();
  53. }
  54. };
  55.  
  56. Particle.prototype.draw = function(c) {
  57. var grd = c.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius * 4);
  58. grd.addColorStop(0, "rgba(" + this.GradientColors + ", " + Math.min(this.opacity - 0.2, 0.65) + ")");
  59. grd.addColorStop(1, "rgba(" + this.GradientColors + ", " + '0' + ")");
  60. c.fillStyle = grd;
  61.  
  62. c.beginPath();
  63. c.arc(this.x, this.y, this.radius * 4, 2 * Math.PI, false);
  64. c.fill();
  65.  
  66. c.strokeStyle = "rgba(" + this.color + ", " + Math.min(this.opacity, 0.85) + ")";
  67. c.fillStyle = "rgba(" + this.color + ", " + Math.min(this.opacity, 0.65) + ")";
  68. c.beginPath();
  69. c.arc(this.x, this.y, this.radius, 2 * Math.PI, false);
  70. c.fill();
  71. };
  72.  
  73. return Particle;
  74.  
  75. })();
  76.  
  77. mouseVX = mouseVY = mouseX = mouseY = 0;
  78.  
  79. canvas = document.getElementById(CanvasEl);
  80. context = canvas.getContext("2d");
  81. canvas.width = Cwidth;
  82. canvas.height = Cheight;
  83.  
  84. drawables = (function() {
  85. var _i, _results;
  86. _results = [];
  87. for (i = _i = 1; _i <= particles; i = ++_i) {
  88. _results.push(new Particle);
  89. }
  90. return _results;
  91. })();
  92.  
  93. function draw() {
  94. window.requestAnimFrame(draw);
  95. var d, _i, _len;
  96. canvas.width = Cwidth;
  97. canvas.height = Cheight;
  98. context.clearRect(0, 0, canvas.width, canvas.height)
  99.  
  100. for (_i = 0, _len = drawables.length; _i < _len; _i++) {
  101. d = drawables[_i];
  102. d.draw(context);
  103. }
  104. };
  105.  
  106. function update() {
  107. window.requestAnimFrame(update);
  108. var d, _i, _len, _results;
  109. _results = [];
  110. for (_i = 0, _len = drawables.length; _i < _len; _i++) {
  111. d = drawables[_i];
  112. _results.push(d.update());
  113. }
  114. return _results;
  115. };
  116.  
  117. document.onmousemove = function(e) {
  118. mouseVX = mouseX;
  119. mouseVY = mouseY;
  120. mouseX = ~~e.pageX;
  121. mouseY = ~~e.pageY;
  122. mouseVX = ~~ ((mouseVX - mouseX) / 2);
  123. mouseVY = ~~ ((mouseVY - mouseY) / 2);
  124.  
  125. };
  126.  
  127. window.addEventListener('resize', draw, false);
  128. window.requestAnimFrame = (function() {
  129. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
  130. function(callback) {
  131. window.setTimeout(callback, 1000 / 60);
  132. };
  133. })();
  134. window.requestAnimFrame(draw);
  135. window.requestAnimFrame(update);
  136. }).call(this);


可设置的参数有:
  1. CanvasEl = "canvasbg";//Canvas的ID
  2. min = 1;//最小光点直径
  3. max = 4;//最大光点直径
  4. particles = 200;//光点数量
  5. Cwidth = 800;//画布宽度
  6. Cheight = 600;//画布高度
  7. colors = ["255, 255, 255","250, 150, 20","255, 100, 255"];//光点中间的颜色注意和晕开颜色顺序配对
  8. GradientColors = ["84,204,243","250,150,20","255, 100, 255"];//光点晕开的颜色色注意和中间颜色顺序配对
注意:光点和光晕的顺序是配对的,比如第一个光点会搭配第一个光晕。