基于HTML5+Webkit實現(xiàn)樹葉飄落動畫
發(fā)布時間:2017-12-28 15:56:29 作者:佚名
我要評論

本文給大家分享一段實例代碼給大家介紹基于HTML5+Webkit實現(xiàn)樹葉飄落動畫效果,需要的朋友參考下吧
實現(xiàn)如圖所示的東西效果(落葉下落):
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處理連續(xù)幀圖片,下面的代碼基于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;//初始化參數(shù) 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(); } //保證只有圖像加載后才開始循環(huán)動畫 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); } //為了讓圖片以規(guī)定的速度動畫,我們必須追蹤已經(jīng)經(jīng)過的時間,然后根據(jù)分配給每幀的時間播放幀?;静襟E是: //1、按每秒幾幀設(shè)置動畫速度(msPerFrame)。 //2、當(dāng)你循環(huán)游戲時,計算一下自最后一幀以后已經(jīng)經(jīng)過了多少時間(delta)。 //3、如果已經(jīng)經(jīng)過的時間足夠把動畫幀播完,那么播放這一幀并設(shè)置累積delta為0。 //4、如果已經(jīng)經(jīng)過的時間不夠,那么記?。ɡ鄯e)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; //當(dāng)繪制后且?guī)七M(jìn)完,計時器就會重置。 }else{ acDelta += delta; } lastUpdateTime = Date.now(); } //requestAnimFrame的作用基本上就是setTimeout,但瀏覽器知道你正在渲染幀,所以它可以優(yōu)化繪制循環(huán),以及如何與剩下的頁面回流。 //在某些情況下,setTimeout比requestAnimFrame更好用,特別是對于手機(jī)。 //以下是在不同的瀏覽器上調(diào)用requestAnimFrame的情況也不同,標(biāo)準(zhǔn)的檢測方法如下: window.requestAnimFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 / 3); //如果requestAnimFrame支持不可用,還是可以用回內(nèi)置的setTimeout。 }; })(); </script> </head> <body style="position:absolute;margin:0;padding:0;width:100%;height:100%;"> <canvas id="animation_canvas"></canvas> </body> </html>
總結(jié)
以上所述是小編給大家介紹的基于HTML5+Webkit實現(xiàn)樹葉飄落動畫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
html5結(jié)合Canvas實現(xiàn)的樹葉飄落動畫特效源碼
html5結(jié)合Canvas實現(xiàn)的樹葉飄落動畫特效源碼是一段實現(xiàn)了樹葉飄落效果的代碼,非常適合作為背景使用,本段代碼適應(yīng)于所有網(wǎng)頁使用,有需要的朋友們歡迎前來下載使用2017-05-02- 這篇文章主要為大家詳細(xì)介紹了一款逼真的HTML5樹葉飄落動畫,樹葉飄落的動畫效果非常真實,樹葉都是圖片,并非CSS3繪制,感興趣的小伙伴們可以參考一下2016-03-01
- 今天再來分享一款HTML5實現(xiàn)的樹葉飄落動畫特效源碼,這款HTML5樹葉飄落動畫是基于webkit內(nèi)核的,所以需要webkit內(nèi)核的瀏覽器才能播放該動畫,效果非常炫酷。樹葉飄落非常逼2016-03-01