Hello. Is there a way to apply a vector mask (shape) to a group?
yes, but i mean VECTOR mask
I believe the masking I mentioned will allow to you use vector shapes - defined in black in white. By vector mask I assume you mean resolution independent like Photoshop vector masks? Can you describe the effect you are trying to attain? There might be several ways to achieve it - textures, containers, canvases (shaders if you are up for that).
Also, will you be applying physics and touch events on the masked objects?
yes, “resolution independent like Photoshop vector masks”
My English is too bad to explain why I need it. I can only say that my masking is dynamic and therefore it is impossible to prepare raster textures in advance.
There is physics, but masking will not affect the change in body shape, and without touch events.
textures - not suitable as they are raster
containers - not suitable as they are strictly rectangular in shape
canvases - I do not understand what you mean
shaders - I have no experience with shaders. Will it be very difficult to do?
https://www.youtube.com/watch?v=flPx2Eh230g - “Use any polygon as a mask on groups, images and any other display objects”
Hi. Interesting timing.
If you didn’t see it, there’s now a mask-type canvas as of build 3544. (This was an “aha!” sort of idea after watching a video posted on Slack, which included some shape-based masking.)
Here’s the test script I provided with the pull request:
--- Canvas mask test. -- -- Permission is hereby granted, free of charge, to any person obtaining -- a copy of this software and associated documentation files (the -- "Software"), to deal in the Software without restriction, including -- without limitation the rights to use, copy, modify, merge, publish, -- distribute, sublicense, and/or sell copies of the Software, and to -- permit persons to whom the Software is furnished to do so, subject to -- the following conditions: -- -- The above copyright notice and this permission notice shall be -- included in all copies or substantial portions of the Software. -- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- -- [MIT license: http://www.opensource.org/licenses/mit-license.php] -- local tex = graphics.newTexture{ type = "canvas", width = 128, height = 128 } local back = display.newRect(0, 0, 128, 128) back:setFillColor(1, 0, 0) local rect = display.newRect(0, 0, 32, 64) rect:setFillColor(0, 0, 1) tex:draw(back) tex:draw(rect) tex:invalidate() local mask = graphics.newTexture{ type = "maskCanvas", width = 512, height = 512 } local mask\_back = display.newRect(0, 0, 512 - 6, 512 - 6) mask:draw(mask\_back) local circles = {} for i = 1, 10 do circles[i] = display.newCircle(0, 0, math.random(10, 35)) circles[i]:setFillColor(0) circles[i].radius = math.random(i \* 10, 170) circles[i].speed = math.random(1, 7) \* .05 mask:draw(circles[i]) end local background = display.newRect(250, 250, mask.width, mask.height) background.fill = { type = "image", filename = tex.filename, baseDir = tex.baseDir } background:setStrokeColor(0, 0, 1) background.alpha = .7 background.strokeWidth = 3 local masked\_group = display.newGroup() local r1 = display.newRect(masked\_group, 250, 350, 150, 200) local r2 = display.newRect(masked\_group, 450, 100, 200, 100) local r3 = display.newRect(masked\_group, 150, 150, 100, 200) masked\_group:setMask(graphics.newMask(mask.filename, mask.baseDir)) masked\_group.maskX, masked\_group.maskY = background.x, background.y local x0 = background.x timer.performWithDelay(50, function(event) background.x = x0 + math.sin(event.time / 800) \* 50 end, 0) timer.performWithDelay(150, function(event) for i, circ in ipairs(circles) do local angle = (event.time \* circ.speed) \* .0035 circ.x, circ.y = math.cos(angle) \* circ.radius, math.sin(angle) \* circ.radius end mask:invalidate("cache") end, 0)
The test has a normal canvas as well, just because I was making sure I didn’t stomp on any of that code.