mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.41k stars 536 forks source link

Interaction with shapes #2330

Open VeyDlin opened 1 year ago

VeyDlin commented 1 year ago

I'm trying to determine which shapes are located by coordinates (mouse click).

I realized that skia does not provide such an API and I need to implement it myself, but I have no ideas on this.

Everything is complicated by the fact that I need to understand exactly that the click occurred on the figure, and not its square hitbox, for example, I can click inside the text with the number "0" and this should not be considered a hit, there are also rectangles with rounded edges, complex curves, different shapes, and there are thousands of them on the canvas

I will be grateful for any advice because right now I don't even know how to Google for a solution to my problem

themcoo commented 1 year ago

Skia is a rendering library, what you are asking for is completely out of scope.

VeyDlin commented 1 year ago

Skia is a rendering library, what you are asking for is completely out of scope.

Yes, I understand that, I just wanted to find out what ways there are in general,

I could create a mathematical model of each figure I draw and count its collision,

or I could add its own canvas to each drawn figure and draw it there again and get a pixel at the right coordinates (if there is such a function in skia at all), then I will get thousands of canvases, or tens of thousands, and maybe it won't even work too slowly,

I could also create a polygon around pixels, for this I also need to somehow get a drawn shape separately from the general canvas, is this possible in skia?

But it still seems to me that there should be an easier way, if skia returned at least some information about the shape that it just rendered, it would be easier, I think

willn1337 commented 1 year ago

you could draw the shapes to a new surface (not rendered to the screen), pass a unique color for each shape, then call hittestsurface.ReadPixels(..., mousePos) and check if the pixel color is a match?

utenma commented 1 month ago

this is just speculation at the moment but representing the UI as a DOM-like tree of System.Drawing.Drawing2D or equivalent for .Net Core objects then mapping the DOM-like tree to skia drawing methods should be one approach

Then on pointer events use

Rectangle.Contains GraphicsPath.IsVisible

For prioritizing elements of top of others, the DOM-like tree need z-index

@VeyDlin issue should be closed and instead a discussion thread in discussions as it's outside skia codebase

wieslawsoltes commented 1 month ago

You can try https://github.com/wieslawsoltes/Draw2D

Aniganesh commented 1 month ago

Hi @wieslawsoltes! I opened your library and it seems the github pages deployment has failed.