This article mainly describes how to use HTML5 to create fireworks. It mainly uses CanvasAPI. If you need it, you can refer to the Chinese New Year. The New Year's Eve is about fireworks .... So I wrote a fireshow effect using canvas, and clicking the mouse will also generate fireworks, but do not generate too many fireworks. A fireworks emits between 30 and 200 particles, when the number of particles on the page reaches a certain level, the page will be very slow, and I did not specifically optimize Shenma. Let's talk about it later.
Go directly to the DEMO: Fireworks
The principle is very simple... Write a fireworks class and debris class, instantiate it, let it fly up, and then reach a certain point, set the dead attribute of the Fireworks object to true, then, a certain number of debris objects are generated, and a random target point to be reached is given to the debris object. Then, all the debris objects can fly over.
Fireworks]
Copy XML/HTML Code to clipboard
- Var Boom = function (x, r, c, boomArea, shape) {// Fireworks object
- 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 x 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 () {// normal explosion
- 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 < li>
- 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 () {// shaped explosion
- 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 < li>
- 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 );
- }
- })
- }
- }
[Debris]
Copy XML/HTML Code to clipboard
- Var Frag = function (centerX, centerY, radius, color, tx, ty) {// firefresh object
- 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 ();
- }
- }
It is also very easy to make debris generate a virtual shadow, that is, each time you refresh the canvas, it is not to erase the re-painting, but to draw a transparency of 0.1 (if you want to make the virtual shadow longer, you can make this value smaller) background color. Then the virtual shadows can be made. That is:
Copy XML/HTML Code to clipboard
- Ctx. save ();
- Ctx. fillStyle = "rgba (0.1, 24 )";
- Ctx. fillRect (0, 0, canvas. width, canvas. height );
- Ctx. restore ();
Let fireworks form the desired shape, such as fonts and images. That is, you can use the off-screen canvas and canvas getImageData method. The canvas that leaves the screen, that is, the invisible canvas, uses document directly in js. createElement ("canvas") can generate a canvas dom object. As long as this dom object is not assigned to the body, this canvas object is equivalent to an off-screen object, we can get the context object of the canvas on the off-screen, and then do anything we want without seeing it.
To make fireworks form the desired shape, you need to first draw text or images on the off-screen canvas, then use getImageData to obtain the pixel array on the canvas, and then traverse the array to obtain the colored pixels, that is, the content we want is saved and displayed in the main canvas object.
GetImageData pixel processing I have mentioned in my previous blog. If it won't be used, stamp it: Let's talk about using canvas to particle up text images.
Source Code address: https://github.com/whxaxes/canvas-test/tree/gh-pages/src/Funny-demo/shotFire