最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

原生js+canvas實現(xiàn)下雪效果

 更新時間:2020年08月02日 15:36:42   作者:Mr.王征  
這篇文章主要為大家詳細介紹了原生js+canvas實現(xiàn)下雪效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js+canvas實現(xiàn)下雪效果的具體代碼,供大家參考,具體內(nèi)容如下

效果展示:

源碼展示:

<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>canvas下雪效果(原生js)</title>
 <style>
 * {
  margin: 0;
  padding: 0
 }
 
 html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #222;
 }
 
 #canvas {
  position: absolute;
  top: 0;
  left: 0;
 }
 </style>
</head>
<body>
<canvas id="canvas"></canvas>
<img id="imgSnow" width="0" height="0" src="xh.png" alt="雪花">
<img id="bgSnow" width="100%" height="100%" src="bj.jpg" alt="背景">
 
<script>
 window.onload = function () {
 var canvas = document.getElementById("canvas");
 var imgSnow = document.getElementById("imgSnow");
 var bgSnow = document.getElementById("bgSnow");
 var ctx = canvas.getContext('2d');
 
 var mbody = document.querySelector("body");
 
 canvas.width =mbody.offsetWidth;
 canvas.height = mbody.offsetHeight;
 
 var GetRandomNum = function (Min, Max) {
  var Range = Max - Min;
  var Rand = Math.random();
  return (Min + Math.round(Rand * Range));
 }
 
 // console.log(GetRandomNum(0, canvas.width))
 
 var snowArray = {}; //雪花對象
 var snowIndex = 0; //標識符
 var setting = {
  num: 30, //數(shù)量
  snowSize: 20, //大小
  startX: Math.random() * canvas.width, //起始橫坐標
  startY: 0, //起始縱坐標
  vy: 0.01
 }
 
 function snow() {
  // 起始橫坐標
  this.x = Math.random() * canvas.width;
  // 起始縱坐標
  this.y = setting.startY;
  this.size = setting.snowSize + Math.random() * 10 - 10;
 
  //橫坐標偏移量
  this.vx = Math.random() * 3 - 2; //偏移量
  //縱坐標偏移量
  this.vy = Math.random() * 10;
 
  this.life = 0;
  this.maxLife = 100;
  this.id = snowIndex;
  //當前信息保存至對象snowArray
  snowArray[snowIndex] = this;
 
  snowIndex++;
 }
 
 snow.prototype.draw = function () {
  this.x += this.vx;
  this.y += this.vy;
  this.vy += setting.vy;
  this.life++;
 
  //刪除
  if (this.y > canvas.height * 0.9 - 20) {
  snowArray[this.id]
  } else if (this.life >= this.maxLife) {
  snowArray[this.id]
  }
  ctx.drawImage(imgSnow, this.x, this.y, this.size, this.size)
 }
 
 setInterval(function () {
  ctx.drawImage(bgSnow, 0, 0, canvas.width, canvas.height);
 
  //數(shù)
  for (var i = 0; i < setting.num; i++) {
  if (Math.random() > 0.97) {
   new snow();
  }
  }
 
  for (var i in snowArray) {
  snowArray[i].draw();
  }
 
 }, 100)202082104246954
 }
</script>
 
</body>
</html>

圖片:

雪花:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

德钦县| 衡水市| 乐至县| 元朗区| 神池县| 新余市| 乌拉特前旗| 灵丘县| 大田县| 孟州市| 鲁甸县| 斗六市| 鹤庆县| 上栗县| 扎赉特旗| 大同县| 达孜县| 天峻县| 泽普县| 崇文区| 渝北区| 平原县| 黄浦区| 武宁县| 略阳县| 冕宁县| 谷城县| 丰都县| 芮城县| 邹平县| 安多县| 抚宁县| 诏安县| 大姚县| 保亭| 千阳县| 定兴县| 延安市| 丰台区| 额尔古纳市| 呼伦贝尔市|