fireworks.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. function updateCoords(e) {
  2. pointerX = (e.clientX || e.touches[0].clientX) - canvasEl.getBoundingClientRect().left,
  3. pointerY = e.clientY || e.touches[0].clientY - canvasEl.getBoundingClientRect().top
  4. }
  5. function setParticuleDirection(e) {
  6. var t = anime.random(0, 360) * Math.PI / 180
  7. , a = anime.random(50, 180)
  8. , n = [-1, 1][anime.random(0, 1)] * a;
  9. return {
  10. x: e.x + n * Math.cos(t),
  11. y: e.y + n * Math.sin(t)
  12. }
  13. }
  14. function createParticule(e, t) {
  15. var a = {};
  16. return a.x = e,
  17. a.y = t,
  18. a.color = colors[anime.random(0, colors.length - 1)],
  19. a.radius = anime.random(16, 32),
  20. a.endPos = setParticuleDirection(a),
  21. a.draw = function () {
  22. ctx.beginPath(),
  23. ctx.arc(a.x, a.y, a.radius, 0, 2 * Math.PI, !0),
  24. ctx.fillStyle = a.color,
  25. ctx.fill()
  26. }
  27. ,
  28. a
  29. }
  30. function createCircle(e, t) {
  31. var a = {};
  32. return a.x = e,
  33. a.y = t,
  34. a.color = "#F00",
  35. a.radius = .1,
  36. a.alpha = .5,
  37. a.lineWidth = 6,
  38. a.draw = function () {
  39. ctx.globalAlpha = a.alpha,
  40. ctx.beginPath(),
  41. ctx.arc(a.x, a.y, a.radius, 0, 2 * Math.PI, !0),
  42. ctx.lineWidth = a.lineWidth,
  43. ctx.strokeStyle = a.color,
  44. ctx.stroke(),
  45. ctx.globalAlpha = 1
  46. }
  47. ,
  48. a
  49. }
  50. function renderParticule(e) {
  51. for (var t = 0; t < e.animatables.length; t++)
  52. e.animatables[t].target.draw()
  53. }
  54. function animateParticules(e, t) {
  55. for (var a = createCircle(e, t), n = [], i = 0; i < numberOfParticules; i++)
  56. n.push(createParticule(e, t));
  57. anime.timeline().add({
  58. targets: n,
  59. x: function (e) {
  60. return e.endPos.x
  61. },
  62. y: function (e) {
  63. return e.endPos.y
  64. },
  65. radius: .1,
  66. duration: anime.random(1200, 1800),
  67. easing: "easeOutExpo",
  68. update: renderParticule
  69. }).add({
  70. targets: a,
  71. radius: anime.random(80, 160),
  72. lineWidth: 0,
  73. alpha: {
  74. value: 0,
  75. easing: "linear",
  76. duration: anime.random(600, 800)
  77. },
  78. duration: anime.random(1200, 1800),
  79. easing: "easeOutExpo",
  80. update: renderParticule,
  81. offset: 0
  82. })
  83. }
  84. function debounce(fn, delay) {
  85. var timer
  86. return function () {
  87. var context = this
  88. var args = arguments
  89. clearTimeout(timer)
  90. timer = setTimeout(function () {
  91. fn.apply(context, args)
  92. }, delay)
  93. }
  94. }
  95. function isMobileBrowser() {
  96. var sUserAgent = navigator.userAgent.toLowerCase();
  97. var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  98. var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  99. var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  100. var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  101. var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  102. var bIsAndroid = sUserAgent.match(/android/i) == "android";
  103. var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  104. var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  105. if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
  106. return true;
  107. } else
  108. return false;
  109. }
  110. var canvasEl = document.querySelector("#fireworks");
  111. if (canvasEl && !isMobileBrowser()) {
  112. var ctx = canvasEl.getContext("2d")
  113. , numberOfParticules = 30
  114. , pointerX = 0
  115. , pointerY = 0
  116. , tap = "mousedown"
  117. , colors = ["#FF1461", "#18FF92", "#5A87FF", "#FBF38C"]
  118. , setCanvasSize = debounce(function () {
  119. canvasEl.width = 2 * window.innerWidth,
  120. canvasEl.height = 2 * window.innerHeight,
  121. canvasEl.style.width = window.innerWidth + "px",
  122. canvasEl.style.height = window.innerHeight + "px",
  123. canvasEl.getContext("2d").scale(2, 2)
  124. }, 500)
  125. , render = anime({
  126. duration: 1 / 0,
  127. update: function () {
  128. ctx.clearRect(0, 0, canvasEl.width, canvasEl.height)
  129. }
  130. });
  131. document.addEventListener(tap, function (e) {
  132. "sidebar" !== e.target.id && "toggle-sidebar" !== e.target.id && "A" !== e.target.nodeName && "IMG" !== e.target.nodeName && (render.play(),
  133. updateCoords(e),
  134. animateParticules(pointerX, pointerY))
  135. }, !1),
  136. setCanvasSize(),
  137. window.addEventListener("resize", setCanvasSize, !1)
  138. }