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将你的设计带入下个高度
Aug 08 HTML / CSS
CSS3制作半透明边框(Facebox)类似渐变
Dec 09 HTML / CSS
CSS3中的@keyframes关键帧动画的选择器绑定
Jun 13 HTML / CSS
纯CSS3大转盘抽奖示例代码(响应式、可配置)
Jan 13 HTML / CSS
css3遮罩层镂空效果的多种实现方法
May 11 HTML / CSS
html5使用canvas实现图片下载功能的示例代码
Aug 26 HTML / CSS
HTML5边玩边学(2)基础绘图实现方法
Sep 21 HTML / CSS
用html5的canvas画布绘制贝塞尔曲线完整代码
Aug 14 HTML / CSS
怎样实现H5+CSS3手指滑动切换图片的示例代码
May 05 HTML / CSS
利用 Canvas实现绘画一个未闭合的带进度条的圆环
Jul 26 HTML / CSS
HTML5触摸事件(touchstart、touchmove和touchend)的实现
May 08 HTML / CSS
CSS中妙用 drop-shadow 实现线条光影效果
Nov 11 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
如何在Ubuntu下启动Apache的Rewrite功能
2013/07/05 PHP
php根据操作系统转换文件名大小写的方法
2014/02/24 PHP
php中eval函数的危害与正确禁用方法
2014/06/30 PHP
Laravel4中的Validator验证扩展用法详解
2016/07/26 PHP
PHP的mysqli_select_db()函数讲解
2019/01/23 PHP
js+CSS 图片等比缩小并垂直居中实现代码
2008/12/01 Javascript
js判断IE6/IE7/FF的代码[XMLHttpRequest]
2011/02/16 Javascript
jquery动态改变form属性提交表单
2014/06/03 Javascript
jquery中添加属性和删除属性
2015/06/03 Javascript
javascript检查浏览器是否已经启用XX功能
2015/07/10 Javascript
异步JS框架的作用以及实现方法
2015/10/29 Javascript
分享一道关于闭包、bind和this的面试题
2017/02/20 Javascript
手把手搭建安装基于windows的Vue.js运行环境
2017/06/12 Javascript
Angular排序实例详解
2017/06/28 Javascript
React Native之TextInput组件解析示例
2017/08/22 Javascript
基于js 字符串indexof与search方法的区别(详解)
2017/12/04 Javascript
js循环map 获取所有的key和value的实现代码(json)
2018/05/09 Javascript
Vue 无限滚动加载指令实现方法
2019/05/28 Javascript
javascript如何实现create方法
2019/11/04 Javascript
快速解决vue2+vue-cli3项目ie兼容的问题
2020/11/17 Vue.js
Python中模块pymysql查询结果后如何获取字段列表
2017/06/05 Python
python图像常规操作
2017/11/11 Python
pyqt5之将textBrowser的内容写入txt文档的方法
2019/06/21 Python
关于torch.optim的灵活使用详解(包括重写SGD,加上L1正则)
2020/02/20 Python
如何解决flask修改静态资源后缓存文件不能及时更改问题
2020/08/02 Python
matplotlib之属性组合包(cycler)的使用
2021/02/24 Python
巴西最大的体育用品商城:Netshoes巴西
2016/11/29 全球购物
人力资源总监工作说明
2014/03/03 职场文书
2014年国培研修感言
2014/03/09 职场文书
大班上学期幼儿评语
2014/04/30 职场文书
公证委托书标准格式
2014/09/11 职场文书
小区的门卫岗位职责
2014/10/01 职场文书
年底个人总结范文
2015/03/10 职场文书
2015年社区矫正工作总结
2015/04/21 职场文书
MySQL的存储函数与存储过程的区别解析
2022/04/08 MySQL
GPU服务器的多用户配置方法
2022/07/07 Servers