How to Create fireworks with HTML5 _ html5 tutorial skills-

Source: Internet
Author: User
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

  1. Var Boom = function (x, r, c, boomArea, shape) {// Fireworks object
  2. This. booms = [];
  3. This. x = x;
  4. This. y = (canvas. height + r );
  5. This. r = r;
  6. This. c = c;
  7. This. shape = shape | false;
  8. This. boomArea = boomArea;
  9. This. theta = 0;
  10. This. dead = false;
  11. This. ba = parseInt (getRandom (80,200 ));
  12. }
  13. Boom. prototype = {
  14. _ Paint: function (){
  15. Ctx. save ();
  16. Ctx. beginPath ();
  17. Ctx. arc (this. x, this. y, this. r, 0, 2 * Math. PI );
  18. Ctx. fillStyle = this. c;
  19. Ctx. fill ();
  20. Ctx. restore ();
  21. },
  22. _ Move: function (){
  23. Var dx = this. boomArea. x-this. x, dy = this. boomArea. y-this. y;
  24. Thisthis. x = this. x + dx x 0.01;
  25. Thisthis. y = this. y + dy * 0.01;
  26. If (Math. abs (dx) <= this. ba & Math. abs (dy) <= this. ba ){
  27. If (this. shape ){
  28. This. _ shapBoom ();
  29. }
  30. Else this. _ boom ();
  31. This. dead = true;
  32. }
  33. Else {
  34. This. _ paint ();
  35. }
  36. },
  37. _ DrawLight: function (){
  38. Ctx. save ();
  39. Ctx. fillStyle = "rgba (255,228,150, 0.3 )";
  40. Ctx. beginPath ();
  41. Ctx. arc (this. x, this. y, this. r + 3 * Math. random () + 1, 0, 2 * Math. PI );
  42. Ctx. fill ();
  43. Ctx. restore ();
  44. },
  45. _ Boom: function () {// normal explosion
  46. Var fragNum = getRandom (30,200 );
  47. Var style = getRandom (0, 10)> = 5? 1: 2;
  48. Var color;
  49. If (style = 1 ){
  50. Color = {
  51. A: parseInt (getRandom (128,255 )),
  52. B: parseInt (getRandom (128,255 )),
  53. C: parseInt (getRandom (128,255 ))
  54. }
  55. }
  56. Var fanwei = parseInt (getRandom (300,400 ));
  57. For (var I = 0; I < li>
  58. If (style = 2 ){
  59. Color = {
  60. A: parseInt (getRandom (128,255 )),
  61. B: parseInt (getRandom (128,255 )),
  62. C: parseInt (getRandom (128,255 ))
  63. }
  64. }
  65. Var a = getRandom (-Math. PI, Math. PI );
  66. Var x = getRandom (0, fanwei) * Math. cos (a) + this. x;
  67. Var y = getRandom (0, fanwei) * Math. sin (a) + this. y;
  68. Var radius = getRandom (0, 2)
  69. Var frag = new Frag (this. x, this. y, radius, color, x, y );
  70. This. booms. push (frag );
  71. }
  72. },
  73. _ ShapBoom: function () {// shaped explosion
  74. Var that = this;
  75. PutValue (ocas, octx, this. shape, 5, function (dots ){
  76. Var dx = canvas. width/2-that.x;
  77. Var dy = canvas. height/2-that.y;
  78. For (var I = 0; I < li>
  79. Color = {a: dots [I]. a, B: dots [I]. B, c: dots [I]. c}
  80. Var x = dots [I]. x;
  81. Var y = dots [I]. y;
  82. Var radius = 1;
  83. Var frag = new Frag (that. x, that. y, radius, color, x-dx, y-dy );
  84. That. booms. push (frag );
  85. }
  86. })
  87. }
  88. }


[Debris]

Copy XML/HTML Code to clipboard

  1. Var Frag = function (centerX, centerY, radius, color, tx, ty) {// firefresh object
  2. This. tx = tx;
  3. This. ty = ty;
  4. This. x = centerX;
  5. This. y = centerY;
  6. This. dead = false;
  7. This. centerX = centerX;
  8. This. centerY = centerY;
  9. This. radius = radius;
  10. This. color = color;
  11. }
  12. Frag. prototype = {
  13. Paint: function (){
  14. Ctx. save ();
  15. Ctx. beginPath ();
  16. Ctx. arc (this. x, this. y, this. radius, 0, 2 * Math. PI );
  17. Ctx. fillStyle = "rgba (" + this. color. a + "," + this. color. B + "," + this. color. c + ", 1 )";
  18. Ctx. fill ()
  19. Ctx. restore ();
  20. },
  21. MoveTo: function (index ){
  22. Thisthis. ty = this. ty + 0.3;
  23. Var dx = this. tx-this. x, dy = this. ty-this. y;
  24. This. x = Math. abs (dx) <0.1? This. tx: (this. x + dx* 0.1 );
  25. This. y = Math. abs (dy) <0.1? This. ty: (this. y + dy * 0.1 );
  26. If (dx = 0 & Math. abs (dy) <= 80 ){
  27. This. dead = true;
  28. }
  29. This. paint ();
  30. }
  31. }


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

  1. Ctx. save ();
  2. Ctx. fillStyle = "rgba (0.1, 24 )";
  3. Ctx. fillRect (0, 0, canvas. width, canvas. height );
  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.