JS开发下雪特效

  1. /* 控制下雪 */
  2. function snowFall(snow) {
  3. /* 可配置属性 */
  4. snow = snow || {};
  5. this.maxFlake = snow.maxFlake || 20; /* 最多片数 */
  6. this.flakeSize = snow.flakeSize || 10; /* 雪花形状 */
  7. this.fallSpeed = snow.fallSpeed || 1; /* 坠落速度 */
  8. }
  9. /* 兼容写法 */
  10. requestAnimationFrame = window.requestAnimationFrame ||
  11. window.mozRequestAnimationFrame ||
  12. window.webkitRequestAnimationFrame ||
  13. window.msRequestAnimationFrame ||
  14. window.oRequestAnimationFrame ||
  15. function(callback) { setTimeout(callback, 1000 / 60); };
  16. cancelAnimationFrame = window.cancelAnimationFrame ||
  17. window.mozCancelAnimationFrame ||
  18. window.webkitCancelAnimationFrame ||
  19. window.msCancelAnimationFrame ||
  20. window.oCancelAnimationFrame;
  21. /* 开始下雪 */
  22. snowFall.prototype.start = function(){
  23. /* 创建画布 */
  24. snowCanvas.apply(this);
  25. /* 创建雪花形状 */
  26. createFlakes.apply(this);
  27. /* 画雪 */
  28. drawSnow.apply(this)
  29. }
  30. /* 创建画布 */
  31. function snowCanvas() {
  32. /* 添加Dom结点 */
  33. var snowcanvas = document.createElement("canvas");
  34. snowcanvas.id = "snowfall";
  35. snowcanvas.width = window.innerWidth;
  36. snowcanvas.height = document.body.clientHeight;
  37. snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
  38. document.getElementsByTagName("body")[0].appendChild(snowcanvas);
  39. this.canvas = snowcanvas;
  40. this.ctx = snowcanvas.getContext("2d");
  41. /* 窗口大小改变的处理 */
  42. window.onresize = function() {
  43. snowcanvas.width = window.innerWidth;
  44. /* snowcanvas.height = window.innerHeight */
  45. }
  46. }
  47. /* 雪运动对象 */
  48. function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
  49. this.x = Math.floor(Math.random() * canvasWidth); /* x坐标 */
  50. this.y = Math.floor(Math.random() * canvasHeight); /* y坐标 */
  51. this.size = Math.random() * flakeSize + 2; /* 形状 */
  52. this.maxSize = flakeSize; /* 最大形状 */
  53. this.speed = Math.random() * 1 + fallSpeed; /* 坠落速度 */
  54. this.fallSpeed = fallSpeed; /* 坠落速度 */
  55. this.velY = this.speed; /* Y方向速度 */
  56. this.velX = 0; /* X方向速度 */
  57. this.stepSize = Math.random() / 30; /* 步长 */
  58. this.step = 0 /* 步数 */
  59. }
  60. flakeMove.prototype.update = function() {
  61. var x = this.x,
  62. y = this.y;
  63. /* 左右摆动(余弦) */
  64. this.velX *= 0.98;
  65. if (this.velY <= this.speed) {
  66. this.velY = this.speed
  67. }
  68. this.velX += Math.cos(this.step += .05) * this.stepSize;
  69. this.y += this.velY;
  70. this.x += this.velX;
  71. /* 飞出边界的处理 */
  72. if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
  73. this.reset(canvas.width, canvas.height)
  74. }
  75. };
  76. /* 飞出边界-放置最顶端继续坠落 */
  77. flakeMove.prototype.reset = function(width, height) {
  78. this.x = Math.floor(Math.random() * width);
  79. this.y = 0;
  80. this.size = Math.random() * this.maxSize + 2;
  81. this.speed = Math.random() * 1 + this.fallSpeed;
  82. this.velY = this.speed;
  83. this.velX = 0;
  84. };
  85. // 渲染雪花-随机形状(此处可修改雪花颜色!!!)
  86. flakeMove.prototype.render = function(ctx) {
  87. var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
  88. snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)"); /* 此处是雪花颜色,默认是白色 */
  89. snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)"); /* 若要改为其他颜色,请自行查 */
  90. snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)"); /* 找16进制的RGB 颜色代码。 */
  91. ctx.save();
  92. ctx.fillStyle = snowFlake;
  93. ctx.beginPath();
  94. ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
  95. ctx.fill();
  96. ctx.restore();
  97. };
  98. /* 创建雪花-定义形状 */
  99. function createFlakes() {
  100. var maxFlake = this.maxFlake,
  101. flakes = this.flakes = [],
  102. canvas = this.canvas;
  103. for (var i = 0; i < maxFlake; i++) {
  104. flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
  105. }
  106. }
  107. /* 画雪 */
  108. function drawSnow() {
  109. var maxFlake = this.maxFlake,
  110. flakes = this.flakes;
  111. ctx = this.ctx, canvas = this.canvas, that = this;
  112. /* 清空雪花 */
  113. ctx.clearRect(0, 0, canvas.width, canvas.height);
  114. for (var e = 0; e < maxFlake; e++) {
  115. flakes[e].update();
  116. flakes[e].render(ctx);
  117. }
  118. /* 一帧一帧的画 */
  119. this.loop = requestAnimationFrame(function() {
  120. drawSnow.apply(that);
  121. });
  122. }
  123. /* 调用及控制方法 */
  124. var snow = new snowFall({maxFlake:100});
  125. snow.start();