@vlads, I tried two different approaches to this but without success. Perhaps I am misunderstanding something that you can help me with. I am using Build 2866.
I took the existing Flashlight demo that comes in the Samples folder of Corona SDK and made the following tweaks:
I added this texture:
local tex = graphics.newTexture( { type="canvas", width=200, height=200 } ) local blackRect = display.newRect(0,0,200,200) blackRect:setFillColor(0) tex:draw( blackRect ) -- Create a circle and draw/render it to the texture local glow = display.newCircle( 0, 0, 90 ) glow.fill.effect = "generator.radialGradient" glow.fill.effect.color1 = { 1, 1, 1, 1 } glow.fill.effect.color2 = { 0, 0, 0, 0 } glow.fill.effect.center\_and\_radiuses = { 0.5, 0.5, 0.3, 0.9 } glow.fill.effect.aspectRatio = 1 tex:draw( glow ) -- Schedule texture objects to be rendered to texture before next frame tex:invalidate()
Then I tried each of the following methods in an attempt to set the mask from the texture. (Besides this, I also tried to create a Mask directly from the canvas file but that blows up as previously indicated StarCrunch.)
local mask = display.newRect(0, 0, 200, 200) mask.fill = { type="image", filename=tex.filename, baseDir = tex.baseDir} image:setMask( mask )
local mask = display.newImageRect(tex.filename, tex.baseDir, 200, 200) image:setMask( mask )
In both these cases, the “mask” is simply displayed at the top left corner of the game.
Below is my full main.lua. The resources from the Flashlight sample app are required to make it run.
-- Abstract: Flashlight sample project -- Demonstrates masking an image. The flashlight skews depending -- on location from center. -- -- Version: 1.0 (September 1, 2010) -- -- Sample code is MIT licensed, see https://www.coronalabs.com/links/code/license -- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved. -- -- Supports Graphics 2.0 ------------------------------------------------------------ display.setStatusBar( display.HiddenStatusBar ) local centerX = display.contentCenterX local centerY = display.contentCenterY -- Black background local bkg = display.newRect( centerX, centerY, 768, 1024 ) bkg:setFillColor( 0 ) -- Image to be masked local image = display.newImageRect( "image.png", 768, 1024 ) image:translate( centerX, centerY ) --Texture for mask?? local tex = graphics.newTexture( { type="canvas", width=200, height=200 } ) local blackRect = display.newRect(0,0,200,200) blackRect:setFillColor(0) tex:draw( blackRect ) -- Create a circle and draw/render it to the texture local glow = display.newCircle( 0, 0, 90 ) glow.fill.effect = "generator.radialGradient" glow.fill.effect.color1 = { 1, 1, 1, 1 } glow.fill.effect.color2 = { 0, 0, 0, 0 } glow.fill.effect.center\_and\_radiuses = { 0.5, 0.5, 0.3, 0.9 } glow.fill.effect.aspectRatio = 1 tex:draw( glow ) -- Schedule texture objects to be rendered to texture before next frame tex:invalidate() -- Masks local mask = graphics.newMask( "circlemask.png" ) --image:setMask( mask ) local mask2 = display.newRect(0, 0, 200, 200) mask2.fill = { type="image", filename=tex.filename, baseDir = tex.baseDir} image:setMask( mask2 ) -- Create display object with texture as contents local mask3 = display.newImageRect(tex.filename, tex.baseDir, 200, 200) mask3.x = display.contentCenterX mask3.y = display.contentCenterY --image:setMask( mask3 ) local radiusMax = math.sqrt( centerX\*centerX + centerY\*centerY ) function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.maskX t.y0 = event.y - t.maskY elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). local maskX = event.x - t.x0 local maskY = event.y - t.y0 t.maskX = maskX t.maskY = maskY -- Stretch the flashlight as it moves further away -- from the screen's center local radius = math.sqrt( maskX\*maskX + maskY\*maskY ) local scaleDelta = radius/radiusMax t.maskScaleX = 1 + scaleDelta t.maskScaleY = 1 + 0.2 \* scaleDelta -- Rotate it appropriately about screen center local rotation = math.deg( math.atan2( maskY, maskX ) ) t.maskRotation = rotation elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end image:addEventListener( "touch", onTouch ) -- By default, the mask will limit the touch region to areas that lie inside -- both the mask and the image being masked. We can override this by setting the -- isHitTestMasked property to false, so the touch region lies inside the entire image. image.isHitTestMasked = false -- Display instructions local labelFont = native.systemFont local myLabel = display.newText( "Touch circle to move flashlight", centerX, 200, labelFont, 34 ) myLabel:setFillColor( 1, 1, 1, 180/255 )