How can i set a background image for a rectangle

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
parity
Prole
Posts: 1
Joined: Thu Dec 23, 2021 5:08 pm

How can i set a background image for a rectangle

Post 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
User avatar
EngineerSmith
Prole
Posts: 38
Joined: Thu Dec 02, 2021 11:38 am
Contact:

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

Post 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)
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests