要过年了,过年想起的便是放烟花啦。。。。因而就用canvas写了个放烟花的实际效果,电脑鼠标点一下也会造成烟花,但是不必造成太多烟花哦,1个烟花散出的颗粒是30到200个之间,当网页页面上的颗粒数量做到1定的情况下,网页页面就会很卡咯,我也没特地去提升神马的。之后有时间再说吧。
立即上DEMO吧:放烟花
基本原理很简易。。。就写1个烟花类和碎屑类,案例化后让它飞起来,随后抵达某个点后,把这个烟花目标的dead特性置为true,随后再案例化出1定数量的碎屑目标,而且给予碎屑目标任意1个要抵达的总体目标点,随后让全部碎屑目标飞以往就可以了。
【烟花】
XML/HTML Code拷贝內容到剪贴板
- var Boom = function(x,r,c,boomArea,shape){ //烟花目标
- this.booms = [];
- this.x = x;
- this.y = (canvas.height+r);
- this.r = r;
- this.c = c;
- this.shape = shape || false;
- this.boomArea = boomArea;
- this.theta = 0;
- this.dead = false;
- this.ba = parseInt(getRandom(80 , 200));
- }
- Boom.prototype = {
- _paint:function(){
- ctx.save();
- ctx.beginPath();
- ctx.arc(this.x,this.y,this.r,0,2*Math.PI);
- ctx.fillStyle = this.c;
- ctx.fill();
- ctx.restore();
- },
- _move:function(){
- var dx = this.boomArea.x - this.x , dy = this.boomArea.y - this.y;
- thisthis.x = this.x+dx*0.01;
- thisthis.y = this.y+dy*0.01;
-
- if(Math.abs(dx)<=this.ba && Math.abs(dy)<=this.ba){
- if(this.shape){
- this._shapBoom();
- }
- else this._boom();
- this.dead = true;
- }
- else {
- this._paint();
- }
- },
- _drawLight:function(){
- ctx.save();
- ctx.fillStyle = "rgba(255,228,150,0.3)";
- ctx.beginPath();
- ctx.arc(this.x , this.y , this.r+3*Math.random()+1 , 0 , 2*Math.PI);
- ctx.fill();
- ctx.restore();
- },
- _boom:function(){ //一般发生爆炸
- var fragNum = getRandom(30 , 200);
- var style = getRandom(0,10)>=5? 1 : 2;
- var color;
- if(style===1){
- color = {
- a:parseInt(getRandom(128,255)),
- b:parseInt(getRandom(128,255)),
- c:parseInt(getRandom(128,255))
- }
- }
-
- var fanwei = parseInt(getRandom(300, 400));
- for(var i=0;i<fragNum;i++){
- if(style===2){
- color = {
- a:parseInt(getRandom(128,255)),
- b:parseInt(getRandom(128,255)),
- c:parseInt(getRandom(128,255))
- }
- }
- var a = getRandom(-Math.PI, Math.PI);
- var x = getRandom(0, fanwei) * Math.cos(a) + this.x;
- var y = getRandom(0, fanwei) * Math.sin(a) + this.y;
- var radius = getRandom(0 , 2)
- var frag = new Frag(this.x , this.y , radius , color , x , y );
- this.booms.push(frag);
- }
- },
- _shapBoom:function(){ //有样子的发生爆炸
- var that = this;
- putValue(ocas , octx , this.shape , 5, function(dots){
- var dx = canvas.width/2-that.x;
- var dy = canvas.height/2-that.y;
- for(var i=0;i<dots.length;i++){
- color = {a:dots[i].a,b:dots[i].b,c:dots[i].c}
- var x = dots[i].x;
- var y = dots[i].y;
- var radius = 1;
- var frag = new Frag(that.x , that.y , radius , color , x-dx , y-dy);
- that.booms.push(frag);
- }
- })
- }
- }
【碎屑】
XML/HTML Code拷贝內容到剪贴板
- var Frag = function(centerX , centerY , radius , color ,tx , ty){ //烟花碎屑目标
- this.tx = tx;
- this.ty = ty;
- this.x = centerX;
- this.y = centerY;
- this.dead = false;
- this.centerX = centerX;
- this.centerY = centerY;
- this.radius = radius;
- this.color = color;
- }
-
- Frag.prototype = {
- paint:function(){
- ctx.save();
- ctx.beginPath();
- ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
- ctx.fillStyle = "rgba("+this.color.a+","+this.color.b+","+this.color.c+",1)";
- ctx.fill()
- ctx.restore();
- },
- moveTo:function(index){
- thisthis.ty = this.ty+0.3;
- var dx = this.tx - this.x , dy = this.ty - this.y;
- this.x = Math.abs(dx)<0.1 ? this.tx : (this.x+dx*0.1);
- this.y = Math.abs(dy)<0.1 ? this.ty : (this.y+dy*0.1);
- if(dx===0 && Math.abs(dy)<=80){
- this.dead = true;
- }
- this.paint();
- }
- }
让碎屑造成虚影也很简易,便是每次更新画布时,并不是擦掉重绘,而是绘图全透明度为0.1(假如想虚影更长,能够把这个值弄的更小)的情况色调。随后虚影便可以做出来了。也便是:
XML/HTML Code拷贝內容到剪贴板
- ctx.save();
- ctx.fillStyle = "rgba(0,5,24,0.1)";
- ctx.fillRect(0,0,canvas.width,canvas.height);
- ctx.restore();
让烟花产生自身要想的样子,例如字体样式,照片之类的,也很简易,便是能够根据离屏canvas和canvas的getImageData这个方式便可以做出来。离屏canvas,说白了便是离去显示屏的,也便是不能见的canvas,立即在js里边用document.createElement("canvas")便可以转化成1个canvas dom目标了,要是不把这个dom目标赋给body,这个canvas目标就非常于1个离屏目标了,大家便可以获得到这个离屏canvas的context目标,随后再客户看不见的地区做任何大家想做的事儿了。
让烟花产生自身要想的样子便是先把文本或照片画在离屏canvas上,随后用getImageData获得画布上的像素数字能量数组,随后遍历数字能量数组,获得有色调的像素,也便是大家要想的內容,储存起来后,再放到主canvas目标中显示信息出来。
getImageData的像素解决我以前的blog上有讲过,假如不容易用的,请戳:随意谈谈用canvas来完成文本照片颗粒化
源代码详细地址:https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Funny-demo/shotFire