WebGL-Fluid-Simulation icon indicating copy to clipboard operation
WebGL-Fluid-Simulation copied to clipboard

Get canvas mouse move event listener function to work when text is above it, keep text selectable.

Open ChrisCalabrese opened this issue 3 years ago • 13 comments

Hi everyone, I have a canvas that is running the Web GL Fluid particle animation. The problem I'm having is that if the canvas is below a object the mousemove event for the canvas stops firing. If I use css such as "pointer-events: none;" on the object that's covering the canvas the animation will work but then I can't select text or use buttons in the object. How do I get the canvas to fire its mouseover animation even when objects are covering it all while being able to interact with the those objects. Is there is a way to add to or adjust the canvas mouse event listener so it fires when objects are over it? This is the current code for the canvas mousemove listener:

canvas.addEventListener('mousemove', function (e) { var pointer = pointers[0]; if (!pointer.down) return; var posX = scaleByPixelRatio(e.offsetX); var posY = scaleByPixelRatio(e.offsetY); updatePointerMoveData(pointer, posX, posY); console.log('over the canvas'); });

Here are examples sites that have canvas mouseover interaction below selectable text:
https://advanced.team/ https://www.vantajs.com/

Sample in code pen: https://codepen.io/chriscalabrese/pen/RwjmNZW

Thanks!

ChrisCalabrese avatar Mar 09 '22 18:03 ChrisCalabrese

You can alter the script.js to listen to mousemove on the overlay layer instead of the canvas

lillichoung avatar Mar 10 '22 20:03 lillichoung

I had the same question and someone helped me. You can check out his work.

https://codepen.io/Sempervivum/pen/bGYPXJg

mrtsglk avatar Mar 11 '22 17:03 mrtsglk

I had the same question and someone helped me. You can check out his work.

https://codepen.io/Sempervivum/pen/bGYPXJg

Thanks I'll try to leverage this. Very much appreciated.

ChrisCalabrese avatar Mar 12 '22 03:03 ChrisCalabrese

Thanks for the replies! I managed to get the particles to animate below the page elements using the JavaScript and min .js file @networkmrt provided. However, I'm not able to get the event listener to work with the original version of the fluid GL JS file. How do I tweak the code so it works with the original script.js file? I can't modify the webgl-fluid.umd.min.js because its minified.

This works:

<script src="https://unpkg.com/[email protected]/dist/webgl-fluid.umd.min.js">
</script>

window['webgl-fluid'].default(document.querySelector('canvas'));
	
document.querySelector('#wrap').addEventListener('mousemove', event => {
    console.log('move');
    newEvent = new event.constructor(event.type, event);
    document.querySelector('canvas').dispatchEvent(newEvent);
});`

This does not work:

<script src="myurl/files/fluid/script.js"></script>

window['script'].default(document.querySelector('canvas'));
	
document.querySelector('#wrap').addEventListener('mousemove', event => {
    console.log('move');
    newEvent = new event.constructor(event.type, event);
    document.querySelector('canvas').dispatchEvent(newEvent);
});

What am I doing wrong? :S

ChrisCalabrese avatar Mar 12 '22 15:03 ChrisCalabrese

Have you discovered what was your problem?

EscVM avatar Apr 12 '22 18:04 EscVM

您好,我已经收到你的信件!我会及时阅读并作出回应!

MiracleCK avatar Apr 12 '22 18:04 MiracleCK

So I spent yesterday playing with the code trying to achieve this, and I think I achieved it : 1st thing : listen to window instead of canvas : window.addEventListener(mousemove, function) "2nd thing : change the event property from offsetX and offsetY to clientX and clientY Next you will have to apply some changes to the function ### updatePointerMoveData :

  • change the canvas.width and height in these two lines : pointer.texcoordX = posX / canvas.width; pointer.texcoordY = 1.0 - posY / canvas.height; it should be something like this : pointer.texcoordX = posX / (canvas.width/2); pointer.texcoordY = 1 - posY / (canvas.height/2); This worked for me because i am setting the canvas size to 100vw and 100vh these were all the changes i made and it worked fine for me

anasnaciri avatar May 24 '22 13:05 anasnaciri

您好,我已经收到你的信件!我会及时阅读并作出回应!

MiracleCK avatar May 24 '22 13:05 MiracleCK

This animation not working on mobile, how to solve this issue? any reference please send me urgent

gurdeepdesigner avatar Jul 31 '23 11:07 gurdeepdesigner

您好,我已经收到你的信件!我会及时阅读并作出回应!

MiracleCK avatar Jul 31 '23 11:07 MiracleCK

This animation not working on mobile, how to solve this issue? any reference please send me urgent. Anyone please help me out

gurdeepdesigner avatar Aug 01 '23 04:08 gurdeepdesigner