DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5教程
HTML5 Canvas 進度條特效:粒子飛濺的載入動畫
編輯:HTML5教程     

這個特效把普通的載入進度條做成了會噴出粒子的版本:進度條以 HSL 色相從紅漸变换到綠,前端不斷迸出受重力影響的小粒子,而粒子會以「加色」模式堆疊,看起來頗有霓虹感。請使用支援 HTML5 的瀏覽器觀看。

整體結構

<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>HTML5 粒子進度條</title>
<style>
body { background: #111; color: White; }
canvas {
    background: #111;
    border: 1px solid #171717;
    display: block;
    left: 50%;
    margin: -51px 0 0 -201px;
    position: absolute;
    top: 50%;
}
</style>
</head>
<body>
<script type="text/javascript">
/* 主程式放在這裡 */
</script>
</body>
</html>

lightLoader 建構函式

var lightLoader = function (c, cw, ch) {
    var _this = this;
    this.c = c;
    this.ctx = c.getContext('2d');
    this.cw = cw;
    this.ch = ch;
    this.loaded = 0;
    this.loaderSpeed = .6;        // 每幀前進的百分比
    this.loaderHeight = 10;
    this.loaderWidth = 310;
    this.loader = {
        x: (this.cw / 2) - (this.loaderWidth / 2),
        y: (this.ch / 2) - (this.loaderHeight / 2)
    };
    this.particles = [];
    this.particleLift = 180;      // 粒子初始上拋力道
    this.hueStart = 0;
    this.hueEnd = 120;
    this.hue = 0;
    this.gravity = .15;           // 重力加速度
    this.particleRate = 4;        // 每幀產生幾顆粒子

更新與繪製進度條

    this.updateLoader = function () {
        if (this.loaded < 100) {
            this.loaded += this.loaderSpeed;
        } else {
            this.loaded = 0;
        }
    };

    this.renderLoader = function () {
        this.ctx.fillStyle = '#000';
        this.ctx.fillRect(this.loader.x, this.loader.y, this.loaderWidth, this.loaderHeight);

        // 依進度百分比在 0~120 之間取色相
        this.hue = this.hueStart + (this.loaded / 100) * (this.hueEnd - this.hueStart);
        var newWidth = (this.loaded / 100) * this.loaderWidth;

        this.ctx.fillStyle = 'hsla(' + this.hue + ', 100%, 40%, 1)';
        this.ctx.fillRect(this.loader.x, this.loader.y, newWidth, this.loaderHeight);

        // 上半部壓暗,做出立體感
        this.ctx.fillStyle = '#222';
        this.ctx.fillRect(this.loader.x, this.loader.y, newWidth, this.loaderHeight / 2);
    };

粒子系統

    this.Particle = function () {
        this.x = _this.loader.x + ((_this.loaded / 100) * _this.loaderWidth) - _this.rand(0, 1);
        this.y = _this.ch / 2 + _this.rand(0, _this.loaderHeight) - _this.loaderHeight / 2;
        this.vx = (_this.rand(0, 4) - 2) / 100;
        this.vy = (_this.rand(0, _this.particleLift) - _this.particleLift * 2) / 100;
        this.width = _this.rand(1, 4) / 2;
        this.height = _this.rand(1, 4) / 2;
        this.hue = _this.hue;
    };

    this.Particle.prototype.update = function (i) {
        this.vx += (_this.rand(0, 6) - 3) / 100;   // 水平方向隨機擾動
        this.vy += _this.gravity;                  // 受重力落下
        this.x += this.vx;
        this.y += this.vy;
        if (this.y > _this.ch) {                   // 掉出畫面就回收
            _this.particles.splice(i, 1);
        }
    };

    this.Particle.prototype.render = function () {
        _this.ctx.fillStyle = 'hsla(' + this.hue + ', 100%, '
            + _this.rand(50, 70) + '%, ' + _this.rand(20, 100) / 100 + ')';
        _this.ctx.fillRect(this.x, this.y, this.width, this.height);
    };

    this.createParticles = function () {
        var i = this.particleRate;
        while (i--) { this.particles.push(new this.Particle()); }
    };
    this.updateParticles = function () {
        var i = this.particles.length;
        while (i--) { this.particles[i].update(i); }
    };
    this.renderParticles = function () {
        var i = this.particles.length;
        while (i--) { this.particles[i].render(); }
    };

清除畫布與動畫迴圈

    this.clearCanvas = function () {
        this.ctx.globalCompositeOperation = 'source-over';
        this.ctx.clearRect(0, 0, this.cw, this.ch);
        this.ctx.globalCompositeOperation = 'lighter';   // 粒子重疊處加色
    };

    this.loop = function () {
        var loopIt = function () {
            requestAnimationFrame(loopIt, _this.c);
            _this.clearCanvas();
            _this.createParticles();
            _this.updateLoader();
            _this.updateParticles();
            _this.renderLoader();
            _this.renderParticles();
        };
        loopIt();
    };
};

相容性處理與啟動

為了照顧舊版瀏覽器,程式碼先檢查 Canvas 是否可用,再手動掛上 requestAnimationFrame 的各家前綴版本(ms、moz、webkit、o),若仍然沒有,就用 setTimeout 模擬一個約 60fps 的替代實作。

var isCanvasSupported = function () {
    var elem = document.createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
};

var setupRAF = function () {
    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame']
            || window[vendors[x] + 'CancelRequestAnimationFrame'];
    }
    if (!window.requestAnimationFrame) {
        window.requestAnimationFrame = function (callback) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function () { callback(currTime + timeToCall); }, timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
    }
    if (!window.cancelAnimationFrame) {
        window.cancelAnimationFrame = function (id) { clearTimeout(id); };
    }
};

if (isCanvasSupported) {
    var c = document.createElement('canvas');
    c.width = 400;
    c.height = 100;
    var cl = new lightLoader(c, c.width, c.height);
    document.body.appendChild(c);
    setupRAF();
    cl.init();
}

可調參數

想改手感,優先動這幾個值:loaderSpeed 控制跑多快、gravity 控制粒子落得多快、particleRate 控制密度、hueEnd 決定收尾的顏色。把 gravity 調小、particleLift 調大,就會變成比較「太空感」的效果。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved