Function

@paint

A small language for drawing on a grid of cells, compiled to a GPU shader. Every cell runs the same rules, and each picks its own color with fill.

Syntax

  • @paint(<body>)
  • @paint<width>x<height>(<body>)

The body is a list of name: value; statements, run in source order. Several names can share one, a value each: a, b: 1, vec2(2, 3). An operator before the colon works on the name's own value: c*: 1 + du is c: c * (1 + du), and +, - and / work the same. Digits after the name set the image size in pixels (@paint800 is square). Without them the image takes the element's size and redraws when it resizes. @pattern is an older name and still works.

Put it in @content to draw a live <canvas>. It also works as a background, but an animated image is much slower there.

Grid and fill

grid sets the columns, or the columns and rows, 16x9 or 16 9. Each cell knows its column x and row y, both counted from 1 at the top-left. fill gives the cell its color:

@grid: 1 / 60vmin;
@content: @paint(
  grid: 64;
  fill: #fff;
  match((x - 1) & (y - 1)) {
    fill: #000;
  }
);

A fill is a CSS color, a number for gray, a vec3 / vec4, or a list of 3 or 4 channels. In an expression a color reads as its channels: mix(oklch(.75 .15 60), oklch(.45 .2 280), y / 64) blends two colors down the grid. A later fill paints over the one before it, so fill: red, .5 tints it. A pixel with no fill is transparent.

Choosing cells with match

The rules inside match(condition) { … } only run in the cells where the condition holds:

@grid: 1 / 60vmin;
@content: @paint(
  grid: 119;
  match(x*x*y*y % 120 < 24) {
    fill: #000
  }
);

Tests combine with and, or and not, so match(a or b) { … } runs where either holds. For an else chain, list the tests in one block; the first test that holds wins:

@grid: 1 / 60vmin;
@content: @paint(
  grid: 32;
  match {
    x = y or x + y = 33 { fill: #fff }
    max(abs(dx), abs(dy)) % 3 < 1 { fill: #000 }
    else { fill: #fff }
  }
);

Inside a value, match(test, a, b) picks a or b, and more pairs make a chain: match(t1: v1, t2: v2, …, else). There is no ?:.

@grid: 1 / 60vmin;
@content: @paint(
  grid: 32;
  fill: match(y > 16 + 6sin(x / 3 + t), #000, #fff);
);

Shape and size

shape masks each cell: square, circle, diamond, none, or a signed distance in du and dv, the offsets inside the cell, below 0 inside: shape: circle(du, dv, .4, 6) is a hexagon. size scales the named shapes from 0 to 1; on its own it masks with a square. With t, the time in seconds, the image animates:

@grid: 1 / 60vmin;
@content: @paint800(
  grid: 32;
  size: sin(4*log(length(dx, dy)) - 2t + atan(dy, dx));
  fill: #000;
);

Pixels and variables

Without a grid the whole image is one cell, so uv and pos address each pixel. Any name that is not a keyword is a variable, and it keeps the type of its first value:

@grid: 1 / 60vmin;
@content: @paint(
  n: fbm(uv.x*6, uv.y*6 - t*4);
  h: clamp(2n * pow(1 - uv.y, 1.2), 0, 1);
  fill: vec3(1.9h, 1.5h*h, pow(h, 2)*.8)
);

A swizzle on the left writes only those channels of a vector, as in v.xz: …. Built-ins such as pos and du can be written too, inside a block as well: pos.x: abs(pos.x) mirrors everything after it, and match(random() < .5) { du: -du; } flips half the cells:

@grid: 1 / 60vmin;
@content: @paint(
  grid: 12;
  match(random() < .5) { du: -du; }
  p: vec2(du, dv);
  d: min(length(p - .5), length(p + .5));
  fill: #000, shape(abs(d - .5), .12);
);

gradient(t, stop, …) maps t onto stops written as in a CSS gradient, colors or numbers, each with an optional position. Colors mix in oklab, and a repeated position is a hard edge. t sets the kind: uv.y is linear, length(pos) radial:

@grid: 1 / 60vmin;
@content: @paint(
  fill: gradient(length(pos), #f99a53 .28, #006b50 .28, #00055f .5)
);

$name inserts the text of the cell's custom property --name when the image is made, so each cell can draw something different. With no --name the image is not drawn:

@grid: 3 / 60vmin _2px;
--k: @i;
background: @paint(
  shape: circle;
  size: $k / 9;
  fill: #000;
);

$name(…) calls a function defined with --name(). Its parameters read bare, and a body that assigns result gives a value.

Loops

k*n { … } runs its body n times, with k counting from 1 to n. stop: test ends the loop at that line once the test holds. Here it iterates a Julia set until z escapes:

@grid: 1 / 60vmin;
@content: @paint(
  z: 2.8pos;
  c: vec2(-.8, .156);
  n: 0;
  k*96 {
    z: vec2(z.x * z.x - z.y * z.y, 2 * z.x * z.y) + c;
    n+: 1;
    stop: dot(z, z) > 256;
  }
  fill: match(n = 96, #000, hsl(n / 96 * turn, .75, .45));
);

k*0-9 counts k from 0 up to 9, or down with k*9-0. Heads side by side nest, the left one outside: i*3 j*3 { … } is i*3 { j*3 { … } }. n is a whole number up to 1024, or a variable set to one and never changed. stop ends the innermost loop, and its tests join with and and or. A fill inside a loop paints over the one from the step before, and it is the only drawing keyword a loop can set. A name first set inside a loop or a match block is gone after it, which is why n starts at 0 before the loop. repeat(n as k, test) { … } is the older way to write a loop, with k from 0, and it still works.

escape(z, c) runs the same loop from z, so the Julia set above is also escape(2.8pos, vec2(-.8, .156)). With only c it starts from 0, and the Mandelbrot set is one call:

@grid: 1 / 60vmin;
@content: @paint(
  n: escape(3pos - vec2(.7, 0));
  fill: -cos(2π(min(3n, 1) + vec3(0, .1, .2)));
);

Drawing with distances

circle, rect, segment and arc give the distance to a shape, below 0 inside. shape(d) fills where d is below 0, and stroke(d, w) draws a line w wide along the edge:

@grid: 1 / 60vmin;
@content: @paint(
  fill: #fff;
  k*12 {
    d: rect(rotate(pos, k * .12sin(t)), .072k);
    fill: #000, stroke(d, .012);
  }
);

Widths are in the units of pos, so a stroke grows with the image. px is one CSS pixel in those units, and stroke(d, 2px) stays two pixels wide at any size. union(a, b), intersect(a, b) and subtract(a, b) join two distances, and a third number rounds where they meet.

arc(p, r, a, b) is the distance to an arc of radius r from angle a to b, in radians counterclockwise. Its stroke has round ends:

@grid: 1 / 60vmin;
@content: @paint(
  fill: #fff;
  k*8 {
    a: k + t * (k - 4.5) / 4;
    fill: #000, shape(arc(pos, .04 + .045k, a, a + π), .03);
  }
);

Lines with segment. Point k on a circle joins point k × m, and m grows over time:

@grid: 1 / 60vmin;
@content: @paint(
  m: 2 + .2t;
  fill: #fff;
  k*90 {
    p: .45 * vec2(cos(k * π / 45), sin(k * π / 45));
    q: .45 * vec2(cos(k * m * π / 45), sin(k * m * π / 45));
    fill: #000, shape(segment(pos, p, q), .008);
  }
);

Marks

marks in place of grid draws each cell as a dot: marks: 1000x40 is 40 rows of 1,000. The body runs once per dot instead of once per pixel. place puts the dot at a point in the units of pos, and size sets its width.

Each row follows the Lorenz equations from a random start: a thousand steps onto the attractor, then the dot in column x takes x more:

@grid: 1 / 60vmin;
@content: @paint(
  marks: 1000x40;
  p: vec3(40random(y) - 20, 40random(y, 1) - 20, 27);
  n*2 k*1000 {
    p: p + .004vec3(
      10(p.y - p.x),
      p.x * (28 - p.z) - p.y,
      p.x * p.y - 8 / 3 * p.z
    );
    stop: n = 2 and k ≥ x;
  }
  place: vec2(p.x, p.z - 25) / 52;
  size: 1px;
  fill: #000, .25;
);

Two points draw a stroke, place: a, b, and place: p, next joins each dot to the next one in its row. Each row of seven is a blade of grass:

@grid: 1 / 60vmin;
@content: @paint(
  marks: 7x250;
  s: (x - 1) / (X - 1);
  root: random(y) - .5;
  h: .4 + .5random(y, 1);
  bend: (.12 + .08sin(t + 3root)) * s * s;
  place: vec2(root + bend, s * h - .5), next;
  size: (1 - s) * 6px;
  fill: mix(#183a1d, #b4d36a, s);
);

A grid whose body sets place draws marks too.

The pointer

mouse is the pointer over the doodle, in the units of pos, measured from the center of the element the image is drawn on, so an image in each cell sees it from where that cell is. It is 0 until the pointer moves over the doodle, and an image that reads it draws again as the pointer moves. Move the pointer over the needles:

@grid: 1 / 60vmin;
@content: @paint(
  grid: 24;
  d: .016normalize(mouse - pos);
  place: pos - d, pos + d;
  size: 2px;
  shape: circle;
  fill: #000;
);

Doodle textures

A section named texture… holds doodle code. It is rendered to an image, and texture(texture0, uv) reads it. The doodle sees the cell's custom properties, and a block holding only $name draws the doodle stored in --name: @doodle(…). The image repeats past its edges, so 4uv tiles it 4 × 4:

@grid: 1 / 60vmin;
@content: @paint(
  texture0 {
    @shape: heart;
    background: #000;
  }
  fill: texture(texture0, 4uv);
);

The doodle may have a @paint of its own as a background, drawn once as a still image.

Reference

Variables

NameValue
x, y, icolumn, row and index of the cell, from 1, row 1 at the top
X, Y, Inumber of columns, rows and cells
dx, dythe cell's offset from the center, as in @dx but with dy upward; length(dx, dy) is its distance and angle(dx, dy) its angle
du, dvoffset inside the cell, −0.5 to 0.5, dv upward
uvposition in the image, 0 to 1 from the bottom-left corner
poscentered position, the shorter side from −0.5 to 0.5, pos.y upward
mousethe pointer over the doodle, in the units of pos
ttime in seconds; using it animates the image
sizethe current size, 1 until set, or the cell's shorter side for a mark
placethe point of a mark, the cell's center until set
pxone CSS pixel in the units of pos, so 2px is two
deg, turnone degree and one turn in radians, so rotate(p, 45deg) turns by 45 degrees
π, PIpi

Operators

Expressions are GLSL, with a few shortcuts:

WriteMeans
2t, 2π, 2(t + 1)2 * t, 2 * π, 2 * (t + 1)
x^2, x ** 2x to the power 2
a < b < ca < b && b < c
and, or, not&&, ||, !
=, ≠, ≤, ≥==, !=, <=, >=
hsl(h .75 .65)hsl(h, .75, .65), a space between arguments works as a comma
#f80, redthe color, as a vec3, or a vec4 when it has alpha

% is the remainder, with the sign of the left side, as in $(). The bitwise operators work on whole numbers. Operators bind as in $(), not as in GLSL: x & 1 == 1 is (x & 1) == 1. A name that is neither a variable nor a color is reported and reads 0. Only a number multiplies what follows it, so two names need a *: k * t, not k t.

Functions

GLSL functions work, plus these. A point is one vec2 or two numbers, so fbm(uv * 6) is fbm(uv.x * 6, uv.y * 6). random, noise, fbm and voronoi follow the doodle's seed, so another seed gives another image.

  • gradient(t, stop, …) t mapped onto gradient stops, colors mixed in oklab
  • hsl(h, s, l) color, the hue an angle, s and l 0 to 1
  • noise(p) smooth gradient noise, 0 to 1
  • fbm(p) six octaves of noise
  • voronoi(p) distance to the nearest point
  • random(p [, k]) seeded random, 0 to 1; each k gives another number for p
  • random() 0 to 1, different in each cell, each repeat step and each place it is written
  • rotate(p, a) the point p turned a radians counterclockwise, not a matrix
  • angle(p) the angle of p from the x axis, −π to π
  • circle(p, r [, n]) signed distance to a circle of radius r; with n, the regular n-gon inside it, a corner up
  • rect(p, size) signed distance to a rectangle of the full size
  • segment(p, a, b) distance to the segment from a to b
  • arc(p, r, a, b) distance to the arc of radius r from angle a to b
  • shape(d [, size]) 1 where d < 0, 0 outside, antialiased; with size, where d < size / 2
  • stroke(d, w) a line w wide along d = 0, one pixel without w
  • union(a, b, k) both distances joined, rounded by k when given; also intersect and subtract
  • dither(x, y) 4×4 Bayer threshold, 0 to 1
  • match(test: value, …) the value after the first true test
  • pick(k: a, b, …) item k from 1, wrapping around
  • escape([z,] c) 0 where z → z² + c stays bounded within 96 steps, otherwise how soon it leaves; z starts at 0 when left out

See also