Make the Images right for Corona SDK — Image Proportions

When you create a game or a app, you need to handle with lots of Images. Making it proportionate is necessary to make your app looks beautiful and nice. If your image is squeezed then you are messed up.

Make your images proportionate in Corona SDK by using the below methods.

--Function to get the New Height for Given Width
function getNewHeight(image , newWidth) 
    local r = image.width / image.height --4
    local newHeight = newWidth / r --5
    return newHeight --6

--Function to get the New Width for Given Height
function getNewWidth(image , newHeight)
    local r = image.width / image.height
    local newWidth = newHeight * r
    return newWidth

function createAnImage()
    local myImage = display.newImage("myImagesDirectory/MyImages.png") --1
    -- Call this before you are setting the width of the image
    myImage.height = getNewHeight(myImage, display.contentWidth * 0.90)  --2
    --Set the same width you used in the previous line
    myImage.width = display.contentWidth * 0.90  --3

Code Explanation :

1 Create new image

2 Call the newly created function by passing the image and the intended height / width

3 Set the intended height to the image

What actually happening ?

4 creating the ratio of the image width and the height

5 Setting the new height by proportionately increasing based on the width

6 Returning the new height

Happy Coding !

No Comments Yet