-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.js
More file actions
112 lines (85 loc) · 3.04 KB
/
cursor.js
File metadata and controls
112 lines (85 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
document.addEventListener('DOMContentLoaded', () => {
// Only on desktop devices
if (window.matchMedia("(pointer: coarse)").matches) {
return;
}
const cursor = document.createElement('div');
cursor.className = 'custom-cursor';
const follower = document.createElement('div');
follower.className = 'custom-cursor-follower';
document.body.appendChild(cursor);
document.body.appendChild(follower);
let posX = 0, posY = 0;
let mouseX = 0, mouseY = 0;
// Initial position to avoid flying in from top-left
let hasMoved = false;
// Velocity tracking for distortion
let velX = 0;
let velY = 0;
// Smooth factor
const ease = 0.15;
document.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
if (!hasMoved) {
posX = mouseX;
posY = mouseY;
hasMoved = true;
}
// Instant update for the dot
cursor.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`;
});
// Click effects
document.addEventListener('mousedown', () => {
cursor.classList.add('clicking');
follower.classList.add('clicking');
});
document.addEventListener('mouseup', () => {
cursor.classList.remove('clicking');
follower.classList.remove('clicking');
});
// Smooth follower movement with Jelly Effect
function render() {
if (!hasMoved) {
requestAnimationFrame(render);
return;
}
// Calculate smooth movement
const targetX = mouseX;
const targetY = mouseY;
// Current velocity
const dx = targetX - posX;
const dy = targetY - posY;
posX += dx * ease;
posY += dy * ease;
velX = dx * ease;
velY = dy * ease;
// Calculate stretch/squeeze based on velocity
const speed = Math.sqrt(velX * velX + velY * velY);
const maxSkew = 50; // max velocity clamp
const scale = Math.min(speed / 200, 0.4); // max scale distortion
const angle = Math.atan2(velY, velX) * 180 / Math.PI;
// default scale is 1, stretch x, squeeze y
const scaleX = 1 + scale;
const scaleY = 1 - scale;
// Only apply transform if we are moving significantly
const transform = `translate3d(${posX - 20}px, ${posY - 20}px, 0) rotate(${angle}deg) scale(${scaleX}, ${scaleY})`;
follower.style.transform = transform;
requestAnimationFrame(render);
}
if (!window.matchMedia("(pointer: coarse)").matches) {
render();
}
// Hover effects
const clickables = document.querySelectorAll('a, button, .project-card, .blog-post');
clickables.forEach(el => {
el.addEventListener('mouseenter', () => {
cursor.classList.add('hover');
follower.classList.add('hover');
});
el.addEventListener('mouseleave', () => {
cursor.classList.remove('hover');
follower.classList.remove('hover');
});
});
});