Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 中文字幕免费播放,99r在线视频,精品久久久久久影院免费

          整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          canvas實現(xiàn)下雪背景圖


          anvas下雪背景 html+css+js


          實現(xiàn):

          1. 定義標(biāo)簽:

          <h1>北極光之夜。</h1>
              <div class="bg"></div>
              <canvas id="canvas"></canvas>
          

          h1是標(biāo)題,.bg是背景圖,canvas是畫布。

          2. 開始js部分,先獲取畫布:

           var canvas = document.getElementById("canvas");
           var ctx = canvas.getContext("2d");
          

          3.定義常量與數(shù)組,看注釋:

           
           /* 定義x為窗口寬度,y為窗口高度 */
                  var x = 0 ,y=0;      
                  /* 定義數(shù)組,是為了存儲每一片雪的信息 */
                  var arr=[];
                  /* 假設(shè)有600片雪 */
                  var num=600;
          

          4.初始化數(shù)組,給每片雪花定義隨機(jī)位置坐標(biāo)(x,y)與半徑(r)與一個顏色(color):

          for(let i=0;i<num;i++){
                      arr.push({
                          x: x*Math.random(),
                          y: y*Math.random(),
                          r: Math.random()*5,
                          color:`rgba(255,255,255,${Math.random()})`
                      })
                  }
          

          Math.random()會返回0到1間的一個隨機(jī)數(shù)。

          5.繪制每片雪與雪花:

           /* 創(chuàng)建image元素 */
                  var img = new Image();
                  /* 設(shè)置雪花的地址,只有雪花是用圖片表示 */
                  img.src = "img/snow.png";
                  function draw(){
                      /* 遍歷數(shù)組 */
                      for(let i=0;i<num;i++){
                          var yuan = arr[i];     
                          /* 創(chuàng)建路徑 */          
                           ctx.beginPath();
                           /* 給雪設(shè)置顏色 */
                           ctx.fillStyle = yuan.color;
                           /* 繪制雪 */
                          ctx.arc(yuan.x,yuan.y,yuan.r,0,2*3.14,false); 
                          /* 如果i%30==0才繪制雪花,雪花不用太多 */          
                          if(i%30==0){
                              /* 繪制雪花圖片 */
                          ctx.drawImage(img,yuan.x,yuan.y,yuan.r*10,yuan.r*10);
                          }
                          /* 填充路徑 */
                          ctx.fill();
                      }
                  }
                    
          

          6.更新雪的位置:

            function updated() {
                      for(let i=0;i<num;i++){
                          var yuan = arr[i];
                          /* x軸位置+0.1,變化小點 */
                          yuan.x = yuan.x + 0.1; 
                          /* y軸位置+自身半徑一半,這樣越大的學(xué)走越快 */
                          yuan.y = yuan.y+yuan.r/2;
                          /* 如果學(xué)已經(jīng)走出窗口 */
                          if(yuan.y>y){
                              /* 重新給雪數(shù)組賦值 */
                             arr[i]={ x: x*Math.random(),
                          y: -10,
                          r: Math.random()*5,
                          color:`rgba(255,255,255,${Math.random()})`}
                           }
                      }
             
                  }
          

          7.設(shè)置動畫:

           setInterval(function(){
                      /* 清屏 */
                      ctx.clearRect(0,0,x,y);
                      /* 繪制 */
                      draw();
                      /* 更新 */
                      updated(); 
                  },10)
          

          8.完善,讓x與y自適應(yīng)窗口寬度,調(diào)用下面函數(shù)便可:

          /* 綁定窗口大小發(fā)生改變事件,讓canvas隨時鋪滿瀏覽器可視區(qū) */
                window.onresize=resizeCanvas;
                  function resizeCanvas(){
                      x=canvas.width=window.innerWidth;
                      y=canvas.height=window.innerHeight;
                  }
                  resizeCanvas(); 
          

          完整代碼:

          下是一個簡單的PHP下雪效果代碼示例:

          ```php

          <!DOCTYPE html>

          <html>

          <head>

          <title>下雪效果</title>

          <style type="text/css">

          body {

          background-color: #333;

          overflow: hidden;

          }

          .snowflake {

          position: absolute;

          top: 0;

          left: 0;

          width: 10px;

          height: 10px;

          background-color: white;

          border-radius: 50%;

          animation: fall linear infinite;

          }

          @keyframes fall {

          from {top: -10px;}

          to {top: 100%;}

          }

          </style>

          </head>

          <body>

          <script type="text/javascript">

          for (var i = 0; i < 100; i++) {

          (function(i) {

          setTimeout(function() {

          var snowflake = document.createElement('div');

          snowflake.className = 'snowflake';

          snowflake.style.left = Math.random() * window.innerWidth + 'px';

          snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';

          document.body.appendChild(snowflake);

          }, Math.random() * 2000 + 500);

          })(i);

          }

          </script>

          </body>

          </html>

          ```

          該代碼使用JavaScript創(chuàng)建了100個雪花元素,并使用CSS動畫實現(xiàn)了雪花下落的效果。您可以將代碼保存為一個HTML文件并在瀏覽器中打開,即可看到下雪的動畫效果。

          女朋友常逛的設(shè)計網(wǎng)站這兩天頁面上多了下雪的效果,于是問我我的網(wǎng)站能下雪嗎,作為一個程序員我一般會說實現(xiàn)不了,但是作為男朋友,不能說不行。


          雪我們可以使用span標(biāo)簽和css的徑向漸變簡單意思一下:

          .snow {
            display: block;
            width: 100px;
            height: 100px;
            background-image: radial-gradient(#fff 0%, rgba(255, 255, 255, 0) 60%);
            border-radius: 50%;
          }
          復(fù)制代碼
          

          效果如下:

          很多雪

          一片雪是不夠的,成千上萬才浪漫,世界上沒有兩片相同的雪花,所以每片雪都有自己的大小位置速度等屬性,為此先創(chuàng)建一個雪花類:

          class Snow {
            constructor (opt = {}) {
              // 元素
              this.el = null
              // 直徑
              this.width = 0
              // 最大直徑
              this.maxWidth = opt.maxWidth || 80
              // 最小直徑
              this.minWidth = opt.minWidth || 2
              // 透明度
              this.opacity = 0
              // 水平位置
              this.x = 0
              // 重置位置
              this.y = 0
              // 速度
              this.speed = 0
              // 最大速度
              this.maxSpeed = opt.maxSpeed || 4
              // 最小速度
              this.minSpeed = opt.minSpeed || 1
              // 瀏覽器窗口尺寸
              this.windowWidth = window.innerWidth
              this.windowHeight = window.innerHeight
              
              this.init()
            }
          
            // 初始化各種屬性
            init () {
              this.width = Math.floor(Math.random() * this.maxWidth + this.minWidth)
              this.opacity = Math.random()
              this.x = Math.floor(Math.random() * (this.windowWidth - this.width))
              this.y = Math.floor(Math.random() * (this.windowHeight - this.width))
              this.speed = Math.random() * this.maxSpeed + this.minSpeed
            }
          
            // 設(shè)置樣式
            setStyle () {
              this.el.style.cssText = `
                position: fixed;
                left: 0;
                top: 0;
                display: block;
                width: ${this.width}px;
                height: ${this.width}px;
                opacity: ${this.opacity};
                background-image: radial-gradient(#fff 0%, rgba(255, 255, 255, 0) 60%);
                border-radius: 50%;
                z-index: 9999999999999;
                pointer-events: none;
                transform: translate(${this.x}px, ${this.y}px);
              `
            }
          
            // 渲染
            render () {
              this.el = document.createElement('div')
              this.setStyle()
              document.body.appendChild(this.el)
            }
          }
          復(fù)制代碼
          

          init方法用來生成隨機(jī)的初始大小、位置、速度等屬性,在瀏覽器窗口內(nèi)new100片試試:

          let snowList = []
          for (let i = 0; i < 100; i++) {
              let snow = new Snow()
              snow.render()
              snowList.push(snow)
          }
          復(fù)制代碼
          

          效果如下:

          動起來

          雪動起來才能叫下雪,動起來很簡單,不斷改變xy坐標(biāo)就可以了,給snow類加個運動的方法:

          class snow {
              move () {
                  this.x += this.speed
                  this.y += this.speed
                  this.el.style.left = this.x + 'px'
                  this.el.style.top = this.y + 'px'
              }
          }
          復(fù)制代碼
          

          接下來使用requestAnimationFrame不斷刷新:

          moveSnow () {
              window.requestAnimationFrame(() => {
                  snowList.forEach((item) => {
                      item.move()
                  })
                  moveSnow()
              })
          }
          復(fù)制代碼
          

          效果如下,因為速度是正數(shù),所以整體是往右斜的:

          可以看到動起來了,但是出屏幕就不見了,所以雪是會消失得對嗎?要讓雪不停很簡單,檢測雪的位置,如果超出屏幕了就讓它回到頂部,修改一下move方法:

          move () {
              this.x += this.speed
              this.y += this.speed
              // 完全離開窗口就調(diào)一下初始化方法,另外還需要修改一下init方法,因為重新出現(xiàn)我們是希望它的y坐標(biāo)為0或者小于0,這樣就不會又憑空出現(xiàn)的感覺,而是從天上下來的
              if (this.x < -this.width || this.x > this.windowWidth || this.y > this.windowHeight) {
                this.init(true)
                this.setStyle()
              }
              this.el.style.left = this.x + 'px'
              this.el.style.top = this.y + 'px'
            }
          復(fù)制代碼
          
          init (reset) {
              // ...
              this.width = Math.floor(Math.random() * this.maxWidth + this.minWidth)
              this.y = reset ? -this.width : Math.floor(Math.random() * this.windowHeight)
              // ...
            }
          復(fù)制代碼
          

          這樣就能源源不斷地下雪了:

          優(yōu)化

          1.水平速度

          水平和垂直方向的速度是一樣的,但是看起來有點太斜了,所以調(diào)整一下,把水平速度和垂直速度區(qū)分開來:

          class Snow {
            constructor (opt = {}) {
              // ...
              // 水平速度
              this.sx = 0
              // 垂直速度
              this.sy = 0
            // ...
            }
            
            init (reset) {
              // ...
              this.sy = Math.random() * this.maxSpeed + this.minSpeed
              this.sx = this.sy * Math.random()
            }
            
            move () {
              this.x += this.sx
              this.y += this.sy
              // ...
            }
          }
          復(fù)制代碼
          

          2.左下角沒有雪

          因為整體向右傾斜,所以左下角大概率沒有雪,這可以通過讓雪隨機(jī)出現(xiàn)在左側(cè)來解決:

          init (reset) {
            // ...
            this.x = Math.floor(Math.random() * (this.windowWidth - this.width))
            this.y = Math.floor(Math.random() * (this.windowHeight - this.width))
            if (reset && Math.random() > 0.8) {// 讓一小部分的雪初始化在左側(cè)
              this.x = -this.width
            } else if (reset) {
              this.y = -this.width
            }
            // ...
          }
          復(fù)制代碼
          

          3.眼前的雪

          隨機(jī)性的選擇一點雪給它較大的體積、透明度和速度,然后再使用css33D透視效果,把它的z軸數(shù)值調(diào)大一點,這樣的感覺就好像是在眼前劃過的一樣:

          <body style="perspective: 500;-webkit-perspective: 500"></body>
          復(fù)制代碼
          
          class Snow {
            constructor (opt = {}) {
              // ...
              // z軸數(shù)值
              this.z = 0
              // 快速劃過的最大速度
              this.quickMaxSpeed = opt.quickMaxSpeed || 10
              // 快速劃過的最小速度
              this.quickMinSpeed = opt.quickMinSpeed || 8
              // 快速劃過的寬度
              this.quickWidth = opt.quickWidth || 80
              // 快速劃過的透明度
              this.quickOpacity = opt.quickOpacity || 0.2
              // ...
            }
            
            init (reset) {
              let isQuick = Math.random() > 0.8
              this.width = isQuick ? this.quickWidth : Math.floor(Math.random() * this.maxWidth + this.minWidth)
              this.z = isQuick ? Math.random() * 300 + 200 : 0
              this.opacity = isQuick ? this.quickOpacity : Math.random()
              // ...
              this.sy = isQuick ? Math.random() * this.quickMaxSpeed + this.quickMinSpeed : Math.random() * this.maxSpeed + this.minSpeed
              // ...
            }
            
            move () {
              // ...
              this.el.style.transform = `translate3d(${this.x}px, ${this.y}px, ${this.z}px)`
            }
          }
          復(fù)制代碼
          

          4.鵝毛大雪

          雪花嘛,輕如鵝毛,鵝毛是怎么飄的?是不是左右擺動的飄?那我們也可以選擇一部分的雪花讓它跟鵝毛一樣飄,左右搖擺很簡單,速度一會加一會減就可以了:

          class Snow {
            constructor (opt = {}) {
              // ...
              // 是否左右搖擺
              this.isSwing = false
              // 左右搖擺的步長
              this.stepSx = 0.03
              // ...
            }
          
            // 隨機(jī)初始化屬性
            init (reset) {
              // ...
              this.isSwing = Math.random() > 0.8
              // ...
            }
          
            move () {
              if (this.isSwing) {
                if (this.sx >= 1 || this.sx <= -1) {
                  this.stepSx = -this.stepSx
                }
                this.sx += this.stepSx
              }
              // ...
            }
          }
          復(fù)制代碼
          

          除了上述這種方法,左右搖擺還有一種方式,就是使用正弦或余弦函數(shù),因為它們的曲線翻轉(zhuǎn)90度就是左右搖擺:

          img

          我們使用正弦函數(shù),公式為:y=sin(x),x的值是弧度表示,只要一直增加就可以了,y的值用來修改雪花的水平方向的速度變化步長:

          class Snow {
            constructor (opt = {}) {
              // ...
              // 是否左右搖擺
              this.isSwing = false
              // 左右搖擺的正弦函數(shù)x變量
              this.swingRadian = 0
              // 左右搖擺的正弦x步長
              this.swingStep = 0.01
              // ...
            }
          
            init (reset) {
              // ...
              this.swingStep = 0.01 * Math.random()
            }
          
            move () {
              if (this.isSwing) {
                this.swingRadian += this.swingStep
                this.x += this.sx * Math.sin(this.swingRadian * Math.PI) * 0.2
              } else {
                this.x += this.sx
              }
              // ...
            }
          }
          復(fù)制代碼
          

          因為正弦函數(shù)y的值是從1變化到-1,擺動幅度太了,所以乘了個小數(shù)0.2縮小一點,想要幅度小一點,還有一個方法是不要使用整個正弦曲線,可以從中截取一個適合的區(qū)間大小,比如就讓x的值在0.9π1.1π之前變化:

          class Snow {
            constructor (opt = {}) {
              // ...
              // 是否左右搖擺
              this.isSwing = false
              // 左右搖擺的正弦函數(shù)x變量
              this.swingRadian = 1// 需要改成一個中間值
              // 左右搖擺的正弦x步長
              this.swingStep = 0.01
              // ...
            }
          
            init (reset) {
              // ...
              this.swingStep = 0.01 * Math.random()
              this.swingRadian = Math.random() * (1.1 - 0.9) + 0.9// 也讓它隨機(jī)一下
            }
          
            move () {
              if (this.isSwing) {
                if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {
                  this.swingStep = -this.swingStep
                }
                this.swingRadian += this.swingStep
                this.x += this.sx * Math.sin(this.swingRadian * Math.PI)
              } else {
                this.x += this.sx
              }
              // ...
            }
          }
          復(fù)制代碼
          

          5.下得慢一點

          既然給水平加了曲線,垂直方向上是不是也可以改成非勻速呢?當(dāng)然可以,區(qū)別是速度得一直是正的,不然就要出現(xiàn)反自然現(xiàn)象了,改變速度曲線同樣可以使用正余弦,上面我們使用了0.9π1.1π之間的正弦曲線,根據(jù)上圖可以發(fā)現(xiàn)對應(yīng)的余弦曲線都是負(fù)的,趨勢是先慢后快,所以可以利用這一段來改變垂直方向的速度:

          move () {
            if (this.isSwing) {
              if (this.swingRadian > 1.1 || this.swingRadian < 0.9) {
                this.swingStep = -this.swingStep
              }
              this.swingRadian += this.swingStep
              this.x += this.sx * Math.sin(this.swingRadian * Math.PI)
              this.y -= this.sy * Math.cos(this.swingRadian * Math.PI)// 因為速度都是負(fù)的,所以改成-
            } else {
              this.x += this.sx
              this.y += this.sy
            }
            // ...
          }
          復(fù)制代碼
          

          6.在最上面

          為了防止為頁面上原本層級更高的元素遮擋,給雪花的樣式加一個很大的層級:

          render () {
              this.el = document.createElement('div')
              this.el.style.cssText = `
                  // ...
                  z-index: 9999999999999;
              `
              document.body.appendChild(this.el)
          }
          復(fù)制代碼
          

          7.看不見我

          修改了層級,所以雪花會在頁面的最上層,那么可能會擋住其他元素的鼠標(biāo)事件,需要禁止它響應(yīng)鼠標(biāo)事件:

          render () {
              this.el = document.createElement('div')
              this.el.style.cssText = `
                // ...
                pointer-events: none;
              `
              document.body.appendChild(this.el)
            }
          復(fù)制代碼
          

          8.更好一點

          使用性能更好的transform屬性來做動畫:

          render () {
              this.el = document.createElement('div')
              this.el.style.cssText = `
                  left: 0;
                  top: 0;
                  transform: translate(${this.x}px, ${this.y}px);
              `
              document.body.appendChild(this.el)
          }
          復(fù)制代碼
          
          move () {
              // ...
              // this.el.style.left = this.x + 'px'
              // this.el.style.top = this.y + 'px'
              this.el.style.transform = `translate(${this.x}px, ${this.y}px)`
          }
          復(fù)制代碼
          

          當(dāng)然,最好的方式是用canvas來畫。

          最終效果:

          下雨&雨夾雪

          下完雪,接下來順便下個雨,雨和雪差不多,都是從天上掉下來,但是雨的速度更快,通常也不會左右搖擺什么的,方向也基本是一致的,先來修改一下樣式:

          setStyle () {
            this.el.style.cssText = `
              // ...
              width: 1px;
              // ...
            `
          }
          復(fù)制代碼
          

          很簡單,只要把寬度寫死為1就行了:

          接下來把搖擺去掉:

          move () {
            this.x += this.sx
            this.y += this.sy
            // ...
          }
          復(fù)制代碼
          

          效果如下:

          可以發(fā)現(xiàn)雨是豎著在水平移動,顯然是不行的,需要讓它傾斜一定的角度,和運動方向保持一致,這個也很簡單,算一下斜率,水平速度除以垂直速度:

          move () {
            // ...
            this.el.style.transform = `translate(${this.x}px, ${this.y}px) ${this.getRotate(this.sy, this.sx)}`
          }
          getRotate(sy, sx) {
            return `rotate(${sx === 0 ? 0 : (90 + Math.atan(sy / sx) * (180 / Math.PI))}deg)`
          }
          復(fù)制代碼
          

          因為tan(θ)=sy/sx,θ=Math.atan(sy / sx),因為雨的線段默認(rèn)是從上到下垂直的,θ是代表和水平方向上的夾角,所以需要先旋轉(zhuǎn)90度,再旋轉(zhuǎn)夾角的度數(shù),最后弧度轉(zhuǎn)角度的公式為:角度=弧度*(180/π)。

          雨和雪都實現(xiàn)了,讓它們一起出來,就是雨夾雪了:

          根據(jù)天氣下雪

          把上面的代碼放到網(wǎng)站上就有下雪的效果了,另外也可以使用天氣廠商的api,根據(jù)實時天氣來下雪或者下雨,再實現(xiàn)一下太陽、烏云等效果,一個沉浸式天氣就完成了,有興趣的可自行實踐。


          完整代碼

          https://github.com/wanglin2/snow

          源自:juejin.cn/post/6910857740327845901


          主站蜘蛛池模板: 中文字幕精品一区二区三区视频| 亚洲一区二区三区国产精品无码| 亚洲av无码一区二区三区天堂| 无码日韩人妻AV一区二区三区| 无码午夜人妻一区二区三区不卡视频| 91精品福利一区二区| 国产亚洲日韩一区二区三区 | 无码国产精品一区二区免费式芒果 | 色婷婷一区二区三区四区成人网| 午夜视频一区二区| 亚洲一区二区三区免费在线观看| 久久久91精品国产一区二区三区| 亚洲福利视频一区二区| 国产人妖视频一区在线观看| 日韩国产免费一区二区三区| 久久综合亚洲色一区二区三区| 国产一区三区二区中文在线| 国产精品成人一区二区三区| 欧美人妻一区黄a片| 亚洲色偷精品一区二区三区| 精品无码一区二区三区在线| 日韩好片一区二区在线看| 中文字幕精品亚洲无线码一区| 国产精品女同一区二区久久 | 99热门精品一区二区三区无码| 午夜DV内射一区二区| 久久精品一区二区三区四区| 日本在线不卡一区| 久久久精品人妻一区二区三区四| 久久亚洲国产精品一区二区| 亚洲综合无码精品一区二区三区| 国产综合一区二区在线观看| 成人影片一区免费观看| 国产在线aaa片一区二区99| 国产在线精品一区二区不卡| 亚洲国产精品一区第二页| 一区二区三区四区视频| 亚洲欧美日韩中文字幕在线一区| 精品国产日韩亚洲一区在线| 蜜桃视频一区二区三区| 国产在线精品一区二区高清不卡|