這篇文章主要介紹js如何通過循環(huán)多張圖片實(shí)現(xiàn)動(dòng)畫效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括嵊州網(wǎng)站建設(shè)、嵊州網(wǎng)站制作、嵊州網(wǎng)頁(yè)制作以及嵊州網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,嵊州網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到嵊州省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
以小魚搖尾巴和眨眼睛為例
動(dòng)畫思路:
1.將圖片資源放在數(shù)組里面
2.通過計(jì)時(shí)器來設(shè)定間隔時(shí)間
3.通過計(jì)數(shù)器來取相應(yīng)的圖片
第一步:基本框架,魚身體
document.body.onload = game; var can1, ctx1, canWidth, canHeight, lastTime = Date.now(), deltaTime = 0, body = new Image(); function game() { init(); gameloop(); } function init() { can1 = document.getElementById("canvas1"); //fonr--fishes, UI, circles, dust ctx1 = can1.getContext("2d"); canWidth = can1.width; canHeight = can1.height; body.src = './src/baby.png'; } function bodyDraw(){ ctx1.drawImage( body, -body.width * 0.5, -body.height * 0.5); } function gameloop() { requestAnimFrame(gameloop); //時(shí)間幀間隔 var now = Date.now(); deltaTime = now - lastTime; lastTime = now; ctx1.clearRect(0, 0, canWidth, canHeight); bodyDraw(); } window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( /* function FrameRequestCallback */ callback, /* DOMElement Element */ element) { return window.setTimeout(callback, 1000 / 60); }; })();
第二步:搖動(dòng)尾巴
1.圖片資源有8張,從tail0.png ~ tail7.png
2.尾巴是勻速的運(yùn)動(dòng),間隔時(shí)間為固定值
var bTailTimer, //計(jì)時(shí)器 bTailCount, //計(jì)數(shù)器 babyTail = []; //圖片數(shù)組 function init() { //尾巴初始化 bTailTimer = 0; 8 bTailCount = 0; 9 for (var i = 0; i < 8; i++) { babyTail[i] = new Image(); babyTail[i].src = './src/tail' + i +'.png'; } } function tailDraw(){ bTailTimer += deltaTime; if(bTailTimer > 50){ bTailCount = (bTailCount + 1)% 8; bTailTimer %= 50; //初始化計(jì)數(shù)器 } ctx1.drawImage( babyTail[bTailCount], -babyTail[bTailCount].width * 0.5, -babyTail[bTailCount].height * 0.5); } function gameloop() { ctx1.clearRect(0, 0, canWidth, canHeight); bodyDraw(); tailDraw(); }
第三步:眨眼睛
1.圖片資源有2張,從eye0.png ~ eye7.png
2.眼睛睜開時(shí)間不定時(shí),閉上時(shí)間固定值
var bEyeTimer, bEyeCount, bEyeInterval, //時(shí)間間隔變量 babyEye = []; function init() { //眼睛初始化 bEyeTimer = 0; bEyeCount = 0; bEyeInterval = 1000; //間隔時(shí)間 for (var i = 0; i < 2; i++) { babyEye[i] = new Image(); babyEye[i].src = './src/Eye' + i + '.png'; } } function eyeDraw() { bEyeTimer += deltaTime; if (bEyeTimer > bEyeInterval) { bEyeCount = (bEyeCount + 1)% 2; bEyeTimer %= bEyeInterval; if (bEyeCount == 0) { //眼睛睜開保持的時(shí)間隨機(jī) bEyeInterval = Math.random() * 1500 + 2000; //[2000,3500) } else { //眼睛閉上保持時(shí)間固定為100ms bEyeInterval = 100; } } } function gameloop() { eyeDraw(); }
以上是“js如何通過循環(huán)多張圖片實(shí)現(xiàn)動(dòng)畫效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!