HTML5超炫酷粒子效果的进度条的实现示例


Posted in HTML / CSS onAugust 23, 2019

这是一款基于HTML5和JavaScript的进度条应用,这款进度条插件非常有特点,它在进度展示的时候呈现粒子的动画效果,也就是说,进度条在滑动的同时,会产生一些小粒子掉落下来,效果非常酷。另外还有一个特点是随着进度的变化,进度条的颜色也会变化。

HTML5超炫酷粒子效果的进度条的实现示例

JavaScript代码

/*========================================================*/  
/* Light Loader
/*========================================================*/
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;  

    /*========================================================*/    
    /* Initialize
    /*========================================================*/
    this.init = function(){
        this.loop();
    };

    /*========================================================*/    
    /* Utility Functions
    /*========================================================*/                
    this.rand = function(rMi, rMa){return ~~((Math.random()*(rMa-rMi+1))+rMi);};
    this.hitTest = function(x1, y1, w1, h1, x2, y2, w2, h2){return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);};

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

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

        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);
    };  

    /*========================================================*/    
    /* Particles
    /*========================================================*/
    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--){
            var p = this.particles[i];
            p.update(i);                                            
        };                      
    };

    this.renderParticles = function(){
        var i = this.particles.length;                      
        while(i--){
            var p = this.particles[i];
            p.render();                                         
        };                  
    };

    /*========================================================*/    
    /* Clear Canvas
    /*========================================================*/
    this.clearCanvas = function(){
        this.ctx.globalCompositeOperation = 'source-over';
        this.ctx.clearRect(0,0,this.cw,this.ch);                    
        this.ctx.globalCompositeOperation = 'lighter';
    };

    /*========================================================*/    
    /* Animation Loop
    /*========================================================*/
    this.loop = function(){
        var loopIt = function(){
            requestAnimationFrame(loopIt, _this.c);
            _this.clearCanvas();

            _this.createParticles();

            _this.updateLoader();
            _this.updateParticles();

            _this.renderLoader();
            _this.renderParticles();

        };
        loopIt();                   
    };

};

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

/*========================================================*/    
/* Setup requestAnimationFrame
/*========================================================*/
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, element){
            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);
        };
    };
};          

/*========================================================*/    
/* Define Canvas and Initialize
/*========================================================*/
if(isCanvasSupported){
  var c = document.createElement('canvas');
  c.width = 400;
  c.height = 100;           
  var cw = c.width;
  var ch = c.height;    
  document.body.appendChild(c); 
  var cl = new lightLoader(c, cw, ch);              

  setupRAF();
  cl.init();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

HTML / CSS 相关文章推荐
css3新增颜色表示方式分享
Apr 15 HTML / CSS
老生常谈CSS中的长度单位
Jun 27 HTML / CSS
js实现移动端H5页面手指滑动刻度尺功能
Nov 16 HTML / CSS
html5的画布canvas——画出弧线、旋转的图形实例代码+效果图
Jun 09 HTML / CSS
HTML5 window/iframe跨域传递消息 API介绍
Aug 26 HTML / CSS
HTML5标签使用方法详解
Nov 27 HTML / CSS
使用HTML5中的contentEditable来将多行文本自动增高
Mar 01 HTML / CSS
HTML5+CSS3实现机器猫
Oct 17 HTML / CSS
html5 postMessage解决跨域、跨窗口消息传递方案
Dec 20 HTML / CSS
HTML5中图片之间的缝隙完美解决方法
Jul 07 HTML / CSS
Canvas 文本填充线性渐变的使用详解
Jun 22 HTML / CSS
Html5调用企业微信的实现
Apr 16 HTML / CSS
HTML5语义化元素你真的用对了吗
Aug 22 #HTML / CSS
解决HTML5中滚动到底部的事件问题
Aug 22 #HTML / CSS
手摸手教你用canvas实现给图片添加平铺水印的实现
Aug 20 #HTML / CSS
html5 canvas实现给图片添加平铺水印
Aug 20 #HTML / CSS
导出HTML5 Canvas图片并上传服务器功能
Aug 16 #HTML / CSS
HTML5自定义属性的问题分析
Aug 16 #HTML / CSS
HTML5实现视频弹幕功能
Aug 09 #HTML / CSS
You might like
小文件php+SQLite存储方案
2010/09/04 PHP
详解PHP中的mb_detect_encoding函数使用方法
2015/08/18 PHP
隐藏Nginx或Apache以及PHP的版本号的方法
2016/01/03 PHP
如何利用预加载优化Laravel Model查询详解
2017/08/11 PHP
PHP封装请求类实例分析【基于Yii框架】
2019/10/17 PHP
Aster vs KG BO3 第一场2.19
2021/03/10 DOTA
抽出www.templatemonster.com的鼠标悬停加载大图模板的代码
2007/07/11 Javascript
jquery中map函数与each函数的区别实例介绍
2014/06/23 Javascript
JavaScript获取一个范围内日期的方法
2015/04/24 Javascript
jQuery实现的网页竖向菜单效果代码
2015/08/26 Javascript
原生js实现百叶窗效果及原理介绍
2016/04/12 Javascript
详解Vuejs2.0之异步跨域请求
2017/04/20 Javascript
ES6新特性五:Set与Map的数据结构实例分析
2017/04/21 Javascript
微信小程序 侧滑删除(左滑删除)
2017/05/23 Javascript
angular使用post、get向后台传参的问题实例
2017/05/27 Javascript
nodejs创建简易web服务器与文件读写的实例
2017/09/07 NodeJs
使用Layui搭建后台管理界面的操作方法
2019/09/20 Javascript
django创建最简单HTML页面跳转方法
2019/08/16 Python
Python字符串格式化输出代码实例
2019/11/22 Python
详解pycharm连接远程linux服务器的虚拟环境的方法
2020/11/13 Python
Python自动化测试基础必备知识点总结
2021/02/07 Python
浅谈基于Canvas的手绘风格图形库Rough.js
2018/03/19 HTML / CSS
高品质和独特的产品世界:Creations and Collections
2018/01/07 全球购物
索引覆盖(Index Covering)查询含义
2012/02/18 面试题
C# Debug和Testing相关面试题
2015/10/25 面试题
工商管理系学生的自我评价分享
2013/11/29 职场文书
物流管理专业毕业生求职信
2014/03/23 职场文书
《卖木雕的少年》教学反思
2014/04/11 职场文书
优秀教导主任事迹材料
2014/05/09 职场文书
多媒体教室标语
2014/06/26 职场文书
企业领导对照检查材料
2014/08/20 职场文书
个人买房协议书范本
2014/10/06 职场文书
教您:房贷工资收入证明应该怎么写?
2019/08/19 职场文书
Python数据分析之pandas函数详解
2021/04/21 Python
Python 发送SMTP邮件的简单教程
2021/06/24 Python
Python使用pandas导入csv文件内容的示例代码
2022/12/24 Python