Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setMouse / Faulty mouse coordinates #76

Open
moritzsalla opened this issue Nov 3, 2021 · 2 comments
Open

setMouse / Faulty mouse coordinates #76

moritzsalla opened this issue Nov 3, 2021 · 2 comments

Comments

@moritzsalla
Copy link

moritzsalla commented Nov 3, 2021

The .setMouse() function returns faulty values. Please excuse if I am using the function incorrectly.

For example, when passing sandbox.useMouse({ x: 0, y: 0 }) and logging sandbox.uniforms, u_mouse returns as follows:

{
    "name": "u_mouse",
    "type": "vec2",
    "value": [
        1.785604900459418,
        149.35220838052095
    ],
    "method": "uniform2f",
    "location": {}
}

In case the function maps the coordinates to the canvas element, here is my react implementation:

const Canvas = () => {
  const canvasRef = useRef();

  const { current } = canvasRef;

  useEffect(() => {
    if (current) {
      const sandbox = new GlslCanvas(current);
      sandbox.load(program.frag);

      sandbox.setUniform('u_texture', testImg);
      console.log(sandbox);
      sandbox.setMouse({ x: 0, y: 0 });
    }
  }, [current, mouse]);

  return <canvas ref={canvasRef} />;
};
@moritzsalla moritzsalla changed the title Mouse coordinates are wrong FaultymMouse coordinates Nov 6, 2021
@moritzsalla moritzsalla changed the title FaultymMouse coordinates Faulty mouse coordinates Nov 6, 2021
@moritzsalla moritzsalla changed the title Faulty mouse coordinates setMouse / Faulty mouse coordinates Nov 6, 2021
@eriksachse
Copy link

Same issue. Any good workaround?

@moritzsalla
Copy link
Author

Same issue. Any good workaround?

Couldn't make it happen with glsl-canvas but made a custom implementation by passing a u_pointer uniform.

import Canvas from 'glsl-canvas-js/dist/cjs/canvas/canvas'
import { useMove } from '@use-gesture/react'

const canvasElemRef = useRef()
const shaderProgramRef = useRef()
const matchesFine = useMatchMedia(MEDIA_QUERIES.fine)

useEffect(() => {
  const canvasElem = canvasElemRef.current

  // Check if a canvas element exists...
  if (!canvasElem) return

  // instantiate glsl-canvas
  shaderProgramRef.current = new Canvas(canvasElem, {})

  // cleanup glsl-canvas
  return () => {
    shaderProgramRef.current.pause()
    shaderProgramRef.current = null
  }
}, [canvasElemRef.current])

// https://use-gesture.netlify.app/docs/gestures/
useMove(
  ({ xy: [x, y] }) => {
    // If the program has not been initialised yet, return early.
    if (!canvasElemRef.current) return

    const normalisedCoords = [x, window.innerHeight - y * 0.5]

    canvasElemRef.current.setUniforms({ u_pointer: normalisedCoords })
  },
  {
    // ssr check - I'm using next.js
    target: typeof window === 'undefined' ? null : window,
    // ignore coarse pointers
    enabled: matchesFine,
  },
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants