Filters
Any node, a group included, can go through filters: a list of effects applied to the node and its children as one picture.
import { blur, brightness, dropShadow, grayscale, tint } from '@fabiplay/engine'
card.filters = [dropShadow(0, 8, 6, 'rgba(0,0,0,0.35)')]background.filters = [blur(6), brightness(0.8)]lockedLevel.filters = [grayscale()]Script-tag games use the same functions on the global: FabiEngine.blur(6).
The filters
Section titled “The filters”| Filter | What it does | Parameters you can change |
|---|---|---|
tint(color, amount = 1) |
mixes the colours towards color (1: a flat silhouette). A hit flash, a frozen look. Unlike node.tint (a multiply) it can make dark pixels light. |
amount, color |
brightness(v) |
1 unchanged, 0 black, above 1 brighter | amount |
contrast(v) |
1 unchanged, 0 flat grey, above 1 more | amount |
saturate(v) |
1 unchanged, 0 grey, above 1 more vivid | amount |
grayscale(amount = 1) |
grey, 0–1 | amount |
sepia(amount = 1) |
old-photo brown, 0–1 | amount |
hueRotate(deg) |
turns hues around the colour wheel | angle (also amount) |
invert(amount = 1) |
negative, 0–1 | amount |
colorMatrix(m) |
any 4×5 colour matrix (20 numbers, as SVG feColorMatrix) |
values (change numbers in place) |
blur(radius) |
a Gaussian blur; radius is its standard deviation in stage units, like CSS blur(). It reaches about 3 × radius |
radius |
dropShadow(dx, dy, blur, color) |
a shadow of the node’s shape, offset, blurred (σ, stage units), in a colour with its alpha | dx, dy, blur, color, opacity |
shader({ fragment, … }) |
your own GLSL: WebGL only, see Custom shaders | uniforms, padding |
The colour filters use the matrices of the CSS / SVG Filter Effects spec, so they look like the CSS filters of the same name.
Changing and animating them
Section titled “Changing and animating them”A filter’s parameters are plain fields: set them any time, or tween them. Nothing is allocated per frame.
const flash = tint('#ffffff', 0)enemy.filters = [flash]
function hit() { engine.tween(flash, { amount: 1 }, 0.05).then(() => engine.tween(flash, { amount: 0 }, 0.25))}filter.enabled = falseskips a filter without taking it out of the list (cheaper than rebuilding the list).node.filters = null(or[]) removes them all. A node whose filters are all disabled costs nothing extra.- The same filter object can be shared by many nodes: changing it changes them all.
- Order matters:
[blur(4), grayscale()]and[grayscale(), blur(4)]differ where colours bleed.
How it works, and what it costs
Section titled “How it works, and what it costs”A filtered node is drawn into an offscreen layer, then composited through its filters. Only the area the node covers (plus what blurs and shadows reach) is processed, not the whole screen.
| Filters | WebGL | Canvas 2D |
|---|---|---|
any run of colour filters (tint … colorMatrix) |
one pass, however many (each clamped to 0–1 after it, as CSS does; past 8 in a row, 2 on the weakest GPUs, the rest are combined without the clamps) | one ctx.filter composite (a tint between other filters: one more copy) |
blur |
two passes; radii over 8 pixels on a 1/2 to 1/16 size copy (two to six passes, cheaper the bigger) | ctx.filter: blur() |
dropShadow |
the blur’s passes (one when it is hard), then the shadow and the node drawn with what follows (two more passes when the node also fades, or a filter follows) | ctx.filter: drop-shadow() |
shader |
one pass | skipped |
- Every filtered node costs its layer: one more draw call for its content and one per pass. Unfiltered nodes around it are still drawn together. Use filters on a few nodes (a background, a title, the hit enemy), not on each of 200 coins: for many identical nodes, bake the look into their art.
- A filtered node fades as one picture: its
alphaapplies after the filters (as CSSopacity), so overlapping children don’t show through each other. - Filters apply inside the node’s clip and under its mask: a blur does not spill out of a clipped panel.
- Filters count against the layer nesting limit (
MAX_LAYERS, 4, a mask counts as one): past it, a node is drawn without its filters (at its alpha; counted inengine.stats.filtersSkipped). - A mask node can have filters of its own:
photo.mask = shapewithshape.filters = [blur(4)]gives a soft edge. - Size: the filter machinery (the GL passes and programs, the Canvas 2D plans) comes in with the first filter factory a game imports; a game that imports none bundles none of it.
Tiers: the blur limit
Section titled “Tiers: the blur limit”Blurs cost in proportion to their radius and area, so each tier caps them (engine.supports.maxBlur):
| Tier | Largest blur radius | Bigger radii |
|---|---|---|
| high | 32 units | drawn at 32 |
| medium | 16 units | drawn at 16 |
| low | 0: blurs off | blur is skipped; dropShadow is drawn hard (not blurred) |
Colour filters and tint work on every tier. Check engine.supports.blur if a blur carries meaning (a “frosted
glass” pause screen): on the low tier, darken instead.
Canvas 2D
Section titled “Canvas 2D”On Canvas 2D the engine uses the canvas’s own CSS filters (ctx.filter), which compute the same matrices: the
parity tests compare every filter on both. tint is drawn with a source-atop fill and works everywhere.
colorMatrix uses an SVG filter by url() (Chrome and Firefox). Where ctx.filter takes no url(), the browser
may ignore the whole filter string of that node, the other filters with it: there, keep colorMatrix for WebGL.
The engine puts one hidden <svg> in the page per distinct matrix (nodes with the same numbers share it), and takes it
out about a second after no node uses it any more, and when the engine is destroyed. Changing a matrix every frame
works, but makes a new SVG filter each frame: tween the built-in filters (hueRotate, saturate, …) instead.
The filter string of a node is worked out once per distinct set of values, and kept: a list rebuilt every frame
(node.filters = [brightness(b)]) costs no more than one kept, as long as the values repeat.
Some browsers have no ctx.filter: Safari before 18 and some old TV browsers. There the engine draws tint, skips
the other filters (the node is drawn without them), says so once on the console and counts them in
engine.stats.filtersSkipped. engine.supports.filters is false: choose another look if it matters.
if (!engine.supports.filters) lockedLevel.alpha = 0.5 // instead of grayscale()Doing the colour maths in JavaScript (getImageData) every frame would be far too slow on those devices, so the
engine does not try.
engine.supports
Section titled “engine.supports”engine.supports// { filters: true, blur: true, shaders: true, maxBlur: 32,// tiers: { high: { maxBlur: 32 }, medium: { maxBlur: 16 }, low: { maxBlur: 0 } } }| Field | |
|---|---|
filters |
colour filters, blur and drop shadows draw (WebGL; Canvas 2D with ctx.filter) |
blur |
blurs and soft shadows draw: filters, and a tier with maxBlur above 0 |
shaders |
shader() filters draw: WebGL only |
maxBlur |
the current tier’s blur limit, stage units |
tiers |
each tier’s limits (the tier can drop during play) |
Read it when you need it (it follows the renderer and the tier, which can change during play).
Parity
Section titled “Parity”pnpm parity draws every filter (and a colour chain, a fading shadow, a tint and a blur between colour filters) with WebGL 2, WebGL 1 and
Canvas 2D in headless Chrome and compares the pixels: at most 0.02% of painted pixels differ by more than 24/255, the
mean difference is under 5/255 (SwiftShader and a real GPU). The only known difference: colours are computed in
floating point on WebGL and in 8 bits on Canvas 2D, so very dark, nearly transparent edges can differ by a few levels.