I’m no artist so I like to generate my inner images dynamically, at one time. This is usually fine for most images and even effects because of the power of the Corona engine, but one thing I am having trouble with recently is dynamically generated masks. This did not used to be a problem at all.
While I understand that masks need to be a multiple of 4 in both dimensions and that dynamic scaling will make this tricky, I refuse to believe that it is not possible (because I’ve done it.)
So, here is my current code as a boiled down version…
config.lua:
application = { content = { width = 768, height = 1024, scale = "letterbox", xAlign = "left", yAlign = "top", antialias = true, imageSuffix = { ["-x2"] = 2, ["-x4"] = 4, }, } }
main.lua:
display.actualCenterX, display.actualCenterY = display.actualContentWidth/2, display.actualContentHeight/2 local maskgroup = display.newGroup() maskgroup.x, maskgroup.y = display.actualCenterX, display.actualCenterY display.newRect( maskgroup, 0, 0, 400, 400 ).fill = {0,0,0} display.newRoundedRect( maskgroup, 0, 0, 350, 150, 35 ).fill = {1,1,1} display.newText{ parent=maskgroup, fontSize=100, text="SAVE" }.fill = {0,0,0} display.save( maskgroup, { filename="mask.png", baseDir=system.CachesDirectory, backgroundColor={0,0,0,0} } ) maskgroup:removeSelf() maskgroup = nil local g = display.newGroup() g.x, g.y = display.actualCenterX, display.actualCenterY display.newRect( g, 0, 0, 500, 500 ).fill = {1,0,0} local mask = graphics.newMask( "mask.png", system.CachesDirectory ) g:setMask( mask )
What I want this code to do is render a big black rectangle with a white, rounded rectangle inside, with black text “SAVE” inside that. It should then save that as an image, remove the original group and load the saved image as a mask, applying it to a red rectangle. The result should be a red pill shape with the word “SAVE” being punched out.
Currently, the mask distorts horribly when loaded and it does not appear to matter how I adjust the size of the outer black rectangle to get the mask to fit the requirements.
Does anyone have a solution for this or a better way to do it - which does not involve pre-rendering the mask image?