您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關HTML5+Webkit如何實現樹葉飄落動畫,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
實現如圖所示的東西效果(落葉下落):
html代碼:
<!DOCTYPE html> <html> <head> <title>HTML5樹葉飄落動畫</title> <meta charset="utf-8"> <meta name="viewport" content="width=500px, initial-scale=0.64"> <link rel="stylesheet" href="leaves.css" type="text/css"> <script src="leaves.js" type="text/javascript"></script> </head> <body> <div id="container"> <div id="leafContainer"></div> <div id="message"> <em>這是基于webkit的落葉動畫</em> </div> </div> </body> </html> css代碼: body{ background-color: #4E4226; } #container { position: relative; height: 700px; width: 500px; margin: 10px auto; overflow: hidden; border: 4px solid #5C090A; background: #4E4226 url('images/backgroundLeaves.jpg') no-repeat top left; } #leafContainer { position: absolute; width: 100%; height: 100%; } #message{ position: absolute; top: 160px; width: 100%; height: 300px; background:transparent url('images/textBackground.png') repeat-x center; color: #5C090A; font-size: 220%; font-family: 'Georgia'; text-align: center; padding: 20px 10px; -webkit-box-sizing: border-box; -webkit-background-size: 100% 100%; z-index: 1; } em { font-weight: bold; font-style: normal; } #leafContainer > div { position: absolute; width: 100px; height: 100px; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: normal; -webkit-animation-timing-function: linear; } #leafContainer > div > img { position: absolute; width: 100px; height: 100px; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: alternate; -webkit-animation-timing-function: ease-in-out; -webkit-transform-origin: 50% -100%; } @-webkit-keyframes fade{ 0% { opacity: 1; } 95% { opacity: 1; } 100% { opacity: 0; } } @-webkit-keyframes drop{ 0% { -webkit-transform: translate(0px, -50px); } 100% { -webkit-transform: translate(0px, 650px); } } @-webkit-keyframes clockwiseSpin{ 0% { -webkit-transform: rotate(-50deg); } 100% { -webkit-transform: rotate(50deg); } } @-webkit-keyframes counterclockwiseSpinAndFlip { 0% { -webkit-transform: scale(-1, 1) rotate(50deg); } 100% { -webkit-transform: scale(-1, 1) rotate(-50deg); } } js代碼: const NUMBER_OF_LEAVES = 30; function init(){ var container = document.getElementById('leafContainer'); for (var i = 0; i < NUMBER_OF_LEAVES; i++) { container.appendChild(createALeaf()); } } function randomInteger(low, high){ return low + Math.floor(Math.random() * (high - low)); } function randomFloat(low, high){ return low + Math.random() * (high - low); } function pixelValue(value){ return value + 'px'; } function durationValue(value){ return value + 's'; } function createALeaf(){ var leafDiv = document.createElement('div'); leafDiv.style.top = "-100px"; leafDiv.style.left = pixelValue(randomInteger(0, 500)); leafDiv.style.webkitAnimationName = 'fade, drop'; var fadeAndDropDuration = durationValue(randomFloat(5, 11)); leafDiv.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration; var leafDelay = durationValue(randomFloat(0, 5)); leafDiv.style.webkitAnimationDelay = leafDelay + ', ' + leafDelay; var image = document.createElement('img'); image.src = 'images/realLeaf' + randomInteger(1, 5) + '.png'; var spinAnimationName = (Math.random() < 0.5) ? 'clockwiseSpin' : 'counterclockwiseSpinAndFlip'; image.style.webkitAnimationName = spinAnimationName; var spinDuration = durationValue(randomFloat(4, 8)); image.style.webkitAnimationDuration = spinDuration; leafDiv.appendChild(image); return leafDiv; } window.addEventListener('load', init, false);
PS:下面看下html5 canvas處理連續幀圖片,下面的代碼基于IE8以上
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <title>Canvas Demo</title> <script> var canvas = null;//初始化參數 var img = null; var ctx = null; var imageReady = false; window.onload = function() { var canvas = document.getElementById("animation_canvas"); 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); console.log(canvas.height); img = document.createElement('img'); img.src = "images/ab0.png"; img.onload = loaded(); } //保證只有圖像加載后才開始循環動畫 function loaded() { imageReady = true; setTimeout( update, 1000/3);//添加3幀每秒間隔計時器 } function redraw() { ctx.fillStyle="black"; ctx.fillRect(0, 0, 460, 460); ctx.drawImage(img, 0, 0, 232, 180); } //為了讓圖片以規定的速度動畫,我們必須追蹤已經經過的時間,然后根據分配給每幀的時間播放幀。基本步驟是: //1、按每秒幾幀設置動畫速度(msPerFrame)。 //2、當你循環游戲時,計算一下自最后一幀以后已經經過了多少時間(delta)。 //3、如果已經經過的時間足夠把動畫幀播完,那么播放這一幀并設置累積delta為0。 //4、如果已經經過的時間不夠,那么記住(累積)delta時間(acDelta)。 var frame = 0; var lastUpdateTime = 0; var acDelta = 0; var msPerFrame = 200; function update() { requestAnimFrame(update); var delta = Date.now() - lastUpdateTime; //console.log(Date.now(),lastUpdateTime); if (acDelta > msPerFrame){ acDelta = 0; redraw(); img.src='images/ab'+frame+'.png'; frame++; if(frame >= 3) frame = 0; //當繪制后且幀推進完,計時器就會重置。 }else{ acDelta += delta; } lastUpdateTime = Date.now(); } //requestAnimFrame的作用基本上就是setTimeout,但瀏覽器知道你正在渲染幀,所以它可以優化繪制循環,以及如何與剩下的頁面回流。 //在某些情況下,setTimeout比requestAnimFrame更好用,特別是對于手機。 //以下是在不同的瀏覽器上調用requestAnimFrame的情況也不同,標準的檢測方法如下: window.requestAnimFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 3); //如果requestAnimFrame支持不可用,還是可以用回內置的setTimeout。 }; })(); </script> </head> <body style="position:absolute;margin:0;padding:0;width:100%;height:100%;"> <canvas id="animation_canvas"></canvas> </body> </html>
關于“HTML5+Webkit如何實現樹葉飄落動畫”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。