Skip to content

Commit

Permalink
feat: adds delay option to ghost cursor effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 committed Aug 26, 2023
1 parent 2a4d6f7 commit ea76497
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/ghostCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ export function ghostCursor(options) {
let hasWrapperEl = options && options.element;
let element = hasWrapperEl || document.body;

let randomDelay = options && options.randomDelay;
let minDelay = options && options.minDelay || 5;
let maxDelay = options && options.maxDelay || 50;

let width = window.innerWidth;
let height = window.innerHeight;
let cursor = { x: width / 2, y: width / 2 };
Expand Down Expand Up @@ -85,7 +89,17 @@ export function ghostCursor(options) {
}
}

let getDelay = () => Math.floor(Math.random() * (maxDelay - minDelay + 1)) + minDelay;
let lastTimeParticleAdded = Date.now(),
interval = getDelay();

function onMouseMove(e) {
if (randomDelay) {
if (lastTimeParticleAdded + interval > Date.now()) return;
lastTimeParticleAdded = Date.now();
interval = getDelay();
}

if (hasWrapperEl) {
const boundingRect = element.getBoundingClientRect();
cursor.x = e.clientX - boundingRect.left;
Expand Down

0 comments on commit ea76497

Please sign in to comment.