Skip to content

Commit 1f2f0a6

Browse files
committed
fix: ease splash spotlight back to center when cursor leaves
1 parent 4e7d776 commit 1f2f0a6

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/components/SplashBackground.astro

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
if (root.dataset.otxSplashInit === 'true') return;
1818
root.dataset.otxSplashInit = 'true';
1919

20+
const CENTER_X = 0.5;
21+
const CENTER_Y = 0.4;
22+
2023
const state = {
21-
targetX: 0.5,
22-
targetY: 0.4,
23-
currentX: 0.5,
24-
currentY: 0.4,
24+
targetX: CENTER_X,
25+
targetY: CENTER_Y,
26+
currentX: CENTER_X,
27+
currentY: CENTER_Y,
2528
time: 0,
29+
pointerInside: false,
2630
};
2731

2832
const setVars = () => {
@@ -44,21 +48,30 @@
4448
};
4549

4650
const onPointerMove = (event) => {
51+
state.pointerInside = true;
4752
state.targetX = event.clientX / window.innerWidth;
4853
state.targetY = event.clientY / window.innerHeight;
4954
};
5055

56+
const onPointerLeave = () => {
57+
state.pointerInside = false;
58+
state.targetX = CENTER_X;
59+
state.targetY = CENTER_Y;
60+
};
61+
5162
const tick = (timestamp) => {
5263
state.time = timestamp * 0.001;
5364
const idleX = Math.sin(state.time * 0.35) * 0.04;
5465
const idleY = Math.cos(state.time * 0.28) * 0.03;
66+
const lerp = state.pointerInside ? 0.06 : 0.08;
5567

56-
state.currentX += (state.targetX + idleX - state.currentX) * 0.06;
57-
state.currentY += (state.targetY + idleY - state.currentY) * 0.06;
68+
state.currentX += (state.targetX + idleX - state.currentX) * lerp;
69+
state.currentY += (state.targetY + idleY - state.currentY) * lerp;
5870
setVars();
5971
requestAnimationFrame(tick);
6072
};
6173

74+
document.documentElement.addEventListener('pointerleave', onPointerLeave);
6275
window.addEventListener('pointermove', onPointerMove, { passive: true });
6376
setVars();
6477
requestAnimationFrame(tick);

0 commit comments

Comments
 (0)