XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>html5 裝載圖片</title>
- </head>
- <body>
- <button onclick="drawImg1()">從左到右</button>
- <button onclick="drawImg2()">從中央到左右兩邊</button>
- <button onclick="drawImg3()">以逐漸橫向柵格</button>
- <hr/>
- <canvas class="canvas" id="canvas" width="600" height="300"></canvas>
- </body>
- </html>
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- //初始化
- var canvas = document.getElementById("canvas"),
- context = canvas.getContext("2d"),
- image = new Image();
- image.src = "img/test.jpg";
- //從左到右加載方法
- function drawImg1(){
- var drawWidth = 0,
- interval = setInterval(function(){
- context.drawImage(image, 0, 0, drawWidth, image.height, 0, 0, drawWidth, image.height);
- drawWidth += 20;
- if(drawWidth > canvas.width) clearInterval(interval);
- },100);
- }
- //從中央向左右兩邊拉開(kāi)加載方法
- function drawImg2(){
- var drawWidth = 0,
- drawLeft = canvas.width/2,
- interval = setInterval(function(){
- context.drawImage(image, drawLeft, 0, drawWidth, image.height, drawLeft, 0, drawWidth, image.height);
- drawWidth += 20;
- drawLeft -= 10;
- if(drawLeft < 0) clearInterval(interval);
- },100);
- }
- //以逐漸橫向柵格加載方法
- function drawImg3(){
- var drawWidth = 0,
- spaceWidth = canvas.width/20, //10指分割的塊數(shù)
- interval = setInterval(function(){
- for(var i = 0;i<20;i++){
- context.drawImage(image, i*spaceWidth, 0, drawWidth, image.height, i*spaceWidth, 0, drawWidth, image.height);
- }
- drawWidth += 5;
- if(drawWidth > spaceWidth) clearInterval(interval);
- },100);
- }
以上內(nèi)容是小編給大家介紹的HTML5 用動(dòng)畫(huà)的表現(xiàn)形式裝載圖像,希望對(duì)大家有所幫助!