Game asset processing3 min read

Canvas Sprite Mask Keeps the Mask Color? Check destination-in

Use the same colored sprite and white circular mask to see which layer each Canvas compositing operator retains.

Canvas Sprite Mask Keeps the Mask Color? Check destination-in
An orange striped tail clipped to the area inside a circular mask
Concept illustration

Your circular mask leaves a white disc where the colored sprite should be. Check which layer the compositor keeps. In Canvas, the source is the draw you are issuing and the destination is what was already on the canvas.

Keep draw order fixed

Start with two empty 64 by 64 canvases. Draw the same fully opaque colored square on each. Make a matching mask bitmap with a white circle and fully transparent pixels outside. In A, set destination-in and draw the entire mask. In B, keep the same order but use source-in.

Sample the center and corners

A should retain the original sprite color at the center. B should show the incoming white mask color. The corners should be transparent in both. Their result alpha is the product of the two alpha values, which matters for partial edge pixels. Sample away from the antialiasing region first.

Restore source-over after masking, or use save and restore around the operation. If the mask cuts out the scene background too, isolate the sprite on its own transparent canvas before combining it with the scene. Prepare a sample sprite in the sprite cutter and verify the compositing sequence in your own export pipeline.

Recommended tools

Ready to try "Canvas Sprite Mask Keeps the Mask Color? Check destination-in"?

Use our online tools to apply this tutorial to your indie game project right away.