Set image width and height

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
roggie
Prole
Posts: 22
Joined: Sat May 31, 2014 2:52 pm
Location: I think it's Earth
Contact:

Set image width and height

Post 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?
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Set image width and height

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Set image width and height

Post 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 )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
roggie
Prole
Posts: 22
Joined: Sat May 31, 2014 2:52 pm
Location: I think it's Earth
Contact:

Re: Set image width and height

Post 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:
Post Reply

Who is online

Users browsing this forum: DTmg, Google [Bot] and 4 guests