Search code examples
luacoronasdkmasking

Scaling masked images in Corona SDK


I have a masked image like so:

local mask = graphics.newMask("1_mask.png")
local image = display.newImage("1.png")
image:setMask(mask)
image.maskScaleX = image.xScale
image.maskScaleY = display.yScale
image.maskX = image.contentWidth/2
image.maskY = image.contentHeight/2

If I remove the code after setMask, the image appears fine, although it's quite too big so I need to scale it. However, if I trying to scale the image, it looks broken and it's way too small. I'm trying to avoid having to resize my images so it's my last option so how can I scale my image without it looking broken or ending up way too small?


Solution

    1. You can shorten your code by using the following assignment schemes:

      local mask, image = graphics.newMask("1_mask.png"), display.newImage("1.png")
      
    2. There is a scale() method available in Corona. You can use it to set the dimensions as a percentage of original. For eg. to scale the image down to 50% of original

      image:scale( 0.5, 0.5 )
      
    3. To preserve mask scale same as that of the display object, you need to set maskScaleX and maskScaleY properties too.

      image.maskScaleX, image.maskScaleY = 0.5, 0.5