@tile
Cuts a shape into pieces and gives each cell one of them, as a value for clip-path.
When every cell covers the whole doodle, the pieces fit back together into a mosaic.
Syntax
- @tile([<preset>; ]<commands>)
- @tile.<kind>([<preset>; ]<commands>)
- @tile.x, @tile.y, @tile.xy, @tile.k, @tile.r
The shape is a preset from the @shape property, such as heart or circle,
or @shape commands that draw one, text and path included.
@tile.hex(text: O; gap: 1) fills a letter with hexagons. The default is square.
The kind picks how the shape is cut; @tile alone is @tile.slice.
Inside @svg, each copy of an element gets the next piece as path data.
The commands are in the table below.
The kinds
Each cuts the default square into 30 pieces:
One piece per cell
Each cell gets the piece at its index, clipped from the whole doodle.
To make every cell cover the doodle, @tile adds @place: center and @size: 100% unless the doodle sets them itself.
gap leaves space between the pieces, in percent of the element, where the background shows through:
@grid: 24x1 / 60vmin / #111;
background: @p(
#f2efe6, #f2efe6, #f2efe6,
#d7301f, #1f4e9c, #f5c400
);
clip-path: @tile(gap: 1.5);
Without gap the pieces overlap by a hair, so no thin lines show where they meet.
gap: 0 makes them meet exactly, for see-through colors.
The number of cells is the number of pieces, so the examples here put the cells in one row:
@grid: 60x1 gives 60 pieces, where @grid: 60 would give 3600.
points sets a smaller number of pieces; cells left without one show nothing.
It counts pieces, not corners as in @shape,
so leave it out of a body copied from there.
On a doodle that isn't square, the pieces keep their shape when @grid gives both lengths in one unit, as in 12rem 6rem:
@grid: 60x1 / 12rem 6rem / #3b2410;
background: hsl(@r(34, 44) 92% @r(52, 64)%);
clip-path: @tile.hex(gap: 1.2);
Otherwise, as when the size comes from CSS, they are laid out on a square and stretch with the doodle.
aspect gives the proportions then, as a number or a fraction such as 2 / 1.
Slices
@tile.slice starts from the shape's bounding box and keeps cutting a random piece in two, across its longer side.
The cuts are level or upright; spread lets them tilt by up to half that many degrees either way:
@grid: 30x1 / 60vmin;
background: repeating-linear-gradient(
@r(180)deg, #000 0 2px, #fff 0 6px
);
clip-path: @tile(spread: 90); density works here too: where it is larger, the pieces are smaller.
Around scattered points
@tile.voronoi spreads points inside the shape, the same points as @plot.scatter,
and gives each point the region closer to it than to any other. The regions fill the whole box, not only the shape:
@grid: 60x1 / 60vmin / #1b1b1b;
background: @p(
#264653, #2a9d8f, #e9c46a, #f4a261, #e76f51
);
clip-path: @tile.voronoi(gap: 1); @tile.delaunay joins the same kind of points into triangles and keeps the ones inside the shape.
For these two kinds, points is the number of points, not pieces:
@grid: 60x1 / 60vmin;
background: #000;
clip-path: @tile.delaunay(circle; points: 9; gap: 1); density is an expression in x and y, the -1 to 1 space with y pointing up,
and d, the distance to the shape's outline.
More points land where it is larger, so the pieces are smaller there:
@grid: 120x1 / 60vmin / #000;
background: #fff;
clip-path: @tile.voronoi(density: 1 - y; gap: .6);
Where the points land follows the doodle's seed, or the seed command.
relax evens them out, in 10 rounds by default; relax: 0 leaves them where they fell.
Shaping the regions
bound: shape cuts the regions at the shape's outline, holes included, so the pieces stay inside the shape:
@grid: 80x1 / 60vmin;
background: #000;
clip-path: @tile.voronoi(
subtract { circle {} circle { r: .5 } }
bound: shape;
gap: 1;
); Lattices
@tile.grid, @tile.hex, @tile.triangle, @tile.cube and @tile.penrose lay a regular tiling over the shape.
They pick the largest piece size that fits the cells and keep every piece that reaches into the shape, so the edge is jagged:
@grid: 60x1 / 60vmin / #3b2410;
background: hsl(@r(34, 44) 92% @r(52, 64)%);
clip-path: @tile.hex(circle; gap: 1.2; r: .8);
Cells left over show nothing.
cube splits each hexagon into the three faces of a cube,
and penrose is a Penrose tiling of thin and thick rhombs.
The seed changes a lattice only through jitter or a random edge.
shift slides every other row of a grid by that part of a square, so .5 lays bricks;
shift: 0 .3 slides every other column instead:
@grid: 60x1 / 60vmin / #eee6dc;
background: hsl(@R(120, 240, 2, 8) 60% 50%);
clip-path: @tile.grid(shift: .5; gap: .8); jitter moves every corner of a lattice by up to that part of a piece's size, and the pieces still fit.
Above about .3 they start to fold over each other.
@plot.hex, @plot.grid and @plot.penrose give the centers of these pieces as points,
keeping only the pieces inside the shape.
Bending the edges
edge bends every edge of a grid, hex or triangle lattice along a curve.
It is an expression in t, which runs from 0 to 2π along the edge,
and its value pushes the edge sideways, as a part of the edge's length.
Both neighbours of an edge share the curve, so the pieces still fit:
@grid: 40x1 / 60vmin / #222;
background: hsl(@r(360) 60% 70%);
clip-path: @tile.grid(edge: .15 * sin(t); gap: .6); e is the direction of the edge. On a grid, 1 is level and 2 upright,
so this bends only the upright edges:
@grid: 40x1 / 60vmin;
background: @p(#0f4c5c, #e36414, #fb8b24, #9a031e);
clip-path: @tile.grid(edge: (e - 1) * .12 * sin(t));
On a triangle, 1 is level; on a hex, 2 is upright. The other values are the two slanted directions.
On a triangle a bump such as sin(t / 2) flattens out, while a wave such as sin(t) keeps its shape.
random() is drawn once per edge, so every edge gets its own curve, like the cuts of a jigsaw puzzle:
@grid: 60x1 / 60vmin / #1b1b1b;
background: hsl(@r(190, 230) 55% @r(45, 70)%);
clip-path: @tile.hex(
edge: .25 * sin(t) * (random() - .5);
gap: .5
); @R.t gives every edge a wobble of its own:
@grid: 40x1 / 60vmin / #222;
background: hsl(@r(360) 60% 70%);
clip-path: @tile.grid(edge: @R.t(-.2, .2); gap: .6); x and y are the middle of the edge, in the -1 to 1 space with y pointing up,
so the curve can change across the shape:
@grid: 80x1 / 60vmin;
background: #000;
clip-path: @tile.triangle(
edge: .12 * x * sin(2t);
gap: .5
); slide moves the points of an edge along it, as a part of the edge's length, and reads the same values as edge.
Together they draw curves that fold back:
@grid: 40x1 / 60vmin / #222;
background: hsl(@r(360) 60% 70%);
clip-path: @tile.hex(
edge: .15 * sin(t);
slide: .2 * sin(t / 2)^2;
gap: .6
);
With bent edges every piece is the same shape, moved.
With pair: turn every piece is the same shape, turned, as in Escher's lizards.
@tile.k tells the turns apart, in steps of 120° on a hex and 90° on a grid,
and e numbers the pairs of edges.
It works on hex, and on grid without shift:
@grid: 48x1 / 60vmin / #222;
clip-path: @tile.hex(
pair: turn;
edge: .3 * sin(t / 2)^2 * (e - 2) + .1 * sin(t);
gap: .5
);
background: oklch(.78 .11 calc(@tile.k * 120)); Operators
op turns the pieces into a new tiling, one step per letter.
The letters come from Conway's polyhedron notation.
op: t cuts every corner off, so the squares of a grid become octagons with small squares between them:
@grid: 100x1 / 60vmin / #222;
clip-path: @tile.grid(op: t; gap: .8);
background: @match(@tile.k == 1, #ebbf4d, #e6437d);
It works on grid, hex, triangle, cube, penrose and voronoi.
Click a letter to open it in the playground:
-
hexbefore any op -
op: da piece for each corner -
op: acorners cut to the middle of each side -
op: tcorners cut off -
op: keach piece split into triangles -
op: epieces pulled apart -
op: btandetogether -
op: spulled apart with a twist -
op: gthe same asds -
op: jthe same asda -
op: mthe same askda -
op: othe same asde
The colors come from @tile.k.
Pieces grown from the old ones keep their number, 1 in yellow, pieces at the old corners get 2, in pink,
and pieces along the old sides get 3, in white.
Letters combine, and run right to left as in Conway's notation: op: ds applies s first, then d.
On a grid that gives the Cairo tiling of pentagons, which g writes in one letter:
@grid: 80x1 / 60vmin / #222;
clip-path: @tile.grid(op: g; gap: .8);
background: hsl(@r(190, 230) 55% @r(45, 70)%); Stars
star draws a star inside every piece, the way Islamic star patterns are drawn.
Two lines leave the middle of each side at the given angle, between 0 and 90 degrees, and meet inside the piece.
The space left around each corner becomes a piece of its own, in pink here:
star comes after op, so the two combine.
On the octagons and squares of op: t, star: 67.5 gives eight-pointed stars and crosses, with the corner pieces in white:
@grid: 150x1 / 60vmin / #000;
clip-path: @tile.grid(op: t; star: 67.5; gap: .5);
background: @match(@tile.k == 3, #fff, #e6437d); Packed circles
@tile.circle packs circles into the shape, largest first, and returns each as an ellipse().
Here the shape is a four-petal rose drawn with the @shape command r: cos(2t):
@grid: 160x1 / 60vmin;
background: red;
clip-path: @tile.circle(r: cos(2t); gap: .4); size limits how big a circle can grow where it is centered, in average radii, 1.5 by default.
It is an expression in x, y and d, and 0 leaves no circle there.
Growing with d, the circles are small along the outline:
@grid: 200x1 / 60vmin;
background: #000;
clip-path: @tile.circle(size: .2 + d * 6; gap: .4); Round corners
round turns the corners of every piece into curves of this radius, in the -1 to 1 space, as far as the edges allow.
A rounded piece is a CSS shape() with true curves:
@grid: 100x1 / 60vmin / #2d2a26;
background: hsl(30 8% @r(62, 80)%);
clip-path: @tile.voronoi(round: 1; gap: 1.4); Reading the piece
Once a cell has its piece, @tile.x and @tile.y give the piece's center, as percentages of the element.
They make good amounts for color-mix():
@grid: 60x1 / 60vmin;
clip-path: @tile.voronoi(gap: 1);
background: color-mix(in oklch, #264653 @tile.y, #e9c46a);
In an expression the percent sign is dropped, so @match(@tile.x < 50, …) picks the pieces on the left half.
A little randomness on top of the height shades the triangles like a low-poly model:
@grid: 100x1 / 60vmin;
clip-path: @tile.delaunay(circle);
background: color-mix(in oklch,
#0b2545 calc(@tile.y + @r(-12, 12)%), #b9d6f2
); @tile.xy gives both as x y, for a position such as a gradient's at:
@grid: 60x1 / 60vmin / #000;
clip-path: @tile.voronoi(gap: .6);
background: radial-gradient(8% 8% at @tile.xy,
#fff 15%, #48cae4 30%, #023e8a
); @tile.k tells pieces apart. In cube, 1 is the top face, 2 the left and 3 the right:
@grid: 90x1 / 60vmin;
clip-path: @tile.cube();
background: oklch(calc(1 - @tile.k * .22) .05 250); In penrose, 1 is a thin rhomb and 2 a thick one:
@grid: 150x1 / 60vmin;
clip-path: @tile.penrose(gap: .4);
background: color-mix(in oklch,
#00539c calc((@tile.k - 1) * 100%), #ffd662
); In voronoi, no two neighboring regions share a kind:
@grid: 100x1 / 60vmin;
clip-path: @tile.voronoi(gap: .5);
background: oklch(.75 .12 calc(@tile.k * 72)); @tile.r is the radius of the largest circle about the piece's center that fits inside it:
@grid: 150x1 / 60vmin;
clip-path: @tile.circle(circle; gap: .6);
background: color-mix(in oklch,
#d00000 calc(@tile.r * 8), #ffb3c1
);
These read the cell's last @tile, and are empty in a cell without a piece.
@place: @tile.voronoi() places the cell at the center of its piece instead of clipping it,
as on the @place page.
Moving the pieces
In clip-path, @tile also sets the cell's transform-origin to the center of its piece.
So scale, rotate and their animations work on each piece in place:
@grid: 40x1 / 60vmin;
background: #000;
clip-path: @tile.hex(circle);
animation: pulse 2s infinite alternate;
animation-delay: @r(-2s);
@keyframes pulse {
to { scale: .3 }
} Reference
Kinds
| Kind | Pieces | Kind values |
|---|---|---|
slice | the bounding box cut by straight lines (the default) | 1 |
voronoi | regions around points scattered inside the shape, filling the box | 1, 2, 3…, never the same as a neighbor |
delaunay | triangles between scattered points, inside the shape | 1 |
grid | a grid of squares | 1 |
hex | a grid of hexagons | 1 |
triangle | a grid of triangles | 1 pointing down, 2 up |
cube | hexagons split into three rhombs | 1 top, 2 left, 3 right |
penrose | a Penrose tiling of rhombs | 1 thin, 2 thick |
circle | circles packed inside the shape, largest first | 1 |
Commands
| Command | Kinds | Meaning |
|---|---|---|
gap | every kind | space between pieces, in percent of the element; a negative value overlaps them. Default -.3, or 0 for circle. |
aspect | every kind | the element's width over its height, as 2 or 16 / 9, so pieces keep their shape. Defaults to the proportions of @grid's size when both lengths share a unit other than %, otherwise 1. |
points | every kind | at most this many pieces; for voronoi and delaunay, the number of points. Defaults to the number of cells, or about half of it for delaunay. |
seed | every kind | a fixed seed for everything random in the tiling, random() and @R.t included. Defaults to the doodle's seed. |
round | every kind but circle | curves the corners with this radius, in the -1 to 1 space, so the piece is a shape() |
| shape commands | every kind | r, rotate, scale, move and the rest of @shape's commands change the shape being cut. frame is ignored, and round rounds the pieces, not the shape. |
spread | slice | how far a cut may tilt, in degrees, half of it each way. Default 0. |
density | slice, voronoi, delaunay | where pieces are smaller, as an expression in x, y and d, the distance to the outline |
relax | voronoi, delaunay | rounds that even out the points, up to 50. Default 10; 0 keeps them random. |
bound | voronoi | box, the default, lets the regions fill the box; shape cuts them at the shape's outline. Not with op or star. |
shift | grid | slides every other row by this part of a square; 0 <n> slides every other column instead. Default 0. |
jitter | grid, hex, triangle, cube, penrose | moves each corner by up to this part of a piece's size. Keep it below about .3. |
edge | grid, hex, triangle | bends each edge sideways, as a part of its length. An expression in t (0 to 2π along the edge), e (its direction) and x, y (its middle); random() and @R.t differ from edge to edge. No effect with op or star. |
slide | grid, hex, triangle | moves each edge's points along it, as a part of its length. The same values as edge. |
pair | grid, hex | turn matches each edge with the next one around a corner, so the pieces are one shape turned by @tile.k. Not on a grid with shift. |
op | grid, hex, triangle, cube, penrose, voronoi | builds a new tiling, one step per letter, applied right to left; see the letters. |
star | the same as op | turns every piece into a star, with lines leaving each side's middle at this angle, in degrees between 0 and 90. Applied after op. |
size | circle | the largest radius where a circle is centered, in average radii, as an expression in x, y and d. Default about 1.5. |
A number may be math in the variables of the body, as in k: .1; jitter: 2k,
except in gap and star.
// starts a comment to the end of the line.
Operator letters
| Letter | Step |
|---|---|
d | the dual: one piece for each corner, joining the centers of the pieces around it |
a | cuts every corner off, down to the middle of each side |
t | cuts every corner off |
k | splits every piece into triangles around its center |
e | pulls the pieces apart, with a square along each side and a piece at each corner |
b | t and e at once |
s | pulls the pieces apart with a twist, with triangles in the gaps |
g | short for ds, the dual of s |
j | short for da, the dual of a |
m | short for kda, k of j |
o | short for de, the dual of e |
Values of the cell's piece
| Function | Value |
|---|---|
@tile.x, @tile.y | the center of the piece, in percent of the element |
@tile.xy | the same center as x y |
@tile.k | which kind of piece it is, from the table above |
@tile.r | the radius of the largest circle about the center that fits in the piece, in percent of the element's shorter side |