HTML5加载动画

<!doctype html><meta charset="utf-8" /><title>Sonic</title>

<style>

    body {        background: #222;        color: #FFF;        text-align: center;        font-family: sans-serif;        font-size: .8em;    }

    #container {        overflow: hidden;        padding: 20px;        width: 522px;        margin: 0 auto;    }

    #in {        overflow: hidden;    }

    a { color: #FFF; }

    div.l {        float: left;        margin: 20px 20px 0 0;        border: 2px solid #666;        background: #000;        height: 150px;        width: 150px;        position: relative;    }    .sonic {        position: absolute;        top: 50%;        left: 50%;        transform: translate(-50%, -50%);        -webkit-transform: translate(-50%, -50%);    }    canvas { display: block; }</style>

<body>

<div id="container">

    <h1>&lt;canvas&gt; loaders</h1>

    <div id="in"></div>

    <p>By <a href="http://padolsey.net">Padolsey</a> | <a href="https://github.com/padolsey/Sonic">Sonic on github</a></p>

</div>

<script>/* * Sonic 0.2.1 * -- * https://github.com/padolsey/Sonic * -- * This program is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */

(function(){

    var emptyFn = function(){};

    function Sonic(d) {

        this.converter = d.converter;

        this.data = d.path || d.data;        this.imageData = [];

        this.multiplier = d.multiplier || 1;        this.padding = d.padding || 0;

        this.fps = d.fps || 25;

        this.stepsPerFrame = ~~d.stepsPerFrame || 1;        this.trailLength = d.trailLength || 1;        this.pointDistance = d.pointDistance || .05;

        this.domClass = d.domClass || ‘sonic‘;

        this.backgroundColor = d.backgroundColor || ‘rgba(0,0,0,0)‘;        this.fillColor = d.fillColor;        this.strokeColor = d.strokeColor;

        this.stepMethod = typeof d.step == ‘string‘ ?                stepMethods[d.step] :        d.step || stepMethods.square;

        this._setup = d.setup || emptyFn;        this._teardown = d.teardown || emptyFn;        this._preStep = d.preStep || emptyFn;

        this.pixelRatio = d.pixelRatio || null;

        this.width = d.width;        this.height = d.height;

        this.fullWidth = this.width + 2 * this.padding;        this.fullHeight = this.height + 2 * this.padding;

        this.domClass = d.domClass || ‘sonic‘;

        this.setup();

    }

    var argTypes = Sonic.argTypes = {        DIM: 1,        DEGREE: 2,        RADIUS: 3,        OTHER: 0    };

    var argSignatures = Sonic.argSignatures = {        arc: [1, 1, 3, 2, 2, 0],        bezier: [1, 1, 1, 1, 1, 1, 1, 1],        line: [1,1,1,1]    };

    var pathMethods = Sonic.pathMethods = {        bezier: function(t, p0x, p0y, p1x, p1y, c0x, c0y, c1x, c1y) {

            t = 1-t;

            var i = 1-t,                    x = t*t,                    y = i*i,                    a = x*t,                    b = 3 * x * i,                    c = 3 * t * y,                    d = y * i;

            return [                a * p0x + b * c0x + c * c1x + d * p1x,                a * p0y + b * c0y + c * c1y + d * p1y            ]

        },        arc: function(t, cx, cy, radius, start, end) {

            var point = (end - start) * t + start;

            var ret = [                (Math.cos(point) * radius) + cx,                (Math.sin(point) * radius) + cy            ];

            ret.angle = point;            ret.t = t;

            return ret;

        },        line: function(t, sx, sy, ex, ey) {            return [                (ex - sx) * t + sx,                (ey - sy) * t + sy            ]        }    };

    var stepMethods = Sonic.stepMethods = {

        square: function(point, i, f, color, alpha) {            this._.fillRect(point.x - 3, point.y - 3, 6, 6);        },

        fader: function(point, i, f, color, alpha) {

            this._.beginPath();

            if (this._last) {                this._.moveTo(this._last.x, this._last.y);            }

            this._.lineTo(point.x, point.y);            this._.closePath();            this._.stroke();

            this._last = point;

        }

    }

    Sonic.prototype = {

        calculatePixelRatio: function(){

            var devicePixelRatio = window.devicePixelRatio || 1;            var backingStoreRatio = this._.webkitBackingStorePixelRatio                    || this._.mozBackingStorePixelRatio                    || this._.msBackingStorePixelRatio                    || this._.oBackingStorePixelRatio                    || this._.backingStorePixelRatio                    || 1;

            return devicePixelRatio / backingStoreRatio;        },

        setup: function() {

            var args,                    type,                    method,                    value,                    data = this.data;

            this.canvas = document.createElement(‘canvas‘);            this._ = this.canvas.getContext(‘2d‘);

            if(this.pixelRatio == null){                this.pixelRatio = this.calculatePixelRatio();            }

            this.canvas.className = this.domClass;

            if(this.pixelRatio != 1){

                this.canvas.style.height = this.fullHeight + ‘px‘;                this.canvas.style.width = this.fullWidth + ‘px‘;

                this.fullHeight *= this.pixelRatio;                this.fullWidth  *= this.pixelRatio;

                this.canvas.height = this.fullHeight;                this.canvas.width = this.fullWidth;

                this._.scale(this.pixelRatio, this.pixelRatio);

            }   else{

                this.canvas.height = this.fullHeight;                this.canvas.width = this.fullWidth;

            }

            this.points = [];

            for (var i = -1, l = data.length; ++i < l;) {

                args = data[i].slice(1);                method = data[i][0];

                if (method in argSignatures) for (var a = -1, al = args.length; ++a < al;) {

                    type = argSignatures[method][a];                    value = args[a];

                    switch (type) {                        case argTypes.RADIUS:                            value *= this.multiplier;                            break;                        case argTypes.DIM:                            value *= this.multiplier;                            value += this.padding;                            break;                        case argTypes.DEGREE:                            value *= Math.PI/180;                            break;                    };

                    args[a] = value;

                }

                args.unshift(0);

                for (var r, pd = this.pointDistance, t = pd; t <= 1; t += pd) {

                    // Avoid crap like 0.15000000000000002                    t = Math.round(t*1/pd) / (1/pd);

                    args[0] = t;

                    r = pathMethods[method].apply(null, args);

                    this.points.push({                        x: r[0],                        y: r[1],                        progress: t                    });

                }

            }

            this.frame = 0;

            if (this.converter && this.converter.setup) {                this.converter.setup(this);            }

        },

        prep: function(frame) {

            if (frame in this.imageData) {                return;            }

            this._.clearRect(0, 0, this.fullWidth, this.fullHeight);            this._.fillStyle = this.backgroundColor;            this._.fillRect(0, 0, this.fullWidth, this.fullHeight);

            var points = this.points,                    pointsLength = points.length,                    pd = this.pointDistance,                    point,                    index,                    frameD;

            this._setup();

            for (var i = -1, l = pointsLength*this.trailLength; ++i < l && !this.stopped;) {

                index = frame + i;

                point = points[index] || points[index - pointsLength];

                if (!point) continue;

                this.alpha = Math.round(1000*(i/(l-1)))/1000;

                this._.globalAlpha = this.alpha;

                if (this.fillColor) {                    this._.fillStyle = this.fillColor;                }                if (this.strokeColor) {                    this._.strokeStyle = this.strokeColor;                }

                frameD = frame/(this.points.length-1);                indexD = i/(l-1);

                this._preStep(point, indexD, frameD);                this.stepMethod(point, indexD, frameD);

            }

            this._teardown();

            this.imageData[frame] = (                    this._.getImageData(0, 0, this.fullWidth, this.fullWidth)            );

            return true;

        },

        draw: function() {

            if (!this.prep(this.frame)) {

                this._.clearRect(0, 0, this.fullWidth, this.fullWidth);

                this._.putImageData(                        this.imageData[this.frame],                        0, 0                );

            }

            if (this.converter && this.converter.step) {                this.converter.step(this);            }

            if (!this.iterateFrame()) {                if (this.converter && this.converter.teardown) {                    this.converter.teardown(this);                    this.converter = null;                }            }

        },

        iterateFrame: function() {

            this.frame += this.stepsPerFrame;

            if (this.frame >= this.points.length) {                this.frame = 0;                return false;            }

            return true;

        },

        play: function() {

            this.stopped = false;

            var hoc = this;

            this.timer = setInterval(function(){                hoc.draw();            }, 1000 / this.fps);

        },        stop: function() {

            this.stopped = true;            this.timer && clearInterval(this.timer);

        }    };

    window.Sonic = Sonic;

}());</script><script>

var loaders = [

    {

        width: 100,        height: 50,        padding: 10,

        stepsPerFrame: 2,        trailLength: 1,        pointDistance: .03,

        strokeColor: ‘#FF7B24‘,

        step: ‘fader‘,

        multiplier: 2,

        setup: function() {            this._.lineWidth = 5;        },

        path: [

            [‘arc‘, 10, 10, 10, -270, -90],            [‘bezier‘, 10, 0, 40, 20, 20, 0, 30, 20],            [‘arc‘, 40, 10, 10, 90, -90],            [‘bezier‘, 40, 0, 10, 20, 30, 0, 20, 20]        ]    },

    {

        width: 30,        height: 30,

        stepsPerFrame: 2,        trailLength: .3,        pointDistance: .1,        padding: 10,

        fillColor: ‘#D4FF00‘,        strokeColor: ‘#FFF‘,

        setup: function() {            this._.lineWidth = 20;        },

        path: [            [‘line‘, 0, 0, 30, 0],            [‘line‘, 30, 0, 30, 30],            [‘line‘, 30, 30, 0, 30],            [‘line‘, 0, 30, 0, 0]        ]    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 1,        trailLength: 1,        pointDistance: .025,

        strokeColor: ‘#05E2FF‘,

        fps: 20,

        setup: function() {            this._.lineWidth = 2;        },        step: function(point, index) {

            var cx = this.padding + 50,                    cy = this.padding + 50,                    _ = this._,                    angle = (Math.PI/180) * (point.progress * 360);

            this._.globalAlpha = Math.max(.5, this.alpha);

            _.beginPath();            _.moveTo(point.x, point.y);            _.lineTo(                    (Math.cos(angle) * 35) + cx,                    (Math.sin(angle) * 35) + cy            );            _.closePath();            _.stroke();

            _.beginPath();            _.moveTo(                    (Math.cos(-angle) * 32) + cx,                    (Math.sin(-angle) * 32) + cy            );            _.lineTo(                    (Math.cos(-angle) * 27) + cx,                    (Math.sin(-angle) * 27) + cy            );            _.closePath();            _.stroke();

        },        path: [            [‘arc‘, 50, 50, 40, 0, 360]        ]    },

    {

        width: 100,        height: 50,

        stepsPerFrame: 1,        trailLength: 1,        pointDistance: .1,        fps: 15,        padding: 10,        //step: ‘fader‘,

        fillColor: ‘#FF2E82‘,

        setup: function() {            this._.lineWidth = 20;        },

        path: [            [‘line‘, 0, 20, 100, 20],            [‘line‘, 100, 20, 0, 20]        ]    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 7,        trailLength: .7,        pointDistance: .01,        fps: 30,        fillColor: ‘white‘,

        setup: function() {            this._.lineWidth = 10;        },

        path: [            [‘line‘, 20, 70, 50, 20],            [‘line‘, 50, 20, 80, 70],            [‘line‘, 80, 70, 20, 70]        ]    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 4,        trailLength: 1,        pointDistance: .01,        fps: 25,

        fillColor: ‘#FF7B24‘,

        setup: function() {            this._.lineWidth = 10;        },

        step: function(point, i, f) {

            var progress = point.progress,                    degAngle = 360 * progress,                    angle = Math.PI/180 * degAngle,                    angleB = Math.PI/180 * (degAngle - 180),                    size = i*5;

            this._.fillRect(                    Math.cos(angle) * 25 + (50-size/2),                    Math.sin(angle) * 15 + (50-size/2),                    size,                    size            );

            this._.fillStyle = ‘#63D3FF‘;

            this._.fillRect(                    Math.cos(angleB) * 15 + (50-size/2),                    Math.sin(angleB) * 25 + (50-size/2),                    size,                    size            );

            if (point.progress == 1) {

                this._.globalAlpha = f < .5 ? 1-f : f;

                this._.fillStyle = ‘#EEE‘;

                this._.beginPath();                this._.arc(50, 50, 5, 0, 360, 0);                this._.closePath();                this._.fill();

            }

        },

        path: [            [‘line‘, 40, 10, 60, 90]        ]    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 3,        trailLength: 1,        pointDistance: .01,        fps: 30,        step: ‘fader‘,

        strokeColor: ‘#D4FF00‘,

        setup: function() {            this._.lineWidth = 6;        },

        path: [            [‘arc‘, 50, 50, 20, 360, 0]        ]    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 1,        trailLength: 1,        pointDistance: .02,        fps: 30,

        fillColor: ‘#05E2FF‘,

        step: function(point, index) {

            this._.beginPath();            this._.moveTo(point.x, point.y);            this._.arc(point.x, point.y, index * 7, 0, Math.PI*2, false);            this._.closePath();            this._.fill();

        },

        path: [            [‘arc‘, 50, 50, 30, 0, 360]        ]

    },

    {

        width: 100,        height: 100,

        stepsPerFrame: 1,        trailLength: 1,        pointDistance: .05,

        strokeColor: ‘#FF2E82‘,

        fps: 20,

        setup: function() {            this._.lineWidth = 4;        },        step: function(point, index) {

            var cx = this.padding + 50,                    cy = this.padding + 50,                    _ = this._,                    angle = (Math.PI/180) * (point.progress * 360),                    innerRadius = index === 1 ? 10 : 25;

            _.beginPath();            _.moveTo(point.x, point.y);            _.lineTo(                    (Math.cos(angle) * innerRadius) + cx,                    (Math.sin(angle) * innerRadius) + cy            );            _.closePath();            _.stroke();

        },        path: [            [‘arc‘, 50, 50, 40, 0, 360]        ]    }

];

var d, a, container = document.getElementById(‘in‘);

for (var i = -1, l = loaders.length; ++i < l;) {

    d = document.createElement(‘div‘);    d.className = ‘l‘;

    a = new Sonic(loaders[i]);

    d.appendChild(a.canvas);    container.appendChild(d);

    a.play();

}

</script>

</body>
时间: 2024-08-07 17:02:08

HTML5加载动画的相关文章

HTML5 五彩圆环Loading加载动画实现教程

今天我们要来介绍一款效果很特别的HTML5 Loading加载动画,不像其他的Loading动画,这款Loading动画颜色很丰富,并且在转圈的时候还有淡入淡出的效果.每一个圆环不停地旋转,从而实现加载动画的效果.首先你可以看看效果演示: 你也可以在这里查看在线演示 下面分享一下这款HTML5 Loading动画的实现过程,主要是HTML代码和CSS3代码,以及初始化的JS代码. 首先是HTML代码,只定义一个Loading容器,非常简单. HTML代码: <div id=”hold”><

超酷jQuery进度条加载动画集合

在丰富多彩的网页世界中,进度条加载动画的形式非常多样,有利用gif图片实现的loading动画,也有利用jQuery和CSS3实现的进度加载动画,本文主要向大家介绍很多jQuery和CSS3实现的进度条加载动画,每一个都非常具有创意.如果你喜欢,可以下载源码并将它们应用到自己的网站中去. HTML5 Canvas发光Loading动画 它是一个Loading加载动画,并不能实现具体进度的加载,但是可以提示用户数据或者页面正在加载.并且该应用利用Canvas绘制动画,效果非常不错. DEMO演示 

为网格布局图片打造的超炫 CSS 加载动画

今天,我想与大家分享一些专门为网格布局的图像制作的很酷的 CSS 加载动画效果.您可以把这些效果用在你的作品集,博客或任何你想要的网页中.设置很简单.我们使用了下面这些工具库来实现这个效果: Normalize.css 来替代传统的 CSS 复位: ZURB Foundation 创建具有响应式的网格: Masonry 创建一个动态的网格布局: imagesLoaded 检查是否已加载图像: Infinite Scroll加载更多图片并追加到画廊. 现在,让我们来看看一些实际的代码,这应该是大家

CSS 实现加载动画之五-光盘旋转

原文:CSS 实现加载动画之五-光盘旋转 今天做的这个动画叫光盘旋转,名字自己取的.动画的效果估计很多人都很熟悉,就是微信朋友圈里的加载动画.做过前面几个动画,发现其实都一个原理,就是如何将动画的元素如何分离出来.这个动画的实现也很简单,关键点在于如何将元素拼凑成风车形状.然后定义动画的关键帧为rotate 360度,应用于整个元素上,就可以使整个元素旋转起来.案例在请在chrome中查看. 1. 先看截图 2. 代码实现思路 2.1 首先还是定义四个元素,元素的中心为这四个元素组成的圆的圆心.

CSS 实现加载动画之四-圆点旋转

原文:CSS 实现加载动画之四-圆点旋转 圆点旋转也是加载动画中经常用到的.其实现方式和菊花旋转一样,只不过一个是线条形式,一个是圆点形式.圆点按照固定的旋转角度排列,加上延时的改变透明度的动画就可以实现.这个实现也比较简单. 1. 动画截图 2. 案例源代码 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" Content="text/html

CSS 实现加载动画之三-钢琴按键

原文:CSS 实现加载动画之三-钢琴按键 今天做的这个动画实现也是非常简单,简单数几行代码就可以搞定.给这个动画取了个优雅的名字--钢琴按键,也实在是想不出什么更形象的名字了.废话不多说,直接上图. 1. 先看gif图 2. 代码实现思路 2.1 定义五个方块的元素. 2.2 对方块元素使用动画,延时改变透明度. 3. 主要使用的技术 主要用到了animation动画 @-webkit-keyframes load{ 0%{opacity:1;} 100%{opacity:0;}}.m-load

几行css3代码实现超炫加载动画

之前为大家分享了css3实现的加载动画.今天为大家带来一款只需几行代码就可以实现超炫的动画加载特效.我们一起看下效果图: 在线预览   源码下载 实现代码: 极简的html代码: <div> <i></i> </div> css3代码: body { background: black; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; heig

使用Animate.css和wow.js,实现各大网站常用的页面加载动画

经常见到某个网站的动画是在屏幕显示到This元素的时候,会有个过渡动画. 主要区别于在普通页面是在页面加载完成后所有动画一起动,或者设置延迟时间. 而这两个插件混搭使用可以是页面现在到该元素时才加载动画.拥有非常不错的效果. 算个比较常见的交互效果,研究了一下午,主要使用以下两个插件完成. 1.wow.js 实现了在网页滚动时的动画效果,有漂亮的动画效果,依赖于Animate.css. 2.Animate.css 非常优秀的CSS3动画库,不依赖于jQuery,纯CSS动画 用法: <link

10个样式各异的CSS3 Loading加载动画

前几天我在园子里分享过2款很酷的CSS3 Loading加载动画,今天又有10个最新的Loading动画分享给大家,这些动画的样式都不一样,实现起来也并不难,你很容易把它们应用在项目中,先来看看效果图: 你也可以在这里查看DEMO演示. 下面我们来挑选几个比较典型的案例来分析一下代码. 先来看看第一个案例,是条状动画,HTML代码如下: <div id="caseVerte"> <div id="case1"></div> <