fireworks.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. a
  28. }
  29. function createCircle(e, t) {
  30. var a = {};
  31. return a.x = e,
  32. a.y = t,
  33. a.color = "#F00",
  34. a.radius = .1,
  35. a.alpha = .5,
  36. a.lineWidth = 6,
  37. a.draw = function() {
  38. ctx.globalAlpha = a.alpha,
  39. ctx.beginPath(),
  40. ctx.arc(a.x, a.y, a.radius, 0, 2 * Math.PI, !0),
  41. ctx.lineWidth = a.lineWidth,
  42. ctx.strokeStyle = a.color,
  43. ctx.stroke(),
  44. ctx.globalAlpha = 1
  45. },
  46. a
  47. }
  48. function renderParticule(e) {
  49. for (var t = 0; t < e.animatables.length; t++)
  50. e.animatables[t].target.draw()
  51. }
  52. function animateParticules(e, t) {
  53. for (var a = createCircle(e, t), n = [], i = 0; i < numberOfParticules; i++)
  54. n.push(createParticule(e, t));
  55. anime.timeline().add({
  56. targets: n,
  57. x: function(e) {
  58. return e.endPos.x
  59. },
  60. y: function(e) {
  61. return e.endPos.y
  62. },
  63. radius: .1,
  64. duration: anime.random(1200, 1800),
  65. easing: "easeOutExpo",
  66. update: renderParticule
  67. }).add({
  68. targets: a,
  69. radius: anime.random(80, 160),
  70. lineWidth: 0,
  71. alpha: {
  72. value: 0,
  73. easing: "linear",
  74. duration: anime.random(600, 800)
  75. },
  76. duration: anime.random(1200, 1800),
  77. easing: "easeOutExpo",
  78. update: renderParticule,
  79. offset: 0
  80. })
  81. }
  82. function debounce(fn, delay) {
  83. var timer
  84. return function() {
  85. var context = this
  86. var args = arguments
  87. clearTimeout(timer)
  88. timer = setTimeout(function() {
  89. fn.apply(context, args)
  90. }, delay)
  91. }
  92. }
  93. var canvasEl = document.querySelector(".fireworks");
  94. if (canvasEl) {
  95. var ctx = canvasEl.getContext("2d"),
  96. numberOfParticules = 30,
  97. pointerX = 0,
  98. pointerY = 0,
  99. tap = "mousedown",
  100. colors = ["#FF1461", "#18FF92", "#5A87FF", "#FBF38C"],
  101. setCanvasSize = debounce(function() {
  102. canvasEl.width = 2 * window.innerWidth,
  103. canvasEl.height = 2 * window.innerHeight,
  104. canvasEl.style.width = window.innerWidth + "px",
  105. canvasEl.style.height = window.innerHeight + "px",
  106. canvasEl.getContext("2d").scale(2, 2)
  107. }, 500),
  108. render = anime({
  109. duration: 1 / 0,
  110. update: function() {
  111. ctx.clearRect(0, 0, canvasEl.width, canvasEl.height)
  112. }
  113. });
  114. document.addEventListener(tap, function(e) {
  115. "sidebar" !== e.target.id && "toggle-sidebar" !== e.target.id && "A" !== e.target.nodeName && "IMG" !== e.target.nodeName && (render.play(),
  116. updateCoords(e),
  117. animateParticules(pointerX, pointerY))
  118. }, !1),
  119. setCanvasSize(),
  120. window.addEventListener("resize", setCanvasSize, !1)
  121. }