Skip to content

Adding offsetPos to Touch.Event #13

@joakin

Description

@joakin

Hi!

Thanks for the nice library 💟

I was using it to make a small drawing app, and used Mouse and Touch separately, as I didn't want to deal with polyfills at this time.

When using Mouse, I used .offsetPos, really easy to do, great success.

When I went in to add touch compatibility, I had a harder time. I tried using .clientPos which is what the docs mention you should be using probably most of the time, but that was giving me the coordinates for the viewport instead of relative to the element like offsetPos does on Mouse. So when I scrolled, the coordinates would be off and painting would be broken by whatever scroll offset happened.

I did a bunch of digging and MDN docs looking to see if I would be able to handle this within Elm and a custom decoder, and I found out a way to get the equivalent of offsetPos for the Touch.Event by decoding the offset of the element the events were bound on from event.target.

It was quite hard to figure out how to use custom decoders with the package, so I ended up copying parts of the source on Touch.elm and manually using onWithOptions.

In the end I managed to get it to work, you can see an example here:

I tested it on Chrome, Safari iOS and Chrome Android, so it seems like the main mobile browsers work fine with it.

I was wondering if you would be interested in a PR that adds .offsetPos to the touch events, as I found it very useful, and I imagine that it would be very used for many things like it is on the Mouse package.

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions