Masking or stenciling using cc.Graphics

I need to use a cc.Graphics contour “path” to mask or stencil another sprite or group or sprites like so:

How can I do this within Cocos Creator?

I’ve done similar stuff to get a rounded corner mask. It was done by extending the stock CC.Mask and adding my custom code in it. Here’s a quick how-to:

Extended class header:

let MaskExtended = cc.Class({
extends: cc.Mask,

Exporting it:

module.exports = MaskExtended;

You most probably only need to add more code in the function _refreshStencil() and of course a new option to the type property.

1 Like

Thanks @jake72, I’ll let you know how it goes.

It looks like I can just call setStencil() directly inside my overloaded _refreshStencil() passing in the cc.Graphics object, but I’m still checking the type hierarchy. Will keep you posted.

This example use stencil with cc.Graphics, but extends cc.Mask is also a good way.

1 Like

Thanks, @jyinkailej !!