forked from anasjawed283/Open-For-All
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget_pointer.js
More file actions
68 lines (50 loc) · 2.03 KB
/
target_pointer.js
File metadata and controls
68 lines (50 loc) · 2.03 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
const lerp = (a, b, speed) => a + ((b - a) * speed);
class MagneticCursor {
constructor(cursorQuery, attractorQuery) {
this.target = {x: 0, y: 0};
this.cursorPosition = {x: 0, y: 0};
this.cursorSpeed = 0.35;
this.animationFrame = null;
this.renderCursorMovement.bind(this);
this.cursor = document.querySelector(cursorQuery);
console.log('Cursor', cursorQuery, this.cursor);
window.addEventListener('mousemove', (e) => this.onMouseMove(e));
this.addAttractors(attractorQuery);
}
addAttractors(attractorQuery) {
const attractors = document.querySelectorAll(attractorQuery);
attractors.forEach((attractor) => {
attractor.addEventListener('mouseenter', (e) => {
this.cursor.classList.add('locked');
const bounds = e.target.getBoundingClientRect();
this.target.x = bounds.x + bounds.width / 2;
this.target.y = bounds.y + bounds.height / 2;
});
attractor.addEventListener('mouseleave', (e) => {
this.cursor.classList.remove('locked');
});
});
}
onMouseMove(e) {
if (!this.cursor.classList.contains('locked')) {
this.target.x = e.x;
this.target.y = e.y;
}
if(!this.animationFrame) {
this.animationFrame = requestAnimationFrame(this.renderCursorMovement.bind(this));
}
}
renderCursorMovement() {
this.cursorPosition.x = lerp(this.cursorPosition.x, this.target.x, this.cursorSpeed);
this.cursorPosition.y = lerp(this.cursorPosition.y, this.target.y, this.cursorSpeed);
this.cursor.style.transform = `translate(${this.cursorPosition.x}px,${this.cursorPosition.y}px)`;
const delta = Math.abs(this.target.x - this.cursorPosition.x + this.target.y - this.cursorPosition.y);
if (delta <= 0.001) {
cancelAnimationFrame(this.animationFrame);
this.animationFrame = null;
return;
}
this.animationFrame = requestAnimationFrame(this.renderCursorMovement.bind(this));
}
}
new MagneticCursor('.magnetic-cursor', '.cursor-attractor');