佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > HTML5 Canvas draw方法制作動(dòng)畫效果示例

HTML5 Canvas draw方法制作動(dòng)畫效果示例

熱門標(biāo)簽:智能手機(jī) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 使用U盤裝系統(tǒng) 檢查注冊(cè)表項(xiàng) 阿里云 美圖手機(jī) 硅谷的囚徒呼叫中心 網(wǎng)站建設(shè)
HTML5 Canvas動(dòng)畫效果演示
主要思想:
首先要準(zhǔn)備一張有連續(xù)幀的圖片,然后利用HTML5 Canvas的draw方法在不同的時(shí)間間隔繪制不同的幀,這樣看起來就像動(dòng)畫在播放。
關(guān)鍵技術(shù)點(diǎn):
JavaScript 函數(shù)setTimeout()有兩個(gè)參數(shù),第一個(gè)是參數(shù)可以傳遞一個(gè)JavaScript方法,
另外一個(gè)參數(shù)代表間隔時(shí)間,單位為毫秒數(shù)。代碼示例:
setTimeout( update, 1000/30);
Canvas的API-drawImage()方法,需要指定全部9個(gè)參數(shù):
ctx.drawImage(myImage, offw, offh, width,height, x2, y2, width, height);
其中offw, offh是指源圖像的起始坐標(biāo)點(diǎn),width, height表示源圖像的寬與高,x2,y2表
示源圖像在目標(biāo)Canvas上的起始坐標(biāo)點(diǎn)。
一個(gè)22幀的大雁飛行圖片實(shí)現(xiàn)的效果:
 
源圖像:
 
程序代碼:

復(fù)制代碼
代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Mouse Event Demo</title>
<link href="default.css" rel="stylesheet" />
<script>
var ctx = null; // global variable 2d context
var started = false;
var mText_canvas = null;
var x = 0, y =0;
var frame = 0; // 22 5*5 + 2
var imageReady = false;
var myImage = null;
var px = 300;
var py = 300;
var x2 = 300;
var y2 = 0;
window.onload = function() {
var canvas = document.getElementById("animation_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}
// get 2D context of canvas and draw rectangel
ctx = canvas.getContext("2d");
ctx.fillStyle="black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
myImage = document.createElement('img');
myImage.src = "../robin.png";
myImage.onload = loaded();
}
function loaded() {
imageReady = true;
setTimeout( update, 1000/30);
}
function redraw() {
ctx.clearRect(0, 0, 460, 460)
ctx.fillStyle="black";
ctx.fillRect(0, 0, 460, 460);
// find the index of frames in image
var height = myImage.naturalHeight/5;
var width = myImage.naturalWidth/5;
var row = Math.floor(frame / 5);
var col = frame - row * 5;
var offw = col * width;
var offh = row * height;
// first robin
px = px - 5;
py = py - 5;
if(px < -50) {
px = 300;
}
if(py < -50) {
py = 300;
}
//var rate = (frame+1) /22;
//var rw = Math.floor(rate * width);
//var rh = Math.floor(rate * height);
ctx.drawImage(myImage, offw, offh, width, height, px, py, width, height);
// second robin
x2 = x2 - 5;
y2 = y2 + 5;
if(x2 < -50) {
x2 = 300;
y2 = 0;
}
ctx.drawImage(myImage, offw, offh, width, height, x2, y2, width, height);
}
function update() {
redraw();
frame++;
if (frame >= 22) frame = 0;
setTimeout( update, 1000/30);
}
</script>
</head>
<body>
<h1>HTML Canvas Animations Demo - By Gloomy Fish</h1>
<pre>Play Animations</pre>
<div id="my_painter">
<canvas id="animation_canvas"></canvas>
</div>
</body>
</html>

發(fā)現(xiàn)上傳透明PNG格式有點(diǎn)問題,所以我上傳不透明的圖片。可以用其它圖片替換,替換以后請(qǐng)修改最大幀數(shù)從22到你的實(shí)際幀數(shù)即可運(yùn)行。

標(biāo)簽:黃山 通遼 煙臺(tái) 湖北 懷化 山南 賀州 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5 Canvas draw方法制作動(dòng)畫效果示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    柳州市| 澳门| 巴楚县| 来安县| 上蔡县| 嵊泗县| 印江| 宜州市| 杭州市| 报价| 上高县| 巩留县| 金湖县| 淮滨县| 道真| 柏乡县| 全南县| 琼结县| 长乐市| 潜山县| 昆明市| 开鲁县| 四平市| 宁城县| 石景山区| 大港区| 时尚| 武宣县| 威海市| 东台市| 疏勒县| 龙陵县| 池州市| 滦平县| 府谷县| 陆丰市| 望奎县| 渝中区| 大竹县| 乡宁县| 洛阳市|