-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcursor.js
More file actions
76 lines (67 loc) · 2.02 KB
/
Copy pathcursor.js
File metadata and controls
76 lines (67 loc) · 2.02 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
(function () {
'use strict';
var CURSOR_RING_LERP = 0.14;
if (!window.matchMedia('(pointer: fine)').matches) return;
var dot = document.querySelector('.cursor-dot');
var ring = document.querySelector('.cursor-ring');
if (!dot || !ring) return;
var mx = -200;
var my = -200;
var rx = -200;
var ry = -200;
var rafId = null;
function animateRing() {
rx += (mx - rx) * CURSOR_RING_LERP;
ry += (my - ry) * CURSOR_RING_LERP;
ring.style.left = rx + 'px';
ring.style.top = ry + 'px';
if (Math.abs(mx - rx) > 0.5 || Math.abs(my - ry) > 0.5) {
rafId = requestAnimationFrame(animateRing);
} else {
rafId = null;
}
}
function startLoop() {
if (!rafId && !document.hidden) {
rafId = requestAnimationFrame(animateRing);
}
}
document.addEventListener('mousemove', function (e) {
mx = e.clientX;
my = e.clientY;
dot.style.left = mx + 'px';
dot.style.top = my + 'px';
startLoop();
});
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
if (rafId) {
cancelAnimationFrame(rafId);
rafId = null;
}
} else {
startLoop();
}
});
var hoverTargets = 'a,button,[role="button"],input,textarea,select,label,.case-back';
document.addEventListener('mouseover', function (e) {
if (e.target.closest(hoverTargets)) document.body.classList.add('cursor-hovering');
});
document.addEventListener('mouseout', function (e) {
if (e.target.closest(hoverTargets)) document.body.classList.remove('cursor-hovering');
});
document.addEventListener('mousedown', function () {
document.body.classList.add('cursor-clicking');
});
document.addEventListener('mouseup', function () {
document.body.classList.remove('cursor-clicking');
});
document.addEventListener('mouseleave', function () {
dot.style.opacity = '0';
ring.style.opacity = '0';
});
document.addEventListener('mouseenter', function () {
dot.style.opacity = '1';
ring.style.opacity = '1';
});
})();