Merge pull request #37 from ragusa-it/fix/gradient-blinds-mobile-scroll-5721743487777744153

Fix GradientBlinds scroll drift on mobile
This commit was merged in pull request #37.
This commit is contained in:
Melvin Ragusa
2026-01-30 01:16:59 +01:00
committed by GitHub

View File

@@ -66,6 +66,7 @@ const GradientBlinds: React.FC<GradientBlindsProps> = ({
const mouseTargetRef = useRef<[number, number]>([0, 0]); const mouseTargetRef = useRef<[number, number]>([0, 0]);
// Optimization: store raw pointer position (viewport coords) to decouple event handling from calculation // Optimization: store raw pointer position (viewport coords) to decouple event handling from calculation
const pointerPosRef = useRef<{ x: number; y: number } | null>(null); const pointerPosRef = useRef<{ x: number; y: number } | null>(null);
const isMobileRef = useRef<boolean>(false);
const lastTimeRef = useRef<number>(0); const lastTimeRef = useRef<number>(0);
const firstResizeRef = useRef<boolean>(true); const firstResizeRef = useRef<boolean>(true);
const rectRef = useRef<DOMRect | null>(null); const rectRef = useRef<DOMRect | null>(null);
@@ -283,6 +284,7 @@ void main() {
const rect = container.getBoundingClientRect(); const rect = container.getBoundingClientRect();
rectRef.current = rect; rectRef.current = rect;
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
isMobileRef.current = window.innerWidth <= 768;
scrollPosRef.current = { x: window.scrollX, y: window.scrollY }; scrollPosRef.current = { x: window.scrollX, y: window.scrollY };
} }
renderer.setSize(rect.width, rect.height); renderer.setSize(rect.width, rect.height);
@@ -311,7 +313,30 @@ void main() {
ro.observe(container); ro.observe(container);
const onPointerMove = (e: PointerEvent) => { const onPointerMove = (e: PointerEvent) => {
if (isMobileRef.current) {
// On mobile, calculate relative position immediately and store in target.
// This prevents the "ghost drift" effect when scrolling with inertia after lifting finger,
// because we won't be updating the target based on scroll position in the loop.
const scale = (renderer as unknown as { dpr?: number }).dpr || 1;
let x, y;
if (rectRef.current) {
const dx = window.scrollX - scrollPosRef.current.x;
const dy = window.scrollY - scrollPosRef.current.y;
const rectLeft = rectRef.current.left - dx;
const rectTop = rectRef.current.top - dy;
x = (e.clientX - rectLeft) * scale;
y = (rectRef.current.height - (e.clientY - rectTop)) * scale;
} else {
const rect = canvas.getBoundingClientRect();
x = (e.clientX - rect.left) * scale;
y = (rect.height - (e.clientY - rect.top)) * scale;
}
mouseTargetRef.current = [x, y];
pointerPosRef.current = null; // Ensure loop doesn't override
} else {
pointerPosRef.current = { x: e.clientX, y: e.clientY }; pointerPosRef.current = { x: e.clientX, y: e.clientY };
}
}; };
const loop = (t: number) => { const loop = (t: number) => {
@@ -351,7 +376,7 @@ void main() {
cur[0] += (target[0] - cur[0]) * factor; cur[0] += (target[0] - cur[0]) * factor;
cur[1] += (target[1] - cur[1]) * factor; cur[1] += (target[1] - cur[1]) * factor;
} else { } else {
if (pointerPosRef.current) { if (pointerPosRef.current || isMobileRef.current) {
uniforms.iMouse.value = mouseTargetRef.current; uniforms.iMouse.value = mouseTargetRef.current;
} }
lastTimeRef.current = t; lastTimeRef.current = t;