站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        HTML5編程實(shí)戰(zhàn)之二-使用動(dòng)畫的形式切換圖片代碼案例


        本文主要用到的知識(shí)

          本文主要用到了Canvas API中的drawImage方法,下面對(duì)此方法略做介紹。

          在Canvas API中繪制圖像用drawImage方法,這是一個(gè)重載方法,定義如下:

        context.drawImage(image,x,y);  context.drawImage(image,x,y,w,h);  context.drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh);

          第一個(gè)方法有三個(gè)參數(shù),第一個(gè)參數(shù)為要繪制的圖像(還可以是video元素),x和y為該圖像在畫布中的起始坐標(biāo)。

          第二個(gè)方法有五個(gè)參數(shù),前三個(gè)跟第一個(gè)方法意義一樣,w和h指繪制時(shí)的圖像寬度和高度,可以用來(lái)進(jìn)行圖像縮放。

          第三個(gè)方法有九個(gè)參數(shù),第一個(gè)參數(shù)跟前二個(gè)方法意義相同,后八個(gè)參數(shù)中前四個(gè)在源圖像上一取一個(gè)矩形,后四個(gè)參數(shù)用于在畫布上定義一個(gè)矩形,整個(gè)方法的作用就是把源圖像上部分區(qū)域(第二到五個(gè)參數(shù)定義的部分)復(fù)制到畫布上(后四個(gè)參數(shù)定義的部分)。

          本文主要利用了第三個(gè)方法完成繪制。

        源代碼

        <!DOCTYPE html><html><head>      <meta charset="UTF-8">      <title>用動(dòng)畫的形式切換圖片</title>      <script type="text/javascript">          var width, height;                  var context, image, functionId;                  var drawLeft, drawWidth;                  var drawTop, drawHeight;                  var spaceWidth, spaceHeight;                  var speed=5000;                  var images = ["http://i.6.cn/cvbnm/4e/7e/bb/75f251a8e2ae935d598f17b4f8275060.jpg",           "http://i.6.cn/cvbnm/4a/6e/fb/805175016e502c483b75276f29801df3.jpg",           "http://i.6.cn/cvbnm/6a/72/18/1787a3b2754ef48e374bbd14635f5c36.jpg",           "http://i.6.cn/cvbnm/94/55/6c/b1ba743ba617be2891fa06b67d795511.jpg",           "http://i.6.cn/cvbnm/02/1b/04/8018ee9e5756ac6b30f27d7ad6396b03.jpg",           "http://i.6.cn/cvbnm/85/ea/d1/65f15857b971afb3b6e38b5fcdadc9c0.jpg"];                  function selectFrom(iFirstValue, iLastValue) {                      var iChoices = iLastValue - iFirstValue + 1;                      return Math.floor(Math.random() * iChoices + iFirstValue);          }        function showPicture(effects) {                      var count = 0;                      for (var o in effects) {                  count++;              }                          var canvas = document.getElementById('canvas');              context = canvas.getContext('2d');              width = canvas.width;              height = canvas.height;                          var currImage = 0;              image = new Image();              image.src = images[currImage];              context.drawImage(image, 0, 0, width, height);              currImage++;                          if (count > 0) {                  setInterval(function () {                                      var rand =  selectFrom(0, count - 1);                      image.src = images[currImage];                      currImage++;                                          if (currImage == images.length) {                          currImage = 0;                      }                                          var index = 0;                                          for (var effect in effects) {                                              if (index++ == rand) {                              effects[effect]();                                                          break;                          }                      }                  }, speed);              }          }            window.onload=function(){              showPicture({                  leftToRight: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      drawWidth = 0;                      functionId = self.setInterval("drawleftToRight()", 10);                  },                  topToBottom: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      drawHeight = 0;                      functionId = self.setInterval("drawtopToBottom()", 10);                  },                  hcenter: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      drawLeft = width / 2;                      drawWidth = 0;                      functionId = self.setInterval("drawhcenter()", 10);                  },                  vcenter: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      drawTop = height / 2;                      drawHeight = 0;                      functionId = self.setInterval("drawvcenter()", 10);                  },                  hwindow: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      spaceWidth = width / 10;                      drawWidth = 0;                      functionId = self.setInterval("drawhwindow()", 50);                  },                  vwindow: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      spaceHeight = height / 10;                      drawHeight = 0;                      functionId = self.setInterval("drawvwindow()", 50);                  },                  hvwindow: function () {                      context.fillStyle = "#EEEEFF";                      context.fillRect(0, 0, width, height);                      spaceHeight = height / 10;                      spaceWidth = width / 10;                      drawWidth = 0;                      drawHeight = 0;                      functionId = self.setInterval("drawhvwindow()", 50);                  }              });          }        function drawleftToRight() {              context.drawImage(image, 0, 0, drawWidth, image.height, 0, 0, drawWidth, image.height);              drawWidth = drawWidth + 2;                          if (drawWidth > width) {                  window.clearInterval(functionId);              }          }        function drawtopToBottom() {              context.save();              context.drawImage(image, 0, 0, image.width, drawHeight, 0, 0, image.width, drawHeight);              drawHeight = drawHeight + 2;                          if (drawHeight > height) {                  window.clearInterval(functionId);              }              context.restore();          }                  function drawhcenter() {              context.save();              context.drawImage(image, drawLeft, 0, drawWidth, image.height, drawLeft, 0, drawWidth, image.height);              drawLeft = drawLeft - 1;              drawWidth = drawWidth + 2;                          if (drawLeft <= 0) {                  window.clearInterval(functionId);              }              context.restore();          }                  function drawvcenter() {              context.save();              context.drawImage(image, 0, drawTop, image.width, drawHeight, 0, drawTop, image.width, drawHeight);              drawTop = drawTop - 1;              drawHeight = drawHeight + 2;                          if (drawTop <= 0) {                  window.clearInterval(functionId);              }              context.restore();          }        function drawhwindow() {                      for (i = 0; i < 10; i++) {                  context.drawImage(image, 0 + i * spaceWidth, 0, drawWidth,                   image.height, 0 + i * spaceWidth, 0, drawWidth, image.height);              }              drawWidth += 1;                          if (drawWidth - 1 > spaceWidth) {                  window.clearInterval(functionId);              }          }        function drawvwindow() {              context.save();              context.clearRect(0, 0, width, height);                          for (i = 0; i < 10; i++) {                  context.drawImage(image, 0, 0 + i * spaceHeight, image.width,                   drawHeight, 0, 0 + i * spaceHeight, image.width, drawHeight);              }              drawHeight += 1;                          if (drawHeight - 1 > spaceHeight) {                  window.clearInterval(functionId);              }              context.restore();          }        function drawhvwindow() {              context.save();              context.clearRect(0, 0, width, height);                          for (i = 0; i < 10; i++) {                              for (j = 0; j < 10; j++) {                      context.drawImage(image, 0 + j * spaceWidth, 0 + i *                       spaceHeight, drawWidth, drawHeight, 0 + j * spaceWidth, 0 + i *                       spaceHeight, drawWidth, drawHeight);                  }              }              drawHeight += height / width;              drawWidth += 1;                          if (drawHeight > spaceHeight) {                  context.drawImage(image, 0, 0, width, height);                  window.clearInterval(functionId);              }              context.restore();          }    </script></head><body>      <h1>用動(dòng)畫的形式切換圖片</h1>      <canvas id="canvas" width="192px" height="255px"></canvas></body></html>

        代碼解析

          drawleftToRight():效果為從左向右顯示,主要把第四個(gè)參數(shù)和第八個(gè)參數(shù)從0逐漸增加到圖片的寬度

          drawtopToBottom():效果為從上向下顯示,主要把第五個(gè)參數(shù)和第九個(gè)參數(shù)從0逐漸增加到圖片的高度

          drawhcenter():效果為從中間水平向兩邊顯示,主要把第二、六個(gè)參數(shù)從圖像寬度的一半減小到0,第四、八個(gè)參數(shù)從0增加到圖像寬度

          drawvcenter():效果為從中間向上下兩邊顯示,跟上一個(gè)類似

          drawhwindow():效果為水平方向百葉窗,跟drawhcenter方法原理類似,只是這里是從多個(gè)地方進(jìn)行的

          drawvwindow():效果為垂直方面百葉窗,跟drawvcenter方法原理類似,只是這里是從多個(gè)地方進(jìn)行的

          drawhvwindow():效果為百葉窗,是drawhwindow()跟drawvwindow()的組合

          歡迎大家補(bǔ)充和完善。

          由于圖片是放在其他網(wǎng)站上,所以加載比較慢,顯的不是那么流暢,大家使用時(shí)可以換為本地圖片,效果更佳。

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 亚洲线精品一区二区三区| 国产成人精品无码一区二区| 精品久久久久久久无码| 国产欧美在线观看精品一区二区| 久久精品国产亚洲av影院| 久久久久久青草大香综合精品| 伊人久久精品线影院| 国产精品一区二区久久不卡| 亚洲国产主播精品极品网红| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 国产精品兄妹在线观看麻豆| 色偷偷88欧美精品久久久| 国产精品免费久久| 亚洲永久永久永久永久永久精品| 91麻豆精品国产| 国产精品免费一区二区三区四区| 无码精品人妻一区二区三区中| 亚洲欧洲精品成人久久曰影片 | 亚洲精品高清国产一久久| 国产精品视频色拍拍| 少妇精品久久久一区二区三区| 亚洲一级Av无码毛片久久精品| 免费精品国产自产拍在线观看| 精品乱码久久久久久夜夜嗨| 国产一区二区三区精品视频| 国产精品亚洲综合一区| 国产精品福利电影一区二区三区四区欧美白嫩精品 | 国内精品久久久久影院免费| 国产午夜福利精品一区二区三区 | 亚洲精品乱码久久久久久久久久久久| 欧美激情视频精品一区二区 | 自拍偷自拍亚洲精品被多人伦好爽| 亚洲av午夜成人片精品电影| 亚洲&#228;v永久无码精品天堂久久 | 国产AV国片精品| 国产精品区一区二区三在线播放| 国产日产韩国精品视频| 91精品国产高清久久久久久io| 高清免费久久午夜精品| 极品精品国产超清自在线观看| 久久99热狠狠色精品一区|