react-native-skia icon indicating copy to clipboard operation
react-native-skia copied to clipboard

Touch Handling on each path/group inside a canvas ?

Open BubbleTrouble14 opened this issue 2 years ago • 6 comments

What would be the best approach to handle touch events on specific paths of a canvas ? As at the moment only the full canvas listens for touch events. Would I have to use the canvas touch handler to get the x and y postion and loop through all the path bounds to get the one which it is inside ? For example a simple barchart where each bar can be clickable to receive its value or even animate it ?

BubbleTrouble14 avatar May 26 '22 11:05 BubbleTrouble14

I'm also looking into this. As I understand it is impossible right now. The only react native component is Canvas and it's the only one that has a touch hook. Would love to know if this feature is already on the roadmap. It would make RNS 2x more powerful.

gverri avatar May 26 '22 17:05 gverri

Yea totally. Atm the only working solution I have is using a simple useState hook with the Touch Handler from the canvas and just loop through all the shapes. And if the the x and y postion is inside the bounds of one of the shapes update the state hook.

BubbleTrouble14 avatar May 26 '22 17:05 BubbleTrouble14

The drawing primitives inside the canvas are not components as we're used to in React - they are just wrappers ending up as drawing instructions resulting in Skia rendering rectangles, circles, paths etc on screen.

We've done something similar to what @BubbleTrouble14 did, enumerating our elements and checking if the current touch point is inside the element's drawing area.

We hope we'll be able to provide a good solution for this in the future that will make it easier to add touch handling to specific areas in the Canvas that you can declaratively set up.

chrfalch avatar May 26 '22 19:05 chrfalch

Okay thanks for the reply. I'll just keep my current solution then.

BubbleTrouble14 avatar May 26 '22 19:05 BubbleTrouble14

Should we close this one, @wcandillon? Ref discussion we had?

chrfalch avatar May 31 '22 08:05 chrfalch

Are there any updates? 🙂 I'm implementing multiple draggable backdropBlurs which require a single Image. and I want to attach the touch handler only to itself, not the whole parent. The result will be like Instagram tags.

AlirezaHadjar avatar Jun 23 '22 12:06 AlirezaHadjar

Would be awesome to have touch support per "layer" (path, image, etc). Or alternatively an elementFromPoint kinda function returning the topmost [Element] at x,y.

clemensmol avatar Oct 25 '22 07:10 clemensmol

Referring to the comment I made about above - the primitives aren't real components, just an abstract representation of a part of the drawing and hence does not contain any common information about position etc.. I think the best solution would be to create your own such functions depending on the specific use case you have :)

chrfalch avatar Oct 25 '22 07:10 chrfalch

would be interested in this as well!

KrisLau avatar Dec 08 '22 21:12 KrisLau

+1

aleksey-mukho avatar Jan 28 '23 21:01 aleksey-mukho

Maybe this one helps?

chrfalch avatar Jan 29 '23 12:01 chrfalch

@chrfalch Are there any plans to include that as a part as a part of react-native-skia or to implement something similar? I would be worried about adding a library that isn't popular/have a community around it because of the worry that maintenance would be dropped and the incompatibility between versions. It seems like it would be a great addition to skia

EDIT: Just saw https://github.com/Shopify/react-native-skia/pull/517 but it's closed I couldn't seem to find a successor PR

KrisLau avatar Feb 09 '23 16:02 KrisLau

As we've explained before, this is a hard topic that we've decided is out of scope for what we want to do (see previous comment here).

The main reasons being: Elements in a Skia drawing are NOT separate component receiving mouse or touch input - the Canvas is the component that handles input.

Creating a generic function for returning the bounds of Skia elements are hard, since elements can be translated, clipped and rendered in such a way that we can't calculate where its actual positions are with accurate results..

Our recommendation Please analyse your specific use case and use the useTouchHandler hook to implement your own logic - you are to one that knows your requirements in each of these different areas - so you are the best one to implement a system for implementing a detector for the bounds for your drawing elements.

References You can look at the project referenced above https://github.com/enzomanuelmangano/react-native-skia-gesture to learn how this can be done for individual drawing elements and either use this library or adapt the code to your use.

chrfalch avatar Feb 10 '23 09:02 chrfalch