Advertisement
naocrrds

a ghost cursor that appears ontop of ur container

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