Masks
Back to the reference

Masks

Keeping where something is drawn to a rectangle or to the shape of another object: hard, inverted or soft.

5 symbols

useMaskfunctionhooknacatamalon

useMask(options: TMaskOptions) => TMask

Makes a mask: what keeps objects, and everything under them, to a rectangle or to the shape of another object, so nothing of them is drawn, or touched by the pointer, outside it. It masks nothing until it is added to something, and can be added to as many objects as want it: a map and its enemies, and not the HUD.

  • rect keeps to a rectangle in the space of each object it is added to. Square to the screen it costs nothing at all.
  • by keeps to the shape of another object: everything that object and the ones under it draw, sprites by their pictures, shapes, texts, added together. That object is not drawn itself unless showShape, and it moves, turns, grows and is tweened like any other.
  • channel: 'luminance' reads a black and white cut-out: white shows, black cuts.
  • invert draws only outside the shape. soft lets the shape through as much as it is there, a soft edge or a fade, and is the only kind that costs a picture of its own; the others cut hard, in or out at half way, which is what pixel art wants.

An object has one mask. One mask inside another keeps to both: put each on its own object, one inside the other. zIndex sorts what is masked as it always did. A mask goes away with the object it was asked for in, and everything it masked is drawn whole again.

Parameters

optionsTMaskOptions

What it keeps to (rect or by) and how.

Example

const Flashlight = () => {
    useTransform({ x: 100, y: 80 });
    createSprite({ texture: 'cone' });
};

const Ground = () => {
    createSprite({ texture: 'cave' });
};

const Bats = () => {
    createSprite({ texture: 'bat', transform: { x: 200, y: 60 } });
};

export const Cave: TSceneFn = () => {
    const light = useSpawn(Flashlight)();
    const mask = useMask({ by: light, soft: true });
    mask.add(useSpawn(Ground)());
    mask.add(useSpawn(Bats)());
    useTween()({ from: 40, to: 300, duration: 2, yoyo: true, loop: true, onUpdate: (x) => { light.transform!.x = x; } }).play();
    return createScene();
};

const Inventory = () => {
    useMask({ rect: { x: 0, y: 0, width: 200, height: 120 } }).add(useSelf());
    createSprite({ texture: 'items' });
};
View source · hooks/mask/use_mask.ts:64

TMasktypenacatamalon

A mask: what keeps the objects it is added to, and everything under them, to a rectangle or to the shape of another object. Made with useMask, it masks nothing until it is added to something, and everything it says can be changed at any moment, for all of them at once.

Properties

type'mask'
idstring
rectTMaskRect | null

The rectangle it keeps to, or null. A shape (by) wins over it.

byTBox | null

The object whose shape it keeps to, or null.

softboolean
invertboolean
showShapeboolean
enabledboolean
add
remove
has
boxestypeOperator

Every object it masks, in the order they were added.

View source · mask/types/t_mask.ts:79

TMaskChanneltypenacatamalon

TMaskChannel: 'alpha' | 'luminance'

Which part of a shape's picture says where it lets things through: its 'alpha' (what is transparent cuts), or its 'luminance' (white shows, black cuts), for a cut-out painted in black and white.

View source · mask/types/t_mask.ts:12

TMaskOptionstypenacatamalon

How a mask is made: useMask's options. A mask keeps things to a rect, or to the shape of another object (by), one of the two.

Properties

rectoptionalTMaskRect

A rectangle to keep things to, in the space of each object the mask is added to. When it stays square to the screen it costs nothing at all.

byoptionalTBox

The object whose shape keeps things in: everything it draws, and everything under it, added together. It is not drawn itself (see showShape), and it moves, turns, grows and is tweened like any other object.

channeloptionalTMaskChannel

Which part of the shape's pictures says where it lets things through. 'alpha' by default.

softoptionalboolean

false, the default, cuts hard: a pixel is in or out, at half way, which is what pixel art wants. true lets the shape through as much as it is there, a soft edge or a fade, and is the one kind of mask that costs a picture of its own.

invertoptionalboolean

Draws only outside the shape instead of inside. false by default.

showShapeoptionalboolean

Draws the shape as well as masking with it: a window's frame that also keeps in what is inside. false by default.

enabledoptionalboolean

Whether it masks at all. true by default; false lets everything through for a while without taking the mask away.

View source · mask/types/t_mask.ts:32

TMaskRecttypenacatamalon

A rectangle in the space of the object it masks: where its own x: 0, y: 0 is, turned and grown with it.

Properties

xnumber
ynumber
widthnumber
heightnumber
View source · mask/types/t_mask.ts:22