perf: Optimize GradientBlinds pointer move handler
- Removed synchronous `getBoundingClientRect` call from `pointermove` handler - Implemented caching of canvas dimensions and scroll position using `ResizeObserver` - Added logic to compensate for scroll delta in pointer position calculation - Added regression test to verify `getBoundingClientRect` is not called during pointer interaction - Fixed `ogl` mock in tests to include `Geometry`
This commit is contained in:
@@ -22,6 +22,9 @@ vi.mock('ogl', () => {
|
||||
Mesh: class {
|
||||
remove() {}
|
||||
},
|
||||
Geometry: class {
|
||||
remove() {}
|
||||
},
|
||||
Triangle: class {
|
||||
remove() {}
|
||||
},
|
||||
@@ -129,4 +132,33 @@ describe('GradientBlinds', () => {
|
||||
unmount();
|
||||
expect(disconnectSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('minimizes getBoundingClientRect calls during pointer move', () => {
|
||||
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');
|
||||
|
||||
// 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();
|
||||
|
||||
act(() => {
|
||||
const event = new PointerEvent('pointermove', {
|
||||
clientX: 200,
|
||||
clientY: 200,
|
||||
bubbles: true,
|
||||
});
|
||||
window.dispatchEvent(event);
|
||||
});
|
||||
|
||||
// EXPECTATION: It should NOT be called.
|
||||
// This will fail currently.
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
|
||||
unmount();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user