Skip to Content
valo
Drawing

Transforms and clips

Save, restore, matrices, and clip operations.

save / restore bracket local transforms and clips. An unmatched restore is a debug assertion in Rust and is ignored in release.

builder.save();
builder.translate(160.0, 110.0);
builder.rotate(0.2);
builder.scale(1.2, 1.2);
builder.concat(&Matrix::from_affine(1.0, 0.0, 0.2, 1.0, 0.0, 0.0));
builder.clip_rect(rect, ClipOp::Intersect, true);
builder.clip_rrect(rect, 16.0, ClipOp::Difference, true);
builder.restore();
builder.save();
builder.translate(160, 110);
builder.rotate(0.2);
builder.scale(1.2, 1.2);
builder.transform(new Float32Array([1, 0, 0.2, 1, 0, 0]));
builder.clipRect(x, y, w, h, ClipOp.Intersect, true);
builder.clipRoundedRect(x, y, w, h, new Float32Array([16]), ClipOp.Difference, true);
builder.restore();

The last argument of a clip says whether its edge is anti-aliased. A rectangle clip that is not, under a transform that only scales and moves, cuts at the nearest whole pixels and costs the renderer nothing: what a scroll view's clip wants. On any other shape the flag changes nothing for now. In JavaScript the argument is optional: leave it out, or pass undefined, and the clip is anti-aliased.

Left: a rotated rounded rect. Right: the same shape with a Difference hole.

scene.jsplayground
loading editor
liveacquiring device

Clip operations

OpResult
IntersectKeep pixels inside the shape
DifferenceKeep pixels outside the shape

Canvas2D clip() only intersects. A hole is a Difference clip, not a compositing trick.

Clips are never bounds-culled for Intersect: the ceiling covers the exterior. A Difference clip whose shape misses the viewport is skipped.

Nested rotate and scale inside an intersect clip, with a difference hole in the middle.

scene.jsplayground
loading editor
liveacquiring device

On this page