溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

HTML5和Webkit實(shí)現(xiàn)樹葉飄落動(dòng)畫的案例

發(fā)布時(shí)間:2020-10-26 09:21:06 來源:億速云 閱讀:173 作者:小新 欄目:web開發(fā)

小編給大家分享一下HTML5和Webkit實(shí)現(xiàn)樹葉飄落動(dòng)畫的案例,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

實(shí)現(xiàn)如圖所示的東西效果(落葉下落):

HTML5和Webkit實(shí)現(xiàn)樹葉飄落動(dòng)畫的案例

html代碼:

<!DOCTYPE html>
<html>
<head>
    <title>HTML5樹葉飄落動(dòng)畫</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>
    <p id="container">
        <p id="leafContainer"></p>
        <p id="message">
            <em>這是基于webkit的落葉動(dòng)畫</em>
        </p>
    </p>  
</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 > p {
    position: absolute;
    width: 100px;
    height: 100px;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-direction: normal;
    -webkit-animation-timing-function: linear;
}
#leafContainer > p > 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 leafp = document.createElement('p');
    leafp.style.top = "-100px";
    leafp.style.left = pixelValue(randomInteger(0, 500));
    leafp.style.webkitAnimationName = 'fade, drop';
    var fadeAndDropDuration = durationValue(randomFloat(5, 11));
    leafp.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration;
    var leafDelay = durationValue(randomFloat(0, 5));
    leafp.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;
    leafp.appendChild(image);
    return leafp;
}
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)動(dòng)畫
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ī)定的速度動(dòng)畫,我們必須追蹤已經(jīng)經(jīng)過的時(shí)間,然后根據(jù)分配給每幀的時(shí)間播放幀?;静襟E是:
//1、按每秒幾幀設(shè)置動(dòng)畫速度(msPerFrame)。
//2、當(dāng)你循環(huán)游戲時(shí),計(jì)算一下自最后一幀以后已經(jīng)經(jīng)過了多少時(shí)間(delta)。
//3、如果已經(jīng)經(jīng)過的時(shí)間足夠把動(dòng)畫幀播完,那么播放這一幀并設(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í)器就會(huì)重置。
    }else{
        acDelta += delta;
    }
    lastUpdateTime = Date.now();
}
//requestAnimFrame的作用基本上就是setTimeout,但瀏覽器知道你正在渲染幀,所以它可以優(yōu)化繪制循環(huán),以及如何與剩下的頁(yè)面回流。
//在某些情況下,setTimeout比requestAnimFrame更好用,特別是對(duì)于手機(jī)。
//以下是在不同的瀏覽器上調(diào)用requestAnimFrame的情況也不同,標(biāo)準(zhǔn)的檢測(cè)方法如下:
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>

以上是HTML5和Webkit實(shí)現(xiàn)樹葉飄落動(dòng)畫的案例的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI