Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. if(document.getElementsByClassName('zfr3Q')[1].textContent.indexOf("ZIEL ERREICHT!") >= 0){
  2.  
  3. var canv = document.createElement("canvas");
  4. canv.setAttribute('id','canvas');
  5. canv.setAttribute('style','position:absolute;top:0px');
  6. document.body.appendChild(canv);
  7.  
  8.  
  9. // now we will setup our basic variables for the demo
  10. var canvas = document.getElementById( 'canvas' ),
  11. ctx = canvas.getContext( '2d' ),
  12. // full screen dimensions
  13. cw = window.innerWidth,
  14. ch = window.innerHeight,
  15. // firework collection
  16. fireworks = [],
  17. // particle collection
  18. particles = [],
  19. // starting hue
  20. hue = 120,
  21. // when launching fireworks with a click, too many get launched at once without a limiter, one launch per 5 loop ticks
  22. limiterTotal = 5,
  23. limiterTick = 0,
  24. // this will time the auto launches of fireworks, one launch per 80 loop ticks
  25. timerTotal = 50,
  26. timerTick = 0,
  27. mousedown = false,
  28. // mouse x coordinate,
  29. mx,
  30. // mouse y coordinate
  31. my;
  32.  
  33. // set canvas dimensions
  34. canvas.width = cw;
  35. canvas.height = ch;
  36.  
  37. loop();
  38.  
  39. }
  40.  
  41. // when animating on canvas, it is best to use requestAnimationFrame instead of setTimeout or setInterval
  42. // not supported in all browsers though and sometimes needs a prefix, so we need a shim
  43. window.requestAnimFrame = ( function() {
  44. return window.requestAnimationFrame ||
  45. window.webkitRequestAnimationFrame ||
  46. window.mozRequestAnimationFrame ||
  47. function( callback ) {
  48. window.setTimeout( callback, 1000 / 60 );
  49. };
  50. })();
  51.  
  52. // now we are going to setup our function placeholders for the entire demo
  53.  
  54. // get a random number within a range
  55. function random( min, max ) {
  56. return Math.random() * ( max - min ) + min;
  57. }
  58.  
  59. // calculate the distance between two points
  60. function calculateDistance( p1x, p1y, p2x, p2y ) {
  61. var xDistance = p1x - p2x,
  62. yDistance = p1y - p2y;
  63. return Math.sqrt( Math.pow( xDistance, 2 ) + Math.pow( yDistance, 2 ) );
  64. }
  65.  
  66. // create firework
  67. function Firework( sx, sy, tx, ty ) {
  68. // actual coordinates
  69. this.x = sx;
  70. this.y = sy;
  71. // starting coordinates
  72. this.sx = sx;
  73. this.sy = sy;
  74. // target coordinates
  75. this.tx = tx;
  76. this.ty = ty;
  77. // distance from starting point to target
  78. this.distanceToTarget = calculateDistance( sx, sy, tx, ty );
  79. this.distanceTraveled = 0;
  80. // track the past coordinates of each firework to create a trail effect, increase the coordinate count to create more prominent trails
  81. this.coordinates = [];
  82. this.coordinateCount = 3;
  83. // populate initial coordinate collection with the current coordinates
  84. while( this.coordinateCount-- ) {
  85. this.coordinates.push( [ this.x, this.y ] );
  86. }
  87. this.angle = Math.atan2( ty - sy, tx - sx );
  88. this.speed = 2;
  89. this.acceleration = 1.05;
  90. this.brightness = random( 50, 70 );
  91. // circle target indicator radius
  92. this.targetRadius = 1;
  93. }
  94.  
  95. // update firework
  96. Firework.prototype.update = function( index ) {
  97. // remove last item in coordinates array
  98. this.coordinates.pop();
  99. // add current coordinates to the start of the array
  100. this.coordinates.unshift( [ this.x, this.y ] );
  101.  
  102. // cycle the circle target indicator radius
  103. if( this.targetRadius < 8 ) {
  104. this.targetRadius += 0.3;
  105. } else {
  106. this.targetRadius = 1;
  107. }
  108.  
  109. // speed up the firework
  110. this.speed *= this.acceleration;
  111.  
  112. // get the current velocities based on angle and speed
  113. var vx = Math.cos( this.angle ) * this.speed,
  114. vy = Math.sin( this.angle ) * this.speed;
  115. // how far will the firework have traveled with velocities applied?
  116. this.distanceTraveled = calculateDistance( this.sx, this.sy, this.x + vx, this.y + vy );
  117.  
  118. // if the distance traveled, including velocities, is greater than the initial distance to the target, then the target has been reached
  119. if( this.distanceTraveled >= this.distanceToTarget ) {
  120. createParticles( this.tx, this.ty );
  121. // remove the firework, use the index passed into the update function to determine which to remove
  122. fireworks.splice( index, 1 );
  123. } else {
  124. // target not reached, keep traveling
  125. this.x += vx;
  126. this.y += vy;
  127. }
  128. }
  129.  
  130. // draw firework
  131. Firework.prototype.draw = function() {
  132. ctx.beginPath();
  133. // move to the last tracked coordinate in the set, then draw a line to the current x and y
  134. ctx.moveTo( this.coordinates[ this.coordinates.length - 1][ 0 ], this.coordinates[ this.coordinates.length - 1][ 1 ] );
  135. ctx.lineTo( this.x, this.y );
  136. ctx.strokeStyle = 'hsl(' + hue + ', 100%, ' + this.brightness + '%)';
  137. ctx.stroke();
  138.  
  139. ctx.beginPath();
  140. // draw the target for this firework with a pulsing circle
  141. ctx.arc( this.tx, this.ty, this.targetRadius, 0, Math.PI * 2 );
  142. ctx.stroke();
  143. }
  144.  
  145. // create particle
  146. function Particle( x, y ) {
  147. this.x = x;
  148. this.y = y;
  149. // track the past coordinates of each particle to create a trail effect, increase the coordinate count to create more prominent trails
  150. this.coordinates = [];
  151. this.coordinateCount = 5;
  152. while( this.coordinateCount-- ) {
  153. this.coordinates.push( [ this.x, this.y ] );
  154. }
  155. // set a random angle in all possible directions, in radians
  156. this.angle = random( 0, Math.PI * 2 );
  157. this.speed = random( 1, 10 );
  158. // friction will slow the particle down
  159. this.friction = 0.95;
  160. // gravity will be applied and pull the particle down
  161. this.gravity = 1;
  162. // set the hue to a random number +-50 of the overall hue variable
  163. this.hue = random( hue - 50, hue + 50 );
  164. this.brightness = random( 50, 80 );
  165. this.alpha = 1;
  166. // set how fast the particle fades out
  167. this.decay = random( 0.015, 0.03 );
  168. }
  169.  
  170. // update particle
  171. Particle.prototype.update = function( index ) {
  172. // remove last item in coordinates array
  173. this.coordinates.pop();
  174. // add current coordinates to the start of the array
  175. this.coordinates.unshift( [ this.x, this.y ] );
  176. // slow down the particle
  177. this.speed *= this.friction;
  178. // apply velocity
  179. this.x += Math.cos( this.angle ) * this.speed;
  180. this.y += Math.sin( this.angle ) * this.speed + this.gravity;
  181. // fade out the particle
  182. this.alpha -= this.decay;
  183.  
  184. // remove the particle once the alpha is low enough, based on the passed in index
  185. if( this.alpha <= this.decay ) {
  186. particles.splice( index, 1 );
  187. }
  188. }
  189.  
  190. // draw particle
  191. Particle.prototype.draw = function() {
  192. ctx. beginPath();
  193. // move to the last tracked coordinates in the set, then draw a line to the current x and y
  194. ctx.moveTo( this.coordinates[ this.coordinates.length - 1 ][ 0 ], this.coordinates[ this.coordinates.length - 1 ][ 1 ] );
  195. ctx.lineTo( this.x, this.y );
  196. ctx.strokeStyle = 'hsla(' + this.hue + ', 100%, ' + this.brightness + '%, ' + this.alpha + ')';
  197. ctx.stroke();
  198. }
  199.  
  200. // create particle group/explosion
  201. function createParticles( x, y ) {
  202. // increase the particle count for a bigger explosion, beware of the canvas performance hit with the increased particles though
  203. var particleCount = 30;
  204. while( particleCount-- ) {
  205. particles.push( new Particle( x, y ) );
  206. }
  207. }
  208.  
  209. // main demo loop
  210. function loop() {
  211. // this function will run endlessly with requestAnimationFrame
  212. requestAnimFrame( loop );
  213.  
  214. // increase the hue to get different colored fireworks over time
  215. //hue += 0.5;
  216.  
  217. // create random color
  218. hue= random(0, 360 );
  219.  
  220. // normally, clearRect() would be used to clear the canvas
  221. // we want to create a trailing effect though
  222. // setting the composite operation to destination-out will allow us to clear the canvas at a specific opacity, rather than wiping it entirely
  223. ctx.globalCompositeOperation = 'destination-out';
  224. // decrease the alpha property to create more prominent trails
  225. ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
  226. ctx.fillRect( 0, 0, cw, ch );
  227. // change the composite operation back to our main mode
  228. // lighter creates bright highlight points as the fireworks and particles overlap each other
  229. ctx.globalCompositeOperation = 'lighter';
  230.  
  231. // loop over each firework, draw it, update it
  232. var i = fireworks.length;
  233. while( i-- ) {
  234. fireworks[ i ].draw();
  235. fireworks[ i ].update( i );
  236. }
  237.  
  238. // loop over each particle, draw it, update it
  239. var i = particles.length;
  240. while( i-- ) {
  241. particles[ i ].draw();
  242. particles[ i ].update( i );
  243. }
  244.  
  245. // launch fireworks automatically to random coordinates, when the mouse isn't down
  246. if( timerTick >= timerTotal ) {
  247. if( !mousedown ) {
  248. // start the firework at the bottom middle of the screen, then set the random target coordinates, the random y coordinates will be set within the range of the top half of the screen
  249. fireworks.push( new Firework( cw / 2, ch, random( 0, cw ), random( 0, ch / 2 ) ) );
  250. timerTick = 0;
  251. }
  252. } else {
  253. timerTick++;
  254. }
  255.  
  256. // limit the rate at which fireworks get launched when mouse is down
  257. if( limiterTick >= limiterTotal ) {
  258. if( mousedown ) {
  259. // start the firework at the bottom middle of the screen, then set the current mouse coordinates as the target
  260. fireworks.push( new Firework( cw / 2, ch, mx, my ) );
  261. limiterTick = 0;
  262. }
  263. } else {
  264. limiterTick++;
  265. }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement