1、效果
2、代码解析(1)requestanimationframerequestanimationframe是浏览器用于定时循环操作的一个接口,类似于settimeout,主要用途是按帧对网页进行重绘。
设置这个api的目的是为了让各种网页动画效果(dom动画、canvas动画、svg动画、webgl动画)能够有一个统一的刷新机制,从而节省系统资源,提高系统性能,改善视觉效果。代码中使用这个api,就是告诉浏览器希望执行一个动画,让浏览器在下一个动画帧安排一次网页重绘。
requestanimationframe的优势,在于充分利用显示器的刷新机制,比较节省系统资源。显示器有固定的刷新频率(60hz或75hz),也就是说,每秒最多只能重绘60次或75次,requestanimationframe的基本思想就是与这个刷新频率保持同步,利用这个刷新频率进行页面重绘。此外,使用这个api,一旦页面不处于浏览器的当前标签,就会自动停止刷新。这就节省了cpu、gpu和电力。
不过有一点需要注意,requestanimationframe是在主线程上完成。这意味着,如果主线程非常繁忙,requestanimationframe的动画效果会大打折扣。
requestanimationframe使用一个回调函数作为参数。这个回调函数会在浏览器重绘之前调用。
requestid = window.requestanimationframe(callback);
目前,高版本浏览器firefox 23 / ie 10 / chrome / safari)都支持这个方法。可以用下面的方法,检查浏览器是否支持这个api。如果不支持,则自行模拟部署该方法。
window.requestanimframe = (function(){
return window.requestanimationframe ||
window.webkitrequestanimationframe ||
window.mozrequestanimationframe ||
window.orequestanimationframe ||
window.msrequestanimationframe ||
function( callback ){
window.settimeout(callback, 1000 / 60);
};
})();
上面的代码按照1秒钟60次(大约每16.7毫秒一次),来模拟requestanimationframe。
使用requestanimationframe的时候,只需反复调用它即可。
function repeatoften() { // do whatever requestanimationframe(repeatoften);
}
requestanimationframe(repeatoften);
取消重绘可以用 cancelanimationframe。
window.cancelanimationframe(requestid);
它的参数是requestanimationframe返回的一个代表任务id的整数值。
(2)准备画版(canvas)判断浏览器是否支持canvas,并把宽高设置为浏览器窗口大小。
var canvas = document.getelementbyid("mycanvas");
if (!canvas.getcontext) { return;
}
canvas.width = window.innerwidth;
canvas.height = window.innerheight;var ctx = canvas.getcontext("2d");
(3)烟花对象(firework)烟花效果可以简单地认为是围绕一个点,爆炸产生很多小球向边上扩散。因此需要一个烟花对象,这个对象主要是记录烟花绽放的位置和周围小球的信息。所以我们的烟花对象定义如下。
function firework() {
this.x = -1;
this.y = -1;
this.balls = [];
}
那这个对象应该有哪些方法呢?
首先,要创建爆炸产生的小球。
createballs: function () {
for (var i = 0; i < 300; i++) {
var angle = math.random() * math.pi * 2,
radius = getrandom(50, 200);
this.balls.push(new ball(fwx, fwy, fwx + math.cos(angle) * radius, fwy + math.sin(angle) * radius));
}
}
注:这里fwx为烟花位置x轴坐标,fwy为烟花位置y轴坐标,下同。
这里小球的运行长度为 50 到200 的随机值,小球运行轨迹起点为烟花位置,终点在一个圆上随机的一点。
然后,要对烟花进行初始化,主要是确定位置,产生小球。
init: function () {
this.x = getrandom(200, width - 200);
this.y = getrandom(200, height - 200);
fwx = this.x;
fwy = this.y;
this.createballs();
drawcount = 0;
currballindex = 0;
}
注:这里drawcount为绘制次数,currballindex为当前绘制的小球索引。
整个firework定义如下。
function firework() {
this.x = -1;
this.y = -1;
this.balls = [];
}
firework.prototype = {
init: function () {
this.x = getrandom(200, width - 200);
this.y = getrandom(200, height - 200);
fwx = this.x;
fwy = this.y;
this.createballs();
drawcount = 0;
currballindex = 0;
},
run: function () {
this.init();
},
createballs: function () {
for (var i = 0; i < 300; i++) {
var angle = math.random() * math.pi * 2,
radius = getrandom(50, 200);
this.balls.push(new ball(fwx, fwy, fwx + math.cos(angle) * radius, fwy + math.sin(angle) * radius));
}
}
}
(4)爆炸产生的小球对象(ball)小球需要知道自己的起点和终点的位置,所以定义如下。
function ball(bx, by, ex, ey) { this.bx = bx;//起点x轴坐标
this.by = by;//起点y轴坐标
this.ex = ex;//终点x轴坐标
this.ey = ey;//终点y轴坐标}
小球还要能根据当前绘制的次数和总绘制次数计算得到当前坐标和下一次绘制坐标,这两个坐标连接起来的直线就是本次要绘制的内容,所以定义如下。
ball.prototype = {
getspan: function () {
var xspan = (this.ex - this.bx) / alldrawcount,
yspan = (this.ey - this.by) / alldrawcount;
return {
x: xspan,
y: yspan
};
},
currposition: function () {
var span = this.getspan(),
currx = -1,
curry = -1;
if (drawcount < alldrawcount) {
currx = this.bx + span.x * (drawcount - 1);
curry = this.by + span.y * (drawcount - 1);
return {
x: currx,
y: curry
};
} return null;
},
nextposition: function () {
var span = this.getspan(),
currx = -1,
curry = -1;
if (drawcount < alldrawcount) {
currx = this.bx + span.x * drawcount;
curry = this.by + span.y * drawcount;
return {
x: currx,
y: curry
};
} return null;
}
}
(5)全局变量及工具方法var fwx = -1, //烟花位置x轴坐标
fwy = -1, //烟花位置y轴坐标
currfw = null, //烟花实例
currballindex = -1, //当前正在绘制的小球索引
drawcount = 0, //绘制次数
alldrawcount = 40, //总共需要的绘制次数
width = canvas.width, //画布宽度
height = canvas.height; //画布高度
然后还要几个工具方法。
function componenttohex(c) {
var hex = c.tostring(16);
return hex.length == 1 ? "0" + hex : hex;
}function rgbtohex(r, g, b) {
return "#" + componenttohex(r) + componenttohex(g) + componenttohex(b);
}function getrandom(minnum, maxnum) {
var ichoices = maxnum - minnum + 1;
return math.floor(math.random() * ichoices + minnum);
}
(6)绘制方法最后还剩一个供 requestanimationframe 调用的绘制方法。这个绘制方法就是根据当前的绘制次数,拿到爆炸小球的路径(包含起点和终点的一条线段),然后把上一次绘制的路径擦除。
当一个烟花的效果绘制完成后,进行下一个烟花的绘制。
function drawline(span) {
if (currfw && currballindex !== -1) {
if (drawcount <= alldrawcount) {
ctx.save();
drawcount++;
for (var i = 0, j = currfw.balls.length; i < j; i++) {
var currball = currfw.balls[i],
beginpoint = currball.currposition(),
endpoint = currball.nextposition();
if (beginpoint && endpoint) {
console.log(currballindex, drawcount, currball, beginpoint, endpoint);
ctx.beginpath();
ctx.moveto(currball.bx, currball.by);
ctx.lineto(beginpoint.x, beginpoint.y);
ctx.strokestyle = "#000";
ctx.stroke();
ctx.beginpath();
ctx.moveto(beginpoint.x, beginpoint.y);
ctx.lineto(endpoint.x, endpoint.y);
var r = getrandom(0, 255);
var g = getrandom(0, 255);
var b = getrandom(0, 255);
ctx.strokestyle = rgbtohex(r, g, b);
ctx.stroke();
} else {
ctx.beginpath();
ctx.moveto(currball.bx, currball.by);
ctx.lineto(currball.ex, currball.ey);
ctx.strokestyle = "#000";
ctx.stroke();
}
}
currballindex++;
currballindex %= currfw.balls.length;
ctx.restore();
} else {
ctx.clearrect(0, 0, width, height);
currfw = new firework();
currfw.run();
}
}
requestanimationframe(drawline);
}
这里颜色取的是随机值。
(7)启动绘制最后就是启动绘制。
currfw = new firework();
currfw.run();
requestanimationframe(drawline);
(8)全部代码。全部代码如下,共160行。
1 (function () {
2 var requestanimationframe = window.requestanimationframe ||
window.mozrequestanimationframe ||
window.webkitrequestanimationframe ||
window.msrequestanimationframe ||
function (callback) {
3 return window.settimeout(callback, 1000 / 60);
4 };
5 window.requestanimationframe = requestanimationframe;
6 })();
7
8 var canvas = document.getelementbyid("mycanvas");
9 if (!canvas.getcontext) {
10 return;
11 }
12 canvas.width = window.innerwidth;
13 canvas.height = window.innerheight;
14
15 var ctx = canvas.getcontext("2d");
16
17 var fwx = -1,
18 fwy = -1,
19 currfw = null,
20 currballindex = -1,
21 drawcount = 0,
22 alldrawcount = 40,
23 width = canvas.width,
24 height = canvas.height;
25
26 function componenttohex(c) {
27 var hex = c.tostring(16);
28 return hex.length == 1 ? "0" + hex : hex;
29 }
30
31 function rgbtohex(r, g, b) {
32 return "#" + componenttohex(r) + componenttohex(g) + componenttohex(b);
33 }
34
35 function getrandom(minnum, maxnum) {
36 var ichoices = maxnum - minnum + 1;
37 return math.floor(math.random() * ichoices + minnum);
38 }
39
40 function drawline(span) {
41 if (currfw && currballindex !== -1) {
42 if (drawcount <= alldrawcount) {
43 ctx.save();
44 drawcount++;
45 for (var i = 0, j = currfw.balls.length; i < j; i++) {
46 var currball = currfw.balls[i],
47 beginpoint = currball.currposition(),
48 endpoint = currball.nextposition();
49 if (beginpoint && endpoint) {
50 console.log(currballindex, drawcount, currball, beginpoint, endpoint);
51 ctx.beginpath();
52 ctx.moveto(currball.bx, currball.by);
53 ctx.lineto(beginpoint.x, beginpoint.y);
54 ctx.strokestyle = "#000";
55 ctx.stroke();
56 ctx.beginpath();
57 ctx.moveto(beginpoint.x, beginpoint.y);
58 ctx.lineto(endpoint.x, endpoint.y);
59 var r = getrandom(0, 255);
60 var g = getrandom(0, 255);
61 var b = getrandom(0, 255);
62 ctx.strokestyle = rgbtohex(r, g, b);
63 ctx.stroke();
64 } else {
65 ctx.beginpath();
66 ctx.moveto(currball.bx, currball.by);
67 ctx.lineto(currball.ex, currball.ey);
68 ctx.strokestyle = "#000";
69 ctx.stroke();
70 }
71 }
72 currballindex++;
73 currballindex %= currfw.balls.length;
74 ctx.restore();
75 } else {
76 ctx.clearrect(0, 0, width, height);
77 currfw = new firework();
78 currfw.run();
79 }
80 }
81 requestanimationframe(drawline);
82 }
83
84 function firework() {
85 this.x = -1;
86 this.y = -1;
87 this.balls = [];
88 }
89
90 firework.prototype = {
91 init: function () {
92 this.x = getrandom(200, width - 200);
93 this.y = getrandom(200, height - 200);
94 fwx = this.x;
95 fwy = this.y;
96 this.createballs();
97 drawcount = 0;
98 currballindex = 0;
99 },
100 run: function () {
101 this.init();
102 },
103 createballs: function () {
104 for (var i = 0; i < 300; i++) {
105 var angle = math.random() * math.pi * 2,
106 radius = getrandom(50, 200);
107 this.balls.push(new ball(fwx, fwy, fwx + math.cos(angle) * radius, fwy + math.sin(angle) * radius));
108 }
109 }
110 }
111
112 function ball(bx, by, ex, ey) {
113 this.bx = bx;
114 this.by = by;
115 this.ex = ex;
116 this.ey = ey;
117 }
118
119 ball.prototype = {
120 getspan: function () {
121 var xspan = (this.ex - this.bx) / alldrawcount,
122 yspan = (this.ey - this.by) / alldrawcount;
123 return {
124 x: xspan,
125 y: yspan
126 };
127 },
128 currposition: function () {
129 var span = this.getspan(),
130 currx = -1,
131 curry = -1;
132 if (drawcount < alldrawcount) {
133 currx = this.bx + span.x * (drawcount - 1);
134 curry = this.by + span.y * (drawcount - 1);
135 return {
136 x: currx,
137 y: curry
138 };
139 }
140 return null;
141 },
142 nextposition: function () {
143 var span = this.getspan(),
144 currx = -1,
145 curry = -1;
146 if (drawcount < alldrawcount) {
147 currx = this.bx + span.x * drawcount;
148 curry = this.by + span.y * drawcount;
149 return {
150 x: currx,
151 y: curry
152 };
153 }
154 return null;
155 }
156 }
157
158 currfw = new firework();
159 currfw.run();
160 requestanimationframe(drawline);
欢迎讨论。
以上就是html5 canvas实战之实现烟花效果的代码案例的详细内容。