濮阳杆衣贸易有限公司

主頁 > 知識庫 > 基于HTML5+Webkit實(shí)現(xiàn)樹葉飄落動畫

基于HTML5+Webkit實(shí)現(xiàn)樹葉飄落動畫

熱門標(biāo)簽:當(dāng)涂高德地圖標(biāo)注 南寧點(diǎn)撥外呼系統(tǒng)哪家公司做的好 黃島區(qū)地圖標(biāo)注 成都智能外呼系統(tǒng)平臺 云南大理400電話申請官方 四川點(diǎn)撥外呼系統(tǒng) 電銷機(jī)器人電話用什么卡 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 江蘇智能電銷機(jī)器人哪家好

實(shí)現(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幀每秒間隔計(jì)時(shí)器
}
function redraw() {
    ctx.fillStyle="black";
    ctx.fillRect(0, 0, 460, 460);
    ctx.drawImage(img, 0, 0, 232, 180);
}   
//為了讓圖片以規(guī)定的速度動畫,我們必須追蹤已經(jīng)經(jīng)過的時(shí)間,然后根據(jù)分配給每幀的時(shí)間播放幀?;静襟E是:
//1、按每秒幾幀設(shè)置動畫速度(msPerFrame)。
//2、當(dāng)你循環(huán)游戲時(shí),計(jì)算一下自最后一幀以后已經(jīng)經(jīng)過了多少時(shí)間(delta)。
//3、如果已經(jīng)經(jīng)過的時(shí)間足夠把動畫幀播完,那么播放這一幀并設(shè)置累積delta為0。
//4、如果已經(jīng)經(jīng)過的時(shí)間不夠,那么記?。ɡ鄯e)delta時(shí)間(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)完,計(jì)時(shí)器就會重置。
    }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實(shí)現(xiàn)樹葉飄落動畫,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

標(biāo)簽:十堰 廣西 佳木斯 西寧 咸寧 酒泉 南京 淮安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于HTML5+Webkit實(shí)現(xiàn)樹葉飄落動畫》,本文關(guān)鍵詞  基于,HTML5+Webkit,實(shí)現(xiàn),樹葉,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于HTML5+Webkit實(shí)現(xiàn)樹葉飄落動畫》相關(guān)的同類信息!
  • 本頁收集關(guān)于基于HTML5+Webkit實(shí)現(xiàn)樹葉飄落動畫的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    富裕县| 上犹县| 西贡区| 独山县| 新野县| 游戏| 横峰县| 章丘市| 鄂温| 大新县| 普陀区| 渝北区| 唐山市| 绥芬河市| 保定市| 麻阳| 扬州市| 彭阳县| 宣化县| 章丘市| 浦江县| 北辰区| 营口市| 临洮县| 白城市| 龙陵县| 古蔺县| 无锡市| 通化市| 巴南区| 康平县| 济源市| 涞源县| 甘孜县| 南京市| 隆回县| 六盘水市| 丁青县| 凌云县| 神木县| 临西县|