<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      如何使用 HTML5 Canvas 制作水波紋效果

        今天,我們繼續分享 JavaScript 實現的效果例子,這篇文章會介紹使用 JavaScript 實現水波紋效果。水波效果以圖片為背景,點擊圖片任意位置都會觸發。有時候,我們使用普通的 Javascript可以創建一個很有趣的解決功能。

       

       

      在線演示      源碼下載

       

      Step 1. HTML

      和以前一樣,首先是 HTML 代碼:

      <!DOCTYPE html>
      <html>
          <head>
              <meta charset=utf-8 />
              <title>Water drops effect</title>
              <link rel="stylesheet" href="css/main.css" type="text/css" />
              <script src="js/vector2d.js" type="text/javascript" charset="utf-8"></script>
              <script src="js/waterfall.js" type="text/javascript" charset="utf-8"></script>
          </head>
          <body>
              <div class="example">
                  <h3><a href="#">Water drops effect</a></h3>
      
                  <canvas id="water">HTML5 compliant browser required</canvas>
                  <div id="switcher">
                      <img onclick='watereff.changePicture(this.src);' src="data_images/underwater1.jpg" />
                      <img onclick='watereff.changePicture(this.src);' src="data_images/underwater2.jpg" />
                  </div>
                  <div id="fps"></div>
              </div>
          </body>
      </html> 

      Step 2. CSS

      這是用到的 CSS 代碼:

      body{background:#eee;margin:0;padding:0}
      .example{background:#FFF;width:600px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
      
      #water {
          width:500px;
          height:400px;
          display: block;
          margin:0px auto;
          cursor:pointer;
      }
      #switcher {
          text-align:center;
          overflow:hidden;
          margin:15px;
      }
      #switcher img {
          width:160px;
          height:120px;
      }
      

      Step 3. JS

      下面是主要的 JavaScript 代碼:

      function drop(x, y, damping, shading, refraction, ctx, screenWidth, screenHeight){
          this.x = x;
          this.y = y;
          this.shading = shading;
          this.refraction = refraction;
          this.bufferSize = this.x * this.y;
          this.damping = damping;
          this.background = ctx.getImageData(0, 0, screenWidth, screenHeight).data;
          this.imageData = ctx.getImageData(0, 0, screenWidth, screenHeight);
      
          this.buffer1 = [];
          this.buffer2 = [];
          for (var i = 0; i < this.bufferSize; i++){
              this.buffer1.push(0);
              this.buffer2.push(0);
          }
      
          this.update = function(){
              for (var i = this.x + 1, x = 1; i < this.bufferSize - this.x; i++, x++){
                  if ((x < this.x)){
                      this.buffer2[i] = ((this.buffer1[i - 1] + this.buffer1[i + 1] + this.buffer1[i - this.x] + this.buffer1[i + this.x]) / 2) - this.buffer2[i];
                      this.buffer2[i] *= this.damping;
                  } else x = 0;
              }
      
              var temp = this.buffer1;
              this.buffer1 = this.buffer2;
              this.buffer2 = temp;
          }
      
          this.draw = function(ctx){
              var imageDataArray = this.imageData.data;
              for (var i = this.x + 1, index = (this.x + 1) * 4; i < this.bufferSize - (1 + this.x); i++, index += 4){
                  var xOffset = ~~(this.buffer1[i - 1] - this.buffer1[i + 1]);
                  var yOffset = ~~(this.buffer1[i - this.x] - this.buffer1[i + this.x]);
                  var shade = xOffset * this.shading;
                  var texture = index + (xOffset * this.refraction  + yOffset * this.refraction * this.x) * 4;
                  imageDataArray[index] = this.background[texture] + shade; 
                  imageDataArray[index + 1] = this.background[texture + 1] + shade;
                  imageDataArray[index + 2] = 50 + this.background[texture + 2] + shade;
              }
              ctx.putImageData(this.imageData, 0, 0);
          }
      }
      
      var fps = 0;
      
      var watereff = {
          // variables
          timeStep : 20,
          refractions : 2,
          shading : 3,
          damping : 0.99,
          screenWidth : 500,
          screenHeight : 400,
          pond : null,
          textureImg : null,
          interval : null,
          backgroundURL : 'data_images/underwater1.jpg',
      
          // initialization
          init : function() {
              var canvas = document.getElementById('water');
              if (canvas.getContext){
      
                  // fps countrt
                  fps = 0;
                  setInterval(function() { 
                      document.getElementById('fps').innerHTML = fps / 2 + ' FPS'; 
                      fps = 0;
                  }, 2000);
      
                  canvas.onmousedown = function(e) {
                      var mouse = watereff.getMousePosition(e).sub(new vector2d(canvas.offsetLeft, canvas.offsetTop));
                      watereff.pond.buffer1[mouse.y * watereff.pond.x + mouse.x ] += 200;
                  }
                  canvas.onmouseup = function(e) {
                      canvas.onmousemove = null;
                  }
      
                  canvas.width  = this.screenWidth;
                  canvas.height = this.screenHeight;
                  this.textureImg = new Image(256, 256);
                  this.textureImg.src = this.backgroundURL;
                  canvas.getContext('2d').drawImage(this.textureImg, 0, 0);
                  this.pond = new drop(
                      this.screenWidth, 
                      this.screenHeight, 
                      this.damping,
                      this.shading, 
                      this.refractions,
                      canvas.getContext('2d'),
                      this.screenWidth, this.screenHeight
                  );
                  if (this.interval != null){
                      clearInterval(this.interval);
                  }
                  this.interval = setInterval(watereff.run, this.timeStep);
              }
          },
      
          // change image func
          changePicture : function(url){
              this.backgroundURL = url;
              this.init();
          },
      
          // get mouse position func
          getMousePosition : function(e){
              if (!e){
                  var e = window.event;
              } 
              if (e.pageX || e.pageY){
                  return new vector2d(e.pageX, e.pageY);
              } else if (e.clientX || e.clientY){
                  return new vector2d(e.clientX, e.clientY);
              } 
          },
      
          // loop drawing
          run : function(){
              var ctx = document.getElementById('water').getContext('2d');
              watereff.pond.update();
              watereff.pond.draw(ctx);
              fps++;
          }
      }
      
      window.onload = function(){
          watereff.init();
      }

        正如你所看到的,這里使用 Vector2D 函數,這個函數在 vector2d.js 里提供了。另一個很難的方法是使用純數學實現,感興趣的可以自己實驗一下。

       

      您可能感興趣的相關文章

       

      本文鏈接:如何使用 HTML5 Canvas 制作水波紋效果

      編譯來源:夢想天空 ◆ 關注前端開發技術 ◆ 分享網頁設計資源

      posted @ 2014-09-14 13:25  夢想天空(山邊小溪)  閱讀(28282)  評論(2)    收藏  舉報
      主站蜘蛛池模板: 久久久精品波多野结衣av| av无码一区二区大桥久未| 亚洲av无码精品蜜桃| 国产对白叫床清晰在线播放| 国产私拍福利精品视频| 国产精品中文字幕av| 尤物yw193无码点击进入| 久久精品波多野结衣| 国产成人永久免费av在线| 国产欧美另类精品久久久| 免费99视频| 91偷自国产一区二区三区| 无码中文字幕人妻在线一区| 亚洲高清WWW色好看美女| 久久综合国产一区二区三区| 国产婷婷精品av在线| 超碰伊人久久大香线蕉综合| 91午夜福利一区二区三区| 日韩国产av一区二区三区精品| 国产91精品调教在线播放| 91精品乱码一区二区三区| 国产一区在线播放av| 最近日本免费观看高清视频| 又大又粗又爽的少妇免费视频| 日本福利一区二区精品| 国产女人高潮视频在线观看| 国产专区一线二线三线码| 老色99久久九九爱精品| 精品亚洲精品日韩精品| 国产精品久久毛片av大全日韩| 久久精品国产九一九九九| 东方av四虎在线观看| 午夜福利伦伦电影理论片在线观看| 柠檬福利第一导航在线| 久久天天躁夜夜躁狠狠| 亚洲色拍拍噜噜噜最新网站| 国产办公室秘书无码精品99| 狠狠色丁香婷婷综合尤物| 黄频在线播放观看免费| 国产99视频精品免费视频36| 色悠悠国产精品免费在线|