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

More robust calculation of touch/click position relative to canvas #13

Open
pardo-bsso opened this issue Jul 10, 2020 · 3 comments
Open

Comments

@pardo-bsso
Copy link

Hi @bobboteck , thanks for this library.

While trying it inside a page with a complex layout, using canvas.offsetParent.offsetLeft (and Top) inside of onTouchMove() and onMouseMove() reported (correctly) the offsets for the closest positioned parent element.

However, its offsetTop was 0 but the event.pageY was something like 400 and that made the Y axis of the joystick unusable.

|-------------------------------------|
|    offsetParent with offsetTop=0    |  
|                                     |  
|                                     |
|    |---------------------------|    |  
|    |  joy div, top ~ 400       |    |  
|    |                           |    |  
|    |   ---------------------   |    |  
|    |   |   joy canvas      |   |    |  
|    |   |                   |   |    |  
|    |   |                   |   |    |  
|    |   |-------------------|   |    |  
|    |                           |    |  
|    |---------------------------|    |  
|                                     |
|-------------------------------------|

Nowadays most browsers support the clientX and clientY properties on events that report the desired value (see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/offsetX and related)

We ended up patching the methods like:

  function onMouseMove(event) 
  {
    if(pressed === 1)
    {
      movedX = event.offsetX;
      movedY = event.offsetY;
      // Delete canvas
      context.clearRect(0, 0, canvas.width, canvas.height);
      // Redraw object
      drawExternal();
      drawInternal();
    }
  }

  function onTouchMove(event)
  {
    // Prevent the browser from doing its default thing (scroll, zoom)
    event.preventDefault();
    if(pressed === 1 && event.targetTouches[0].target === canvas)
    {
      movedX = event.targetTouches[0].offsetX;
      movedY = event.targetTouches[0].offsetY;
      // Delete canvas
      context.clearRect(0, 0, canvas.width, canvas.height);
      // Redraw object
      drawExternal();
      drawInternal();
    }
  } 

and that made it work like a charm.
I don't know if you had other reason for the original code but this is related to issues like #10, #11 and #12.

Thanks

@bobboteck
Copy link
Owner

Hi @pardo-bsso, thanks for your suggestion, I will try the code in the test page.
I like your code that is more clean.

@bobboteck bobboteck self-assigned this Jul 24, 2020
@bobboteck bobboteck added this to In progress in ISSUE Traking Jul 24, 2020
@bobboteck
Copy link
Owner

Hi @pardo-bsso, I have read the documentation you have indicated, but as reported in this link it is a Working Draft, so for now it is not definitive. For this reason, even if the code would be much cleaner, I don't think I will adopt this solution for the moment.

I have tried the code you suggested, and it work well in desktop browser, but it doesn't work on mobile device.

You have tested it on mobile?
I tried looking for information on the documentation about offsetX properties of the Touch object (reference here), but I can't find anything.

@bobboteck bobboteck moved this from In progress to Suspended in ISSUE Traking Oct 2, 2020
@fbrnc
Copy link

fbrnc commented Apr 12, 2022

I'm experiencing the same issue (both with mouse and touch on desktop and mobile): Unless the page is scrolled all the way to the top the joystick keeps dragging to the bottom.

Adding this line to both onTouchMove and onMouseMove right before deleting the canvas works for me:

movedY -= window.pageYOffset;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
ISSUE Traking
  
Suspended
Development

No branches or pull requests

3 participants