1. 程式人生 > >原生js+canvas實現下雪效果

原生js+canvas實現下雪效果

 /* 控制下雪 */
  function snowFall(snow) {
      /* 可配置屬性 */
      snow = snow || {};
      this.maxFlake = snow.maxFlake || 200;   /* 最多片數 */
      this.flakeSize = snow.flakeSize || 10;  /* 雪花形狀 */
      this.fallSpeed = snow.fallSpeed || 1;   /* 墜落速度 */
  }
  /* 相容寫法 */
  requestAnimationFrame = window.requestAnimationFrame ||
      window.mozRequestAnimationFrame ||
      window.webkitRequestAnimationFrame ||
      window.msRequestAnimationFrame ||
      window.oRequestAnimationFrame ||
      function(callback) { setTimeout(callback, 1000 / 60); };

  cancelAnimationFrame = window.cancelAnimationFrame ||
      window.mozCancelAnimationFrame ||
      window.webkitCancelAnimationFrame ||
      window.msCancelAnimationFrame ||
      window.oCancelAnimationFrame;
  /* 開始下雪 */
  snowFall.prototype.start = function(){
      /* 建立畫布 */
      snowCanvas.apply(this);
      /* 建立雪花形狀 */
      createFlakes.apply(this);
      /* 畫雪 */
      drawSnow.apply(this)
  }
  /* 建立畫布 */
  function snowCanvas() {
      /* 新增Dom結點 */
      var snowcanvas = document.createElement("canvas");
      snowcanvas.id = "snowfall";
      snowcanvas.width = window.innerWidth;
      snowcanvas.height =document.documentElement.clientHeight;
      snowcanvas.setAttribute("style", "position:absolute; top: 0; left: 0; z-index: 1; pointer-events: none;");
      document.getElementsByTagName("body")[0].appendChild(snowcanvas);
      this.canvas = snowcanvas;
      this.ctx = snowcanvas.getContext("2d");
      /* 視窗大小改變的處理 */
      window.onresize = function() {
          snowcanvas.width = window.innerWidth;
          /* snowcanvas.height = window.innerHeight */
      }
  }
  /* 雪運動物件 */
  function flakeMove(canvasWidth, canvasHeight, flakeSize, fallSpeed) {
      this.x = Math.floor(Math.random() * canvasWidth);   /* x座標 */
      this.y = Math.floor(Math.random() * canvasHeight);  /* y座標 */
      this.size = Math.random() * flakeSize + 2;          /* 形狀 */
      this.maxSize = flakeSize;                           /* 最大形狀 */
      this.speed = Math.random() * 1 + fallSpeed;         /* 墜落速度 */
      this.fallSpeed = fallSpeed;                         /* 墜落速度 */
      this.velY = this.speed;                             /* Y方向速度 */
      this.velX = 0;                                      /* X方向速度 */
      this.stepSize = Math.random() / 30;                 /* 步長 */
      this.step = 0                                       /* 步數 */
  }
  flakeMove.prototype.update = function() {
      var x = this.x,
          y = this.y;
      /* 左右擺動(餘弦) */
      this.velX *= 0.98;
      if (this.velY <= this.speed) {
          this.velY = this.speed
      }
      this.velX += Math.cos(this.step += .05) * this.stepSize;

      this.y += this.velY;
      this.x += this.velX;
      /* 飛出邊界的處理 */
      if (this.x >= canvas.width || this.x <= 0 || this.y >= canvas.height || this.y <= 0) {
          this.reset(canvas.width, canvas.height)
      }
  };
  /* 飛出邊界-放置最頂端繼續墜落 */
  flakeMove.prototype.reset = function(width, height) {
      this.x = Math.floor(Math.random() * width);
      this.y = 0;
      this.size = Math.random() * this.maxSize + 2;
      this.speed = Math.random() * 1 + this.fallSpeed;
      this.velY = this.speed;
      this.velX = 0;
  };
  // 渲染雪花-隨機形狀(此處可修改雪花顏色!!!)
  flakeMove.prototype.render = function(ctx) {
      var snowFlake = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
      snowFlake.addColorStop(0, "rgba(255, 255, 255, 0.9)");  /* 此處是雪花顏色,預設是白色 */
      snowFlake.addColorStop(.5, "rgba(255, 255, 255, 0.5)"); /* 若要改為其他顏色,請自行查 */
      snowFlake.addColorStop(1, "rgba(255, 255, 255, 0)");    /* 找16進位制的RGB 顏色程式碼。 */
      ctx.save();
      ctx.fillStyle = snowFlake;
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
      ctx.fill();
      ctx.restore();
  };
  /* 建立雪花-定義形狀 */
  function createFlakes() {
      var maxFlake = this.maxFlake,
          flakes = this.flakes = [],
          canvas = this.canvas;
      for (var i = 0; i < maxFlake; i++) {
          flakes.push(new flakeMove(canvas.width, canvas.height, this.flakeSize, this.fallSpeed))
      }
  }
  /* 畫雪 */
  function drawSnow() {
      var maxFlake = this.maxFlake,
          flakes = this.flakes;
      ctx = this.ctx, canvas = this.canvas, that = this;
      /* 清空雪花 */
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      for (var e = 0; e < maxFlake; e++) {
          flakes[e].update();
          flakes[e].render(ctx);
      }
      /*  一幀一幀的畫 */
      this.loop = requestAnimationFrame(function() {
          drawSnow.apply(that);
      });
  }
  /* 呼叫及控制方法 */
  var snow = new snowFall({maxFlake:200});
  snow.start();

注意預設頁面背景色白色,雪花顏色也是白色