I’ve been trying to write some code which would create dynamic masks per visible area requirements. The API documentation states that mask image dimensions need to be multiples of four (though this does not apply to the visible area within the mask.) In trying to create dynamic masks I kept running up against an odd issue. The masks, though a multiple of four, would have visual artifacts similar to non-four-multiple masks.
I believe that masks need to be a multiple of 8, not 4, and so here is the dynamic mask code.
API mask documentation: http://docs.coronalabs.com/api/library/graphics/newMask.html#bitmap-mask-image-requirements
You’ll need a content image to apply the masking to; I used this one: http://content.screencast.com/users/HoraceBury/folders/Default/media/b9c654a2-9e6f-48ca-aeaf-50bb0d4d595b/test.png
In this code the content image is displayed and increasing sized masks (starting at 81x81) are generated, with the dimensions printed in the console. To generate the next size mask hit the white circle. The green circle is just for my edification while debugging and simply dumps out the current size and it’s remainder value when divided by 4 and 8, as evidence.
main.lua:
[lua]-- filler background colour
local s = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
s:setFillColor( 0, 0, 255 )
local createMask = nil
local size = 80
local text = display.newText(size, display.contentCenterX+200, 50, native.systemFont, 50)
text:setTextColor(0,0,0)
local button = display.newCircle( 50,50,50 )
function button:tap(e)
size = size + 1
text.text = size
createMask(size,size)
end
button:addEventListener(“tap”,button)
local button2 = display.newCircle( 150,50,50 )
function button2:tap(e)
print('GOOD(4,8): ‘…size…’ ‘…(size%4)…’ '…(size%8))
end
button2:setFillColor(0,255,0)
button2:addEventListener(“tap”,button2)
– create content group
local group = display.newGroup()
local content = display.newImage( group, “test.png” )
content.x,content.y = display.contentCenterX,display.contentCenterY
– create mask
createMask = function()
local maskgroup = display.newGroup()
local invisible = display.newRect(maskgroup,0,0,size,size)
local visible = display.newRect(maskgroup,0,0,size-4,size-4)
invisible.x,invisible.y = 0,0
visible.x,visible.y = 0,0
invisible:setFillColor(0,0,0)
maskgroup.x,maskgroup.y = 200,200
print(‘dims’,maskgroup.width,maskgroup.height,size%4,size%8)
display.save( maskgroup, “newmask.png”, system.TemporaryDirectory )
–display.newImage( “newmask.png”, system.TemporaryDirectory )
maskgroup.alpha = .6 – hide the original image of the mask
local mask = graphics.newMask( “newmask.png”, system.TemporaryDirectory )
group:setMask( mask )
group.maskX,group.maskY=display.contentCenterX,display.contentCenterY
maskgroup:removeSelf()
timer.performWithDelay(500,function()
group:setMask(nil)
end,1)
end[/lua] [import]uid: 8271 topic_id: 28419 reply_id: 328419[/import]
