Page 1 of 1

Set image width and height

Posted: Sun Mar 01, 2015 3:09 pm
by roggie
So, say I have an image that is 30 in width and 20 in height. Is it possible to give it a new width and height in love2d. So I could make it 100 in width and 10 in height?

Re: Set image width and height

Posted: Sun Mar 01, 2015 3:30 pm
by micha
You can make the image have any size by scaling (=stretching) it. For example, if you have a 20 by 20 image and you call this:

Code: Select all

love.graphics.draw(image,0,0,0,2,3)
Then the image will be stretched by factor 2 in x direction and by factor 3 in y direction. So it will have a size of 40 by 60.

Re: Set image width and height

Posted: Sun Mar 01, 2015 3:31 pm
by davisdude
Yes.

Code: Select all

function getImageScaleForNewDimensions( image, newWidth, newHeight )
    local currentWidth, currentHeight = image:getDimensions()
    return ( newWidth / currentWidth ), ( newHeight / currentHeight )
end
Then, you can do this:

Code: Select all

img = love.graphics.newImage( 'pic.png' ) -- 30 x 20

-- Say you want to make it 100 x 10
local scaleX, scaleY = getImageScaleForNewDimensions( img, 100, 10 )

-- In love.draw
love.graphics.draw( img, x, y, rotation, scaleX, scaleY )

Re: Set image width and height

Posted: Sun Mar 01, 2015 3:38 pm
by roggie
davisdude wrote:Yes.

Code: Select all

function getImageScaleForNewDimensions( image, newWidth, newHeight )
    local currentWidth, currentHeight = image:getDimensions()
    return ( newWidth / currentWidth ), ( newHeight / currentHeight )
end
Then, you can do this:

Code: Select all

img = love.graphics.newImage( 'pic.png' ) -- 30 x 20

-- Say you want to make it 100 x 10
local scaleX, scaleY = getImageScaleForNewDimensions( img, 100, 10 )

-- In love.draw
love.graphics.draw( img, x, y, rotation, scaleX, scaleY )
Thanx, this is exactly what I'm looking for :awesome: