Advertisement
powylv

ghost cursor

Dec 1st, 2022 (edited)
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. <script>
  2. function ghostCursor(options) {
  3. let hasWrapperEl = options && options.element;
  4. let element = hasWrapperEl || document.body;
  5.  
  6. let width = window.innerWidth;
  7. let height = window.innerHeight;
  8. let cursor = { x: width / 2, y: width / 2 };
  9. let particles = [];
  10. let canvas, context;
  11.  
  12. let baseImage = new Image();
  13. baseImage.src = "https://cur.cursors-4u.net/cursors/cur-9/cur818.cur";
  14.  
  15. function init() {
  16. canvas = document.createElement("canvas");
  17. context = canvas.getContext("2d");
  18. canvas.style.top = "0px";
  19. canvas.style.left = "0px";
  20. canvas.style.pointerEvents = "none";
  21.  
  22. if (hasWrapperEl) {
  23. canvas.style.position = "absolute";
  24. element.appendChild(canvas);
  25. canvas.width = element.clientWidth;
  26. canvas.height = element.clientHeight;
  27. } else {
  28. canvas.style.position = "fixed";
  29. document.body.appendChild(canvas);
  30. canvas.width = width;
  31. canvas.height = height;
  32. }
  33.  
  34. bindEvents();
  35. loop();
  36. }
  37.  
  38. function bindEvents() {
  39. element.addEventListener("mousemove", onMouseMove);
  40. element.addEventListener("touchmove", onTouchMove, { passive: true });
  41. element.addEventListener("touchstart", onTouchMove, { passive: true });
  42. window.addEventListener("resize", onWindowResize);
  43. }
  44.  
  45. function onWindowResize(e) {
  46. width = window.innerWidth;
  47. height = window.innerHeight;
  48.  
  49. if (hasWrapperEl) {
  50. canvas.width = element.clientWidth;
  51. canvas.height = element.clientHeight;
  52. } else {
  53. canvas.width = width;
  54. canvas.height = height;
  55. }
  56. }
  57.  
  58. function onTouchMove(e) {
  59. if (e.touches.length > 0) {
  60. for (let i = 0; i < e.touches.length; i++) {
  61. addParticle(e.touches[i].clientX, e.touches[i].clientY, baseImage);
  62. }
  63. }
  64. }
  65.  
  66. function onMouseMove(e) {
  67. if (hasWrapperEl) {
  68. const boundingRect = element.getBoundingClientRect();
  69. cursor.x = e.clientX - boundingRect.left;
  70. cursor.y = e.clientY - boundingRect.top;
  71. } else {
  72. cursor.x = e.clientX;
  73. cursor.y = e.clientY;
  74. }
  75.  
  76. addParticle(cursor.x, cursor.y, baseImage);
  77. }
  78.  
  79. function addParticle(x, y, image) {
  80. particles.push(new Particle(x, y, image));
  81. }
  82.  
  83. function updateParticles() {
  84. context.clearRect(0, 0, width, height);
  85.  
  86. particles.sort(function (a, b) {
  87. return a.zIndex - b.zIndex;
  88. });
  89.  
  90. for (let i = 0; i < particles.length; i++) {
  91. particles[i].update(context);
  92. }
  93.  
  94. for (let i = particles.length - 1; i >= 0; i--) {
  95. if (particles[i].lifeSpan < 0) {
  96. particles.splice(i, 1);
  97. }
  98. }
  99. }
  100.  
  101. function loop() {
  102. updateParticles();
  103. requestAnimationFrame(loop);
  104. }
  105.  
  106. function Particle(x, y, image) {
  107. const lifeSpan = 40;
  108. this.initialLifeSpan = lifeSpan;
  109. this.lifeSpan = lifeSpan;
  110. this.position = { x: x, y: y };
  111. this.zIndex = 100;
  112.  
  113. this.image = image;
  114.  
  115. this.update = function (context) {
  116. this.lifeSpan--;
  117. const opacity = Math.max(this.lifeSpan / this.initialLifeSpan, 0);
  118.  
  119. context.globalAlpha = opacity;
  120. context.drawImage(this.image, this.position.x, this.position.y);
  121. };
  122. }
  123.  
  124. init();
  125. }
  126. </script>
  127.  
  128. <script>
  129. new ghostCursor();
  130. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement