confetti.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var canvas;
  2. var ctx;
  3. var W;
  4. var H;
  5. var mp = 500; //max particles
  6. var particles = [];
  7. var angle = 0;
  8. var tiltAngle = 0;
  9. var confettiActive = true;
  10. var animationComplete = true;
  11. var deactivationTimerHandler;
  12. var reactivationTimerHandler;
  13. var animationHandler;
  14. var particleColors = {
  15. colorOptions: ["DodgerBlue", "OliveDrab", "Gold", "pink", "SlateBlue", "lightblue", "Violet", "PaleGreen", "SteelBlue", "SandyBrown", "Chocolate", "Crimson"],
  16. colorIndex: 0,
  17. colorIncrementer: 0,
  18. colorThreshold: 10,
  19. getColor: function () {
  20. if (this.colorIncrementer >= 10) {
  21. this.colorIncrementer = 0;
  22. this.colorIndex++;
  23. if (this.colorIndex >= this.colorOptions.length) {
  24. this.colorIndex = 0;
  25. }
  26. }
  27. this.colorIncrementer++;
  28. return this.colorOptions[this.colorIndex];
  29. }
  30. }
  31. $(window).resize(function () {
  32. W = window.innerWidth;
  33. H = window.innerHeight;
  34. canvas.width = W;
  35. canvas.height = H;
  36. });
  37. window.requestAnimFrame = (function () {
  38. return window.requestAnimationFrame ||
  39. window.webkitRequestAnimationFrame ||
  40. window.mozRequestAnimationFrame ||
  41. window.oRequestAnimationFrame ||
  42. window.msRequestAnimationFrame ||
  43. function (callback) {
  44. return window.setTimeout(callback, 1000 / 60);
  45. };
  46. })();
  47. function confettiParticle(color) {
  48. this.x = Math.random() * W; // x-coordinate
  49. this.y = (Math.random() * H) - H; //y-coordinate
  50. this.r = RandomFromTo(10, 30); //radius;
  51. this.d = (Math.random() * mp) + 10; //density;
  52. this.color = color;
  53. this.tilt = Math.floor(Math.random() * 10) - 10;
  54. this.tiltAngleIncremental = (Math.random() * 0.07) + .05;
  55. this.tiltAngle = 0;
  56. this.draw = function () {
  57. ctx.beginPath();
  58. ctx.lineWidth = this.r / 2;
  59. ctx.strokeStyle = this.color;
  60. ctx.moveTo(this.x + this.tilt + (this.r / 4), this.y);
  61. ctx.lineTo(this.x + this.tilt, this.y + this.tilt + (this.r / 4));
  62. return ctx.stroke();
  63. }
  64. }
  65. function SetGlobals() {
  66. canvas = document.getElementById("canvas");
  67. ctx = canvas.getContext("2d");
  68. W = window.innerWidth - 65; //AJUSTE para cubrir el ancho
  69. H = window.innerHeight * 5; //AJUSTE para cubrir la hoja
  70. canvas.width = W;
  71. canvas.height = H;
  72. }
  73. function InitializeConfetti() {
  74. particles = [];
  75. animationComplete = false;
  76. for (var i = 0; i < mp; i++) {
  77. var particleColor = particleColors.getColor();
  78. particles.push(new confettiParticle(particleColor));
  79. }
  80. StartConfetti();
  81. }
  82. function Draw() {
  83. ctx.clearRect(0, 0, W, H);
  84. var results = [];
  85. for (var i = 0; i < mp; i++) {
  86. (function (j) {
  87. results.push(particles[j].draw());
  88. })(i);
  89. }
  90. Update();
  91. return results;
  92. }
  93. function RandomFromTo(from, to) {
  94. return Math.floor(Math.random() * (to - from + 1) + from);
  95. }
  96. function Update() {
  97. var remainingFlakes = 0;
  98. var particle;
  99. angle += 0.01;
  100. tiltAngle += 0.1;
  101. for (var i = 0; i < mp; i++) {
  102. particle = particles[i];
  103. if (animationComplete) return;
  104. if (!confettiActive && particle.y < -15) {
  105. particle.y = H + 100;
  106. continue;
  107. }
  108. stepParticle(particle, i);
  109. if (particle.y <= H) {
  110. remainingFlakes++;
  111. }
  112. CheckForReposition(particle, i);
  113. }
  114. if (remainingFlakes === 0) {
  115. StopConfetti();
  116. }
  117. }
  118. function CheckForReposition(particle, index) {
  119. if ((particle.x > W + 20 || particle.x < -20 || particle.y > H) && confettiActive) {
  120. if (index % 5 > 0 || index % 2 == 0) //66.67% of the flakes
  121. {
  122. repositionParticle(particle, Math.random() * W, -10, Math.floor(Math.random() * 10) - 20);
  123. } else {
  124. if (Math.sin(angle) > 0) {
  125. //Enter from the left
  126. repositionParticle(particle, -20, Math.random() * H, Math.floor(Math.random() * 10) - 20);
  127. } else {
  128. //Enter from the right
  129. repositionParticle(particle, W + 20, Math.random() * H, Math.floor(Math.random() * 10) - 20);
  130. }
  131. }
  132. }
  133. }
  134. function stepParticle(particle, particleIndex) {
  135. particle.tiltAngle += particle.tiltAngleIncremental;
  136. particle.y += (Math.cos(angle + particle.d) + 3 + particle.r / 2) / 2;
  137. particle.x += Math.sin(angle);
  138. particle.tilt = (Math.sin(particle.tiltAngle - (particleIndex / 3))) * 15;
  139. }
  140. function repositionParticle(particle, xCoordinate, yCoordinate, tilt) {
  141. particle.x = xCoordinate;
  142. particle.y = yCoordinate;
  143. particle.tilt = tilt;
  144. }
  145. function StartConfetti() {
  146. W = window.innerWidth - 65; //AJUSTE para cubrir el ancho
  147. H = window.innerHeight * 3; //AJUSTE para cubrir la hoja
  148. canvas.width = W;
  149. canvas.height = H;
  150. (function animloop() {
  151. if (animationComplete) return null;
  152. animationHandler = requestAnimFrame(animloop);
  153. return Draw();
  154. })();
  155. }
  156. function ClearTimers() {
  157. clearTimeout(reactivationTimerHandler);
  158. clearTimeout(animationHandler);
  159. }
  160. function DeactivateConfetti() {
  161. confettiActive = false;
  162. ClearTimers();
  163. }
  164. function StopConfetti() {
  165. animationComplete = true;
  166. if (ctx == undefined) return;
  167. ctx.clearRect(0, 0, W, H);
  168. }
  169. function RestartConfetti() {
  170. ClearTimers();
  171. StopConfetti();
  172. reactivationTimerHandler = setTimeout(function () {
  173. confettiActive = true;
  174. animationComplete = false;
  175. InitializeConfetti();
  176. }, 100);
  177. }