perf(GradientBlinds): decouple pointer events from render loop
- Move scroll position and bounding client rect calculations from `pointermove` handler to `requestAnimationFrame` loop. - Use `pointerPosRef` to store raw event coordinates, reducing overhead in high-frequency event handlers. - Ensure spotlight effect correctly accounts for scroll position updates during animation frames. - Add regression test to verify `scrollX` is not accessed during pointer events. Co-authored-by: ragusa-it <196988693+ragusa-it@users.noreply.github.com>
This commit is contained in:
@@ -176,17 +176,11 @@ describe('GradientBlinds', () => {
|
|||||||
unmount();
|
unmount();
|
||||||
expect(removeEventListenerSpy).toHaveBeenCalledWith('pointermove', expect.any(Function));
|
expect(removeEventListenerSpy).toHaveBeenCalledWith('pointermove', expect.any(Function));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('minimizes getBoundingClientRect calls during pointer move', () => {
|
it('minimizes getBoundingClientRect calls during pointer move', () => {
|
||||||
const { unmount } = render(<GradientBlinds />);
|
const { unmount } = render(<GradientBlinds />);
|
||||||
|
|
||||||
// Spy on getBoundingClientRect
|
|
||||||
// Note: In jsdom, canvas is an HTMLCanvasElement which inherits from HTMLElement
|
|
||||||
const spy = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
const spy = vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
||||||
|
|
||||||
// Trigger pointer move to clear any initial calls or verify baseline
|
|
||||||
// The initial render calls resize(), which calls getBoundingClientRect on container
|
|
||||||
|
|
||||||
// Clear spy history from initial render
|
|
||||||
spy.mockClear();
|
spy.mockClear();
|
||||||
|
|
||||||
act(() => {
|
act(() => {
|
||||||
@@ -198,9 +192,37 @@ describe('GradientBlinds', () => {
|
|||||||
window.dispatchEvent(event);
|
window.dispatchEvent(event);
|
||||||
});
|
});
|
||||||
|
|
||||||
// EXPECTATION: It should NOT be called because the listener shouldn't be attached (not visible)
|
|
||||||
expect(spy).not.toHaveBeenCalled();
|
expect(spy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
unmount();
|
unmount();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('avoids expensive DOM reads (scrollX/Y) in pointermove handler when visible', () => {
|
||||||
|
const { unmount } = render(<GradientBlinds />);
|
||||||
|
|
||||||
|
// Spy on scrollX/scrollY getters
|
||||||
|
// Note: In jsdom, these are properties on window.
|
||||||
|
const scrollSpy = vi.spyOn(window, 'scrollX', 'get');
|
||||||
|
|
||||||
|
// Make visible to attach listener
|
||||||
|
act(() => {
|
||||||
|
if (ioCallback) {
|
||||||
|
ioCallback([{ isIntersecting: true } as IntersectionObserverEntry]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scrollSpy.mockClear();
|
||||||
|
|
||||||
|
// Trigger pointer move
|
||||||
|
act(() => {
|
||||||
|
window.dispatchEvent(new PointerEvent('pointermove', { clientX: 100, clientY: 100 }));
|
||||||
|
});
|
||||||
|
|
||||||
|
// With the optimization (moving to RAF loop), this should be 0.
|
||||||
|
// Without optimization, this will be > 0.
|
||||||
|
// Since we are mocking RAF and not running the loop, if it's in the loop, it won't be called.
|
||||||
|
expect(scrollSpy).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
unmount();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -64,6 +64,8 @@ const GradientBlinds: React.FC<GradientBlindsProps> = ({
|
|||||||
const geometryRef = useRef<Geometry | null>(null);
|
const geometryRef = useRef<Geometry | null>(null);
|
||||||
const rendererRef = useRef<Renderer | null>(null);
|
const rendererRef = useRef<Renderer | null>(null);
|
||||||
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
|
||||||
|
const pointerPosRef = useRef<{ x: number; y: number } | null>(null);
|
||||||
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);
|
||||||
@@ -309,31 +311,34 @@ void main() {
|
|||||||
ro.observe(container);
|
ro.observe(container);
|
||||||
|
|
||||||
const onPointerMove = (e: PointerEvent) => {
|
const onPointerMove = (e: PointerEvent) => {
|
||||||
const scale = (renderer as unknown as { dpr?: number }).dpr || 1;
|
pointerPosRef.current = { x: e.clientX, y: e.clientY };
|
||||||
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];
|
|
||||||
if (mouseDampening <= 0) {
|
|
||||||
uniforms.iMouse.value = [x, y];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const loop = (t: number) => {
|
const loop = (t: number) => {
|
||||||
rafRef.current = requestAnimationFrame(loop);
|
rafRef.current = requestAnimationFrame(loop);
|
||||||
uniforms.iTime.value = t * 0.001;
|
uniforms.iTime.value = t * 0.001;
|
||||||
|
|
||||||
|
// Update target based on pointer position and scroll offset
|
||||||
|
if (pointerPosRef.current) {
|
||||||
|
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 = (pointerPosRef.current.x - rectLeft) * scale;
|
||||||
|
y = (rectRef.current.height - (pointerPosRef.current.y - rectTop)) * scale;
|
||||||
|
} else {
|
||||||
|
// Fallback if rectRef missing
|
||||||
|
const rect = canvas.getBoundingClientRect();
|
||||||
|
x = (pointerPosRef.current.x - rect.left) * scale;
|
||||||
|
y = (rect.height - (pointerPosRef.current.y - rect.top)) * scale;
|
||||||
|
}
|
||||||
|
mouseTargetRef.current = [x, y];
|
||||||
|
}
|
||||||
|
|
||||||
if (mouseDampening > 0) {
|
if (mouseDampening > 0) {
|
||||||
if (!lastTimeRef.current) lastTimeRef.current = t;
|
if (!lastTimeRef.current) lastTimeRef.current = t;
|
||||||
const dt = (t - lastTimeRef.current) / 1000;
|
const dt = (t - lastTimeRef.current) / 1000;
|
||||||
@@ -346,6 +351,9 @@ 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) {
|
||||||
|
uniforms.iMouse.value = mouseTargetRef.current;
|
||||||
|
}
|
||||||
lastTimeRef.current = t;
|
lastTimeRef.current = t;
|
||||||
}
|
}
|
||||||
if (!paused && programRef.current && meshRef.current) {
|
if (!paused && programRef.current && meshRef.current) {
|
||||||
|
|||||||
Reference in New Issue
Block a user