原生js写的雪花效果,使用画布

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>雪花效果,使用画布</title>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<style>
* {
    margin:0;
    padding:0;
}
html,body {
    background:#081d4d;
}
#vas {
    position:absolute;
    vertical-align:top;
    z-index:1000; 
    pointer-events:none;
}
</style>
</head>
<body>
<canvas id="vas"></canvas>


<script>
  (function() {
      var can = document.getElementById("vas");
      var widths = document.documentElement.clientWidth || document.body.clientWidth;
      var heights = document.documentElement.clientHeight || document.body.clientHeight;
      can.width = widths;
      can.height = heights;
      var conm = 1000;
      var ctx = can.getContext("2d");
      ctx.fillStyle = "#fff";
      var Snowflake = function() {
          this.x = 0;
          this.y = 0;
          this.vy = 0;
          this.vx = 0;
          this.r = 0;
          this.reset();
      };
      Snowflake.prototype.reset = function() {
          this.x = Math.random() * widths;
          this.y = Math.random() * -heights;
          this.vy = 1 + Math.random() * 3;
          this.vx = 0.5 - Math.random();
          this.r = 1 + Math.random() * 2;
          this.o = 0.5 + Math.random() * 0.5;
      };
      var snowflakes = [],
          snowflake;
      for (i = 0; i < conm; i++) {
          snowflake = new Snowflake();
          snowflakes.push(snowflake);
      }
      setInterval(function() {
          ctx.clearRect(0, 0, widths, heights);
          for (i = 0; i < conm; i++) {
              snowflake = snowflakes[i];
              snowflake.y += snowflake.vy;
              snowflake.x += snowflake.vx;
              ctx.globalAlpha = snowflake.o;
              ctx.beginPath();
              ctx.arc(snowflake.x, snowflake.y, snowflake.r, 0, Math.PI * 2);
              ctx.closePath();
              ctx.fill();
              if (snowflake.y > heights) {
                  snowflake.reset();
              }
          }
      }, 15);
  }());
</script>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值