1
0

index.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <html>
  2. <head>
  3. <title>Zoraxy Firework!</title>
  4. <style>
  5. body{
  6. margin: 0 !important;
  7. }
  8. canvas {
  9. display: block;
  10. width: 100vw;
  11. height: 100vh;
  12. }
  13. </style>
  14. <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.2/anime.min.js" integrity="sha512-aNMyYYxdIxIaot0Y1/PLuEu3eipGCmsEUBrUq+7aVyPGMFH8z0eTP0tkqAvv34fzN6z+201d3T8HPb1svWSKHQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  15. </head>
  16. <body>
  17. <canvas id="c"></canvas>
  18. <script>
  19. var c = document.getElementById("c");
  20. var ctx = c.getContext("2d");
  21. var cH;
  22. var cW;
  23. var bgColor = "#FF6138";
  24. var animations = [];
  25. var circles = [];
  26. var colorPicker = (function() {
  27. var colors = ["#FF6138", "#FFBE53", "#2980B9", "#FCFCFC", "#282741"];
  28. var index = 0;
  29. function next() {
  30. index = index++ < colors.length-1 ? index : 0;
  31. return colors[index];
  32. }
  33. function current() {
  34. return colors[index]
  35. }
  36. return {
  37. next: next,
  38. current: current
  39. }
  40. })();
  41. function removeAnimation(animation) {
  42. var index = animations.indexOf(animation);
  43. if (index > -1) animations.splice(index, 1);
  44. }
  45. function calcPageFillRadius(x, y) {
  46. var l = Math.max(x - 0, cW - x);
  47. var h = Math.max(y - 0, cH - y);
  48. return Math.sqrt(Math.pow(l, 2) + Math.pow(h, 2));
  49. }
  50. function addClickListeners() {
  51. document.addEventListener("touchstart", handleEvent);
  52. document.addEventListener("mousedown", handleEvent);
  53. };
  54. function handleEvent(e) {
  55. if (e.touches) {
  56. e.preventDefault();
  57. e = e.touches[0];
  58. }
  59. var currentColor = colorPicker.current();
  60. var nextColor = colorPicker.next();
  61. var targetR = calcPageFillRadius(e.pageX, e.pageY);
  62. var rippleSize = Math.min(200, (cW * .4));
  63. var minCoverDuration = 750;
  64. var pageFill = new Circle({
  65. x: e.pageX,
  66. y: e.pageY,
  67. r: 0,
  68. fill: nextColor
  69. });
  70. var fillAnimation = anime({
  71. targets: pageFill,
  72. r: targetR,
  73. duration: Math.max(targetR / 2 , minCoverDuration ),
  74. easing: "easeOutQuart",
  75. complete: function(){
  76. bgColor = pageFill.fill;
  77. removeAnimation(fillAnimation);
  78. }
  79. });
  80. var ripple = new Circle({
  81. x: e.pageX,
  82. y: e.pageY,
  83. r: 0,
  84. fill: currentColor,
  85. stroke: {
  86. width: 3,
  87. color: currentColor
  88. },
  89. opacity: 1
  90. });
  91. var rippleAnimation = anime({
  92. targets: ripple,
  93. r: rippleSize,
  94. opacity: 0,
  95. easing: "easeOutExpo",
  96. duration: 900,
  97. complete: removeAnimation
  98. });
  99. var particles = [];
  100. for (var i=0; i<32; i++) {
  101. var particle = new Circle({
  102. x: e.pageX,
  103. y: e.pageY,
  104. fill: currentColor,
  105. r: anime.random(24, 48)
  106. })
  107. particles.push(particle);
  108. }
  109. var particlesAnimation = anime({
  110. targets: particles,
  111. x: function(particle){
  112. return particle.x + anime.random(rippleSize, -rippleSize);
  113. },
  114. y: function(particle){
  115. return particle.y + anime.random(rippleSize * 1.15, -rippleSize * 1.15);
  116. },
  117. r: 0,
  118. easing: "easeOutExpo",
  119. duration: anime.random(1000,1300),
  120. complete: removeAnimation
  121. });
  122. animations.push(fillAnimation, rippleAnimation, particlesAnimation);
  123. }
  124. function extend(a, b){
  125. for(var key in b) {
  126. if(b.hasOwnProperty(key)) {
  127. a[key] = b[key];
  128. }
  129. }
  130. return a;
  131. }
  132. var Circle = function(opts) {
  133. extend(this, opts);
  134. }
  135. Circle.prototype.draw = function() {
  136. ctx.globalAlpha = this.opacity || 1;
  137. ctx.beginPath();
  138. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  139. if (this.stroke) {
  140. ctx.strokeStyle = this.stroke.color;
  141. ctx.lineWidth = this.stroke.width;
  142. ctx.stroke();
  143. }
  144. if (this.fill) {
  145. ctx.fillStyle = this.fill;
  146. ctx.fill();
  147. }
  148. ctx.closePath();
  149. ctx.globalAlpha = 1;
  150. }
  151. var animate = anime({
  152. duration: Infinity,
  153. update: function() {
  154. ctx.fillStyle = bgColor;
  155. ctx.fillRect(0, 0, cW, cH);
  156. animations.forEach(function(anim) {
  157. anim.animatables.forEach(function(animatable) {
  158. animatable.target.draw();
  159. });
  160. });
  161. }
  162. });
  163. var resizeCanvas = function() {
  164. cW = window.innerWidth;
  165. cH = window.innerHeight;
  166. c.width = cW * devicePixelRatio;
  167. c.height = cH * devicePixelRatio;
  168. ctx.scale(devicePixelRatio, devicePixelRatio);
  169. };
  170. (function init() {
  171. resizeCanvas();
  172. if (window.CP) {
  173. // CodePen's loop detection was causin' problems
  174. // and I have no idea why, so...
  175. window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;
  176. }
  177. window.addEventListener("resize", resizeCanvas);
  178. addClickListeners();
  179. if (!!window.location.pathname.match(/fullcpgrid/)) {
  180. startFauxClicking();
  181. }
  182. handleInactiveUser();
  183. })();
  184. function handleInactiveUser() {
  185. var inactive = setTimeout(function(){
  186. fauxClick(cW/2, cH/2);
  187. }, 2000);
  188. function clearInactiveTimeout() {
  189. clearTimeout(inactive);
  190. document.removeEventListener("mousedown", clearInactiveTimeout);
  191. document.removeEventListener("touchstart", clearInactiveTimeout);
  192. }
  193. document.addEventListener("mousedown", clearInactiveTimeout);
  194. document.addEventListener("touchstart", clearInactiveTimeout);
  195. }
  196. function startFauxClicking() {
  197. setTimeout(function(){
  198. fauxClick(anime.random( cW * .2, cW * .8), anime.random(cH * .2, cH * .8));
  199. startFauxClicking();
  200. }, anime.random(200, 900));
  201. }
  202. function fauxClick(x, y) {
  203. var fauxClick = new Event("mousedown");
  204. fauxClick.pageX = x;
  205. fauxClick.pageY = y;
  206. document.dispatchEvent(fauxClick);
  207. }
  208. </script>
  209. </body>
  210. </html>