memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 770 forks source link

Problem of nvgImagePattern #629

Closed BusyStudent closed 5 months ago

BusyStudent commented 2 years ago

It there any way to use a part of image as pattern? not the whole image.

mulle-nat commented 2 years ago

That's pretty vague. The answer is probably NO.

I assume you mean you want to use only a certain rectangle of an image as the fill ? You could do this by "zooming" into the imagePattern, with the nvgImagePattern function like in this test:

grafik

The math to get this to do exactly what you want, might be too tricky to be useful though.

Also it may not look so great. In this test case, there are non-transparent pixels in the image borders. So the clamping of OpenGL makes the ugly green line.

You couldn't get a replicating pattern out of the image, as NVG_IMAGE_REPEATX|NVG_IMAGE_REPEATY applies to the whole image.

At least that's how I understand it. I am not a graphics authority :smile:


The header declaration of the function is misleading, as ex ey should be w h.

NVGpaint nvgImagePattern(NVGcontext* ctx, float ox, float oy, float ex, float ey,
                         float angle, int image, float alpha);

As they are in the source file file.

NVGpaint nvgImagePattern(NVGcontext* ctx,
                                float cx, float cy, float w, float h, float angle,
                                int image, float alpha)
BusyStudent commented 2 years ago

Yes. Using zooming is the way I was using for slove it.too tricky

Maybe we should add a api like nvgSubImagePattern @memononen ?

Is it possible to implement it in nanovg?

mulle-nat commented 5 months ago

Just for posterity, it is possible to do it and I have done it. But it is fairly complicated. Basically you have to compute the inverse transform of what nanovg will be doing and then modify it. It's part of MulleUI, which should see the light of day this century...