Transform any polygon into a mask with Corona SDK

This bit comes directly from our library.
Instead of creating each needed mask manually, we found it easier to simply choose a display object, a polygon, and mask the object using the chosen polygon. We would then either run the app as is, or go into our documents directory and take the created mask so that we could use it anywhere.

If you need to mask a single image, it’s still easier to simply fill the polygon with the image itself with Graphics 2.0.
But for groups of objects, animated sprites and the likes, this made everything so much easier (;

To use it, you just need to require our library, and call the proper function, passing a display object, the polygon you wish to use as a mask, and a name for the mask that will be created.


Get the project from here.


  1. Hi. Thanks for sharing this masking code. I’ve tried to use it and am finding problems due to the mask file saved out not being divisible by 4. The problem can be reproduced on the corona simulator when viewing on different dives other than the default screen dimensions. I think it has something to do with the issue discussed here:
    Do you have any suggestions?

    • Most probably you’re right. We usually use 320×480 as the base res, which would explain why on iPad Retina this snippet seems to have troubles.
      Unfortunately, unless Coronalabs decides to make it more precise (not sure if it’s even possible), the snippet is more limited (you would have to use it just on a certain range of devices).
      We’ll look into it to check wether or not there’s another way to achieve it (;