Page 1 of 1

How can i set a background image for a rectangle

Posted: Thu Dec 23, 2021 5:13 pm
by parity
I have this rectangle i drew using this function

Code: Select all

love.graphics.rectangle("line", center[1]+100, center[2]-50, 200, 100, 1, 5)
I made a pattern that i want to set as the background of the rectangle, i know i can just put the image at the same position but i want it to respect the radius, how can i do that

Re: How can i set a background image for a rectangle

Posted: Thu Dec 23, 2021 6:49 pm
by EngineerSmith
Just find the scale ratio and draw by that ratio

Code: Select all

local scaleX = 200/image:getWidth() -- 200 and 100 from rectangle width and height
local scaleY = 100/image:getHeight() -- e.g. 100/200 -> scales by 0.5
love.graphics.draw(image, 0,0, 0, scaleX, scaleY)

Re: How can i set a background image for a rectangle

Posted: Thu Dec 23, 2021 6:52 pm
by BrotSagtMist
What you probably want instead is to use a scissor.
https://love2d.org/wiki/love.graphics.setScissor

Or check again on how to use draw() with quads but that might be a tad harder.

The function to draw rectangles is made to draw rectangles, hard to get more out of it.

Re: How can i set a background image for a rectangle

Posted: Fri Dec 24, 2021 7:38 pm
by zorg
If your issue is that you're drawing a rectangle -with rounded corners-, and you want the image to not show outside of the rounded edges, then it's a bit tricky to accomplish that; my first idea would be to use stencils: draw the rounded rectangle to a canvas with stencils enabled, then use that as a test when drawing the image - only the parts that are covered by the stencil should be drawn.