springyEmojiCursor.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // The springy emoji effect has been translated over from this old
  2. // code, to modern js & canvas
  3. // - http://www.yaldex.com/FSMessages/ElasticBullets.htm
  4. function springyEmojiCursor(options) {
  5. let emoji = (options && options.emoji) || "🤪"
  6. let hasWrapperEl = options && options.element
  7. let element = hasWrapperEl || document.body
  8. let nDots = 7
  9. let DELTAT = 0.01
  10. let SEGLEN = 10
  11. let SPRINGK = 10
  12. let MASS = 1
  13. let GRAVITY = 50
  14. let RESISTANCE = 10
  15. let STOPVEL = 0.1
  16. let STOPACC = 0.1
  17. let DOTSIZE = 11
  18. let BOUNCE = 0.7
  19. let width = window.innerWidth
  20. let height = window.innerHeight
  21. let cursor = { x: width / 2, y: width / 2 }
  22. let particles = []
  23. let canvas, context
  24. let emojiAsImage
  25. function init() {
  26. canvas = document.createElement("canvas")
  27. context = canvas.getContext("2d")
  28. canvas.style.top = "0px"
  29. canvas.style.left = "0px"
  30. canvas.style.pointerEvents = "none"
  31. if (hasWrapperEl) {
  32. canvas.style.position = "absolute"
  33. element.appendChild(canvas)
  34. canvas.width = element.clientWidth
  35. canvas.height = element.clientHeight
  36. } else {
  37. canvas.style.position = "fixed"
  38. document.body.appendChild(canvas)
  39. canvas.width = width
  40. canvas.height = height
  41. }
  42. // Save emoji as an image for performance
  43. context.font = "16px serif"
  44. context.textBaseline = "middle"
  45. context.textAlign = "center"
  46. let measurements = context.measureText(emoji)
  47. let bgCanvas = document.createElement("canvas")
  48. let bgContext = bgCanvas.getContext("2d")
  49. bgCanvas.width = measurements.width
  50. bgCanvas.height = measurements.actualBoundingBoxAscent * 2
  51. bgContext.textAlign = "center"
  52. bgContext.font = "16px serif"
  53. bgContext.textBaseline = "middle"
  54. bgContext.fillText(
  55. emoji,
  56. bgCanvas.width / 2,
  57. measurements.actualBoundingBoxAscent
  58. )
  59. emojiAsImage = bgCanvas
  60. let i = 0
  61. for (i = 0; i < nDots; i++) {
  62. particles[i] = new Particle(emojiAsImage)
  63. }
  64. bindEvents()
  65. loop()
  66. }
  67. // Bind events that are needed
  68. function bindEvents() {
  69. element.addEventListener("mousemove", onMouseMove)
  70. element.addEventListener("touchmove", onTouchMove, { passive: true })
  71. element.addEventListener("touchstart", onTouchMove, { passive: true })
  72. window.addEventListener("resize", onWindowResize)
  73. }
  74. function onWindowResize(e) {
  75. width = window.innerWidth
  76. height = window.innerHeight
  77. if (hasWrapperEl) {
  78. canvas.width = element.clientWidth
  79. canvas.height = element.clientHeight
  80. } else {
  81. canvas.width = width
  82. canvas.height = height
  83. }
  84. }
  85. function onTouchMove(e) {
  86. if (e.touches.length > 0) {
  87. if (hasWrapperEl) {
  88. const boundingRect = element.getBoundingClientRect()
  89. cursor.x = e.touches[0].clientX - boundingRect.left
  90. cursor.y = e.touches[0].clientY - boundingRect.top
  91. } else {
  92. cursor.x = e.touches[0].clientX
  93. cursor.y = e.touches[0].clientY
  94. }
  95. }
  96. }
  97. function onMouseMove(e) {
  98. if (hasWrapperEl) {
  99. const boundingRect = element.getBoundingClientRect()
  100. cursor.x = e.clientX - boundingRect.left
  101. cursor.y = e.clientY - boundingRect.top
  102. } else {
  103. cursor.x = e.clientX
  104. cursor.y = e.clientY
  105. }
  106. }
  107. function updateParticles() {
  108. canvas.width = canvas.width
  109. // follow mouse
  110. particles[0].position.x = cursor.x
  111. particles[0].position.y = cursor.y
  112. // Start from 2nd dot
  113. for (i = 1; i < nDots; i++) {
  114. let spring = new vec(0, 0)
  115. if (i > 0) {
  116. springForce(i - 1, i, spring)
  117. }
  118. if (i < nDots - 1) {
  119. springForce(i + 1, i, spring)
  120. }
  121. let resist = new vec(
  122. -particles[i].velocity.x * RESISTANCE,
  123. -particles[i].velocity.y * RESISTANCE
  124. )
  125. let accel = new vec(
  126. (spring.X + resist.X) / MASS,
  127. (spring.Y + resist.Y) / MASS + GRAVITY
  128. )
  129. particles[i].velocity.x += DELTAT * accel.X
  130. particles[i].velocity.y += DELTAT * accel.Y
  131. if (
  132. Math.abs(particles[i].velocity.x) < STOPVEL &&
  133. Math.abs(particles[i].velocity.y) < STOPVEL &&
  134. Math.abs(accel.X) < STOPACC &&
  135. Math.abs(accel.Y) < STOPACC
  136. ) {
  137. particles[i].velocity.x = 0
  138. particles[i].velocity.y = 0
  139. }
  140. particles[i].position.x += particles[i].velocity.x
  141. particles[i].position.y += particles[i].velocity.y
  142. let height, width
  143. height = canvas.clientHeight
  144. width = canvas.clientWidth
  145. if (particles[i].position.y >= height - DOTSIZE - 1) {
  146. if (particles[i].velocity.y > 0) {
  147. particles[i].velocity.y = BOUNCE * -particles[i].velocity.y
  148. }
  149. particles[i].position.y = height - DOTSIZE - 1
  150. }
  151. if (particles[i].position.x >= width - DOTSIZE) {
  152. if (particles[i].velocity.x > 0) {
  153. particles[i].velocity.x = BOUNCE * -particles[i].velocity.x
  154. }
  155. particles[i].position.x = width - DOTSIZE - 1
  156. }
  157. if (particles[i].position.x < 0) {
  158. if (particles[i].velocity.x < 0) {
  159. particles[i].velocity.x = BOUNCE * -particles[i].velocity.x
  160. }
  161. particles[i].position.x = 0
  162. }
  163. particles[i].draw(context)
  164. }
  165. }
  166. function loop() {
  167. updateParticles()
  168. requestAnimationFrame(loop)
  169. }
  170. function vec(X, Y) {
  171. this.X = X
  172. this.Y = Y
  173. }
  174. function springForce(i, j, spring) {
  175. let dx = particles[i].position.x - particles[j].position.x
  176. let dy = particles[i].position.y - particles[j].position.y
  177. let len = Math.sqrt(dx * dx + dy * dy)
  178. if (len > SEGLEN) {
  179. let springF = SPRINGK * (len - SEGLEN)
  180. spring.X += (dx / len) * springF
  181. spring.Y += (dy / len) * springF
  182. }
  183. }
  184. function Particle(canvasItem) {
  185. this.position = { x: cursor.x, y: cursor.y }
  186. this.velocity = {
  187. x: 0,
  188. y: 0,
  189. }
  190. this.canv = canvasItem
  191. this.draw = function(context) {
  192. context.drawImage(
  193. this.canv,
  194. this.position.x - this.canv.width / 2,
  195. this.position.y - this.canv.height / 2,
  196. this.canv.width,
  197. this.canv.height
  198. )
  199. }
  200. }
  201. init()
  202. }