Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. 'use strict'
  2. //constants
  3. const { PI: π, sin, cos, floor, round } = Math
  4. const g = 0.2
  5. //variables
  6. let c, ctx, W, H
  7. //animation variables
  8. let fc = 0 //frame count
  9. let pd = 2 //pixel density
  10. let paused = false
  11. let lastTimeCalled, fps
  12. let btn
  13. let fireworks = []
  14. let explParticles = []
  15.  
  16. //functions
  17. class Particle {
  18. constructor() {
  19. this.x = random(W)
  20. this.y = H
  21. this.r = 4
  22.  
  23. this.dx = random(-4, 4)
  24. this.dy = random(-20, -12)
  25.  
  26. this.red = round(random(255))
  27. this.green = round(random(255))
  28. this.blue = round(random(255))
  29. this.a = 1
  30. }
  31.  
  32. update() {
  33. //update speed
  34. this.dy += g
  35. //update position
  36. this.x += this.dx
  37. this.y += this.dy
  38. }
  39.  
  40. draw() {
  41. //only draw particles inside the screen
  42. if (0 < this.x && this.x < W && 0 < this.y && this.y < H) {
  43. ctx.beginPath()
  44. ctx.arc(this.x, this.y, this.r, 0, 2 * π)
  45. ctx.fillStyle =
  46. 'rgba(' +
  47. this.red +
  48. ',' +
  49. this.green +
  50. ',' +
  51. this.blue +
  52. ',' +
  53. this.a +
  54. ')'
  55. ctx.fill()
  56. }
  57. }
  58.  
  59. stoped() {
  60. return this.dy >= 0
  61. }
  62.  
  63. explode() {
  64. for (let i = 0; i <= round(random(75, 100)); i++) {
  65. explParticles.push(
  66. new ExplParticle(this.x, this.y, this.red, this.green, this.blue)
  67. )
  68. }
  69. }
  70. }
  71.  
  72. class ExplParticle extends Particle {
  73. constructor(x, y, red, green, blue) {
  74. super()
  75. this.x = x
  76. this.y = y
  77. this.r = random(1, 2)
  78.  
  79. this.dx = random(25) * sin(random(0, 2 * π))
  80. this.dy = random(25) * sin(random(0, 2 * π))
  81.  
  82. this.red = red
  83. this.green = green
  84. this.blue = blue
  85. this.a = 1
  86.  
  87. this.da = random(0.012, 0.015)
  88. }
  89.  
  90. update() {
  91. //air friction
  92. this.dx *= 0.85
  93. this.dy *= 0.85
  94. //update speed
  95. this.dy += g
  96. //update position
  97. this.x += this.dx
  98. this.y += this.dy
  99. //decrease opacity
  100. this.a -= this.da
  101. }
  102.  
  103. faded() {
  104. return this.a < 0
  105. }
  106. }
  107.  
  108. window.onload = () => {
  109. //timestamp
  110. lastTimeCalled = Date.now()
  111. fps = document.getElementById('Fps')
  112. //canvas setup
  113. c = document.getElementById('Canvas')
  114. ctx = c.getContext('2d')
  115. //canvas --> full screen
  116. setSize(window.innerWidth, window.innerHeight, pd)
  117. //add event listeners
  118. c.addEventListener('click', sp)
  119. c.addEventListener('dblclick', clearAll)
  120. btn = document.getElementById('Info')
  121. btn.addEventListener('click', info)
  122.  
  123. //begin animation
  124. requestAnimationFrame(animate)
  125. }
  126.  
  127. function animate(timestamp) {
  128. calcFPS()
  129. clear()
  130.  
  131. if (random() < 0.06 && explParticles.length < 500) {
  132. fireworks.push(new Particle())
  133. }
  134.  
  135. for (let i = fireworks.length - 1; i >= 0; i--) {
  136. fireworks[i].update()
  137. fireworks[i].draw()
  138. //remove exploded particles for performance
  139. if (fireworks[i].stoped()) {
  140. fireworks[i].explode()
  141. fireworks.splice(i, 1)
  142. }
  143. }
  144.  
  145. //separate loop for explosion particles
  146. for (let i = explParticles.length - 1; i >= 0; i--) {
  147. explParticles[i].update()
  148. explParticles[i].draw()
  149. //remove faded particles for performance
  150. if (explParticles[i].faded()) {
  151. explParticles.splice(i, 1)
  152. }
  153. }
  154.  
  155. if (!paused) {
  156. //call next animation recursively
  157. fc = requestAnimationFrame(animate)
  158. }
  159. }
  160.  
  161. //utility functions
  162. function calcFPS() {
  163. //current time-last timestamp
  164. let timeDiff = Date.now() - lastTimeCalled
  165. //update timestamp
  166. lastTimeCalled = Date.now()
  167. //1000ms/time passed
  168. fps.innerText = 'fps: ' + floor(1000 / timeDiff)
  169. }
  170.  
  171. function sp() {
  172. if (!paused) {
  173. window.cancelAnimationFrame(fc)
  174. paused = true
  175. } else {
  176. window.requestAnimationFrame(animate)
  177. paused = false
  178. }
  179. }
  180.  
  181. function clear() {
  182. ctx.fillStyle = 'rgba(0,0,0,0.45)'
  183. ctx.fillRect(0, 0, W, H)
  184. }
  185.  
  186. function clearAll() {
  187. fireworks = []
  188. explParticles = []
  189. clear()
  190. }
  191.  
  192. function setSize(w, h, pd) {
  193. //canvas apparent size
  194. c.style.width = w + 'px'
  195. c.style.height = h + 'px'
  196. //canvas actual size
  197. c.width = W = w * pd
  198. c.height = H = h * pd
  199. }
  200.  
  201. function random(min = 0, max = 1) {
  202. return Math.random() * (max - min) + min
  203. }
  204.  
  205. function info() {
  206. alert(
  207. 'Vanilla js fireworks!\n\nClick --> Start/Pause.\nDouble click --> Reset.\n\nUpdates:\n1) Cleaned & updated to es6 and also finally added round explosions.\n2) Further cleaned the code using object inheritance.'
  208. )
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement