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让登陆面板3D旋转起来
May 03 HTML / CSS
使用CSS变量实现炫酷惊人的悬浮效果
Apr 26 HTML / CSS
CSS3中媒体查询结合rem布局适配手机屏幕
Jun 10 HTML / CSS
HTML5 Canvas API中drawImage()方法的使用实例
Mar 25 HTML / CSS
canvas学习笔记之绘制简单路径
Jan 28 HTML / CSS
html5-websocket基于远程方法调用的数据交互实现
Dec 04 HTML / CSS
HTML5 placeholder(空白提示)属性介绍
Aug 07 HTML / CSS
图片上传插件ImgUploadJS:用HTML5 File API 实现截图粘贴上传、拖拽上传
Jan 20 HTML / CSS
如何用H5实现一个触屏版的轮播器的实例
Jan 09 HTML / CSS
使用Html5多媒体实现微信语音功能
Jul 26 HTML / CSS
AmazeUI的下载配置与Helloworld的实现
Aug 19 HTML / CSS
css3实现的加载动画效果
Apr 07 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&amp;&amp;mysql)二
2006/10/09 PHP
SSI指令
2006/11/25 PHP
解析php中const与define的应用区别
2013/06/18 PHP
thinkphp5使用无限极分类
2019/02/18 PHP
javascript 清空form表单中某种元素的值
2009/12/26 Javascript
JavaScript中清空数组的三种方法分享
2011/04/07 Javascript
再谈javascript面向对象编程
2012/03/18 Javascript
用Javascript实现Windows任务管理器的代码
2012/03/27 Javascript
用按钮控制iframe显示的网页实现方法
2013/02/04 Javascript
JS实现清除指定cookies的方法
2014/09/20 Javascript
基于Bootstrap+jQuery.validate实现Form表单验证
2014/12/16 Javascript
JavaScript中原型和原型链详解
2015/02/11 Javascript
AngularJS中使用HTML5手机摄像头拍照
2016/02/22 Javascript
JavaScript tab选项卡插件实例代码
2016/02/23 Javascript
javascript验证香港身份证的格式或真实性
2017/02/07 Javascript
jQuery输入框密码的显示隐藏【代码分享】
2017/04/29 jQuery
如何在AngularJs中调用第三方插件库
2017/05/21 Javascript
Node.js简单入门前传
2017/08/21 Javascript
vue基于两个计算属性实现选中和全选功能示例
2019/02/08 Javascript
swiper Scrollbar滚动条组件详解
2019/09/08 Javascript
微信小程序用户盒子、宫格列表的实现
2020/07/01 Javascript
JS遍历树层级关系实现原理解析
2020/08/31 Javascript
[00:44]2016完美“圣”典 风云人物:Mikasa宣传片
2016/12/07 DOTA
详解Python程序与服务器连接的WSGI接口
2015/04/29 Python
浅谈python配置与使用OpenCV踩的一些坑
2018/04/02 Python
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
python使用pdfminer解析pdf文件的方法示例
2018/12/20 Python
Python在Matplotlib图中显示中文字体的操作方法
2019/07/29 Python
Django之PopUp的具体实现方法
2019/08/31 Python
Python socket聊天脚本代码实例
2020/01/02 Python
Python页面加载的等待方式总结
2021/02/28 Python
解决HTML5中的audio在手机端和微信端的不能自动播放问题
2019/11/04 HTML / CSS
旧时光糖果:Old Time Candy
2018/02/05 全球购物
新教师培训心得体会
2014/09/02 职场文书
高中生国庆节演讲稿范文2014
2014/09/21 职场文书
vue 数字翻牌器动态加载数据
2022/04/20 Vue.js