Function

@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:

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

KindPiecesKind values
slicethe bounding box cut by straight lines (the default)1
voronoiregions around points scattered inside the shape, filling the box1, 2, 3…, never the same as a neighbor
delaunaytriangles between scattered points, inside the shape1
grida grid of squares1
hexa grid of hexagons1
trianglea grid of triangles1 pointing down, 2 up
cubehexagons split into three rhombs1 top, 2 left, 3 right
penrosea Penrose tiling of rhombs1 thin, 2 thick
circlecircles packed inside the shape, largest first1

Commands

CommandKindsMeaning
gapevery kindspace between pieces, in percent of the element; a negative value overlaps them. Default -.3, or 0 for circle.
aspectevery kindthe 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.
pointsevery kindat 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.
seedevery kinda fixed seed for everything random in the tiling, random() and @R.t included. Defaults to the doodle's seed.
roundevery kind but circlecurves the corners with this radius, in the -1 to 1 space, so the piece is a shape()
shape commandsevery kindr, 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.
spreadslicehow far a cut may tilt, in degrees, half of it each way. Default 0.
densityslice, voronoi, delaunaywhere pieces are smaller, as an expression in x, y and d, the distance to the outline
relaxvoronoi, delaunayrounds that even out the points, up to 50. Default 10; 0 keeps them random.
boundvoronoibox, the default, lets the regions fill the box; shape cuts them at the shape's outline. Not with op or star.
shiftgridslides every other row by this part of a square; 0 <n> slides every other column instead. Default 0.
jittergrid, hex, triangle, cube, penrosemoves each corner by up to this part of a piece's size. Keep it below about .3.
edgegrid, hex, trianglebends 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.
slidegrid, hex, trianglemoves each edge's points along it, as a part of its length. The same values as edge.
pairgrid, hexturn 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.
opgrid, hex, triangle, cube, penrose, voronoibuilds a new tiling, one step per letter, applied right to left; see the letters.
starthe same as opturns every piece into a star, with lines leaving each side's middle at this angle, in degrees between 0 and 90. Applied after op.
sizecirclethe 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

LetterStep
dthe dual: one piece for each corner, joining the centers of the pieces around it
acuts every corner off, down to the middle of each side
tcuts every corner off
ksplits every piece into triangles around its center
epulls the pieces apart, with a square along each side and a piece at each corner
bt and e at once
spulls the pieces apart with a twist, with triangles in the gaps
gshort for ds, the dual of s
jshort for da, the dual of a
mshort for kda, k of j
oshort for de, the dual of e

Values of the cell's piece

FunctionValue
@tile.x, @tile.ythe center of the piece, in percent of the element
@tile.xythe same center as x y
@tile.kwhich kind of piece it is, from the table above
@tile.rthe radius of the largest circle about the center that fits in the piece, in percent of the element's shorter side

See also