2.5 KiB
2.5 KiB
Fluid Effect Debugging Analysis
Problem Statement
The fluid effect (WebGL canvas with mouse interaction) was not working properly over text areas and other HTML elements on the page.
Root Cause Analysis
How Canvas Events Work
Canvas elements only capture mouse events when:
- The mouse pointer is directly over the canvas element itself, OR
- The canvas has
pointer-events: noneCSS property (allowing events to pass through), OR - Event listeners are attached at a higher level (document/body) and manually checked for intersection with canvas bounds.
Current Implementation Issue
// OLD CODE -only works when mouse is over canvas
canvas.addEventListener("mousemove", function(e) {
// Uses e.offsetX and e.offsetY - only available on canvas element
});
Expected Behavior
The fluid effect should respond to mouse movement anywhere on the page, including over text elements.
Solution Implemented
Key Changes Made in fluid.js:
-
Changed event listener target from canvas to document:
// NEW CODE - works globally document.addEventListener("mousemove", function(e) { // Manually calculate position relative to canvas }); -
Updated coordinate calculation to account for canvas position:
// OLD: Uses offsetX/Y (canvas-relative) pointers[0].dx = (e.offsetX - pointers[0].x) * 10.0; // NEW: Uses clientX/Y plus manual canvas positioning pointers[0].x = e.clientX - canvas.offsetLeft; pointers[0].y = e.clientY - canvas.offsetTop;
Verification Steps
After implementing the fix, verify that:
- ✅ Fluid effect works when mouse is over the background/text areas
- ✅ Color transitions between brand colors still function correctly
- ✅ No JavaScript errors in browser console
- ✅ Mobile touch events continue to work (already implemented)
Files Modified
fluid.js:372-395- Updated mousemove event listener
Technical Details
Event Propagation Model
The solution leverages event bubbling:
- Mouse move events bubble up from target element to document root
- By attaching the listener at the
documentlevel, we capture ALL mouse movements - We manually check if coordinates intersect with canvas bounds (via canvas.offsetLeft/Top)
Coordinate Transformation
clientX/clientY: Absolute page coordinatescanvas.offsetLeft/canvas.offsetTop: Canvas position relative to viewport- Result: Accurate positioning within canvas regardless of mouse location on page