css-doodle: Generative CSS Art & Pattern Generator

Category: Javascript | October 9, 2026
Authorcss-doodle
Last UpdateOctober 9, 2026
LicenseMIT
Tags
Views0 views
css-doodle: Generative CSS Art & Pattern Generator

css-doodle is a JavaScript Web Component for creating generative CSS art, geometric patterns, and animated backgrounds.

It uses CSS-like rules to style a grid of cells, with random values, math functions, SVG, and custom shapes to control the artwork.

The <css-doodle> element works in a plain HTML page. Patterns can be reproduced with a seed, updated through JavaScript, or exported as images.

Features

  • Grid-based CSS patterns with configurable dimensions and cell spacing.
  • Random colors, shapes, sizes, and positions with repeatable seeds.
  • CSS animations and time-based effects.
  • SVG artwork, filters, gradients, and polygon shapes.
  • Procedural tiling, including Voronoi, lattice, and sliced patterns.
  • Pointer-aware effects and WebGL shader rendering.
  • JavaScript controls for updates, animation playback, and image export.
  • Reusable doodle rules through CSS custom properties.

How To Use It

Installation

Load the css-doodle library from a CDN:

<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/css-doodle.min.js"></script>

npm Installation

Or install the package with NPM and import it in your JavaScript entry file.

npm install css-doodle
import 'css-doodle';

Basic Usage

Place CSS-like rules inside <css-doodle>. The @grid directive sets the number of cells, the artwork dimensions, and the background of the component. Other declarations apply to the cells.

<css-doodle>
  @grid: 8 / 320px / #152238;
  background: @p(#f9c74f, #f9844a, #90be6d, #577590);
  border-radius: @p(15%, 50%);
  scale: @r(.35, .9);
  rotate: @r(-30deg, 30deg);
  margin: 3px;
</css-doodle>

Reproduce a Pattern with a Seed

A fixed seed keeps random choices consistent across renders. Use the seed attribute when a background or illustration must keep the same appearance between page loads.

<css-doodle seed="brand-pattern-42">
  @grid: 12 / 360px / #101820;
  background: @p(#f2aa4c, #f5f0e6);
  border-radius: @p(0, 50%);
  scale: @r(.4, 1);
</css-doodle>

Refresh a Pattern with JavaScript

Call update() on the custom element to regenerate its styles. Remove a fixed seed when each update should produce a different arrangement. For built-in interaction, click:update updates the element when clicked, and auto:update="1.5s" changes it periodically.

<button id="shuffle-pattern" type="button">Shuffle pattern</button>
<script>
  document.querySelector('#shuffle-pattern').addEventListener('click', () => {
    document.querySelector('css-doodle').update();
  });
</script>

Export Artwork as an Image

The export() method resolves to an object containing SVG markup and image dimensions. Set download: true to save a PNG, or detail: true to request the generated PNG blob and its source.

const doodle = document.querySelector('css-doodle');
const image = await doodle.export({
  scale: 2,
  download: true,
  name: 'geometric-pattern'
});
console.log(image.width, image.height);

Create SVG Artwork

@svg() accepts XML or a CSS-like SVG language. Place its result in background to use it as an image, or in @content for inline SVG markup.

<css-doodle>
  @grid: 1 / 320px;
  background: @svg(
    viewBox: 0 0 100 100;
    circle {
      cx, cy: 50;
      r: 38;
      fill: #f9844a;
    }
    circle {
      cx, cy: 50;
      r: 22;
      fill: #152238;
    }
  );
</css-doodle>

Use css-doodle in React

Import css-doodle once, then place the rules inside a JSX template literal. JSX otherwise interprets braces in the doodle syntax as JavaScript expressions.

import 'css-doodle';
export function ArtPattern() {
  return (
    <css-doodle>{`
      @grid: 7 / 300px / #222;
      border-radius: @pn(100% 0, 0 100%);
      background: #fff;
    `}</css-doodle>
  );
}

Configuration and Reference

HTML Attributes for element

AttributePurpose
gridSets columns, rows, and optional depth (5, 5x7, or 1x1x8).
seedSets the seed for deterministic random functions; accepts text or numeric strings.
usePasses doodle rules directly or loads rules through CSS custom properties.
click:updateRegenerates the doodle when the element is clicked.
auto:updateRegenerates the doodle on an interval, for example 1.5s.
experimentalIncreases the allowable grid dimensions.
ignore-reduced-motionOverrides the automatic pause applied for a reduced-motion preference.

Doodle Properties

Properties beginning with @ are interpreted by css-doodle. Ordinary CSS properties continue to work on the individual cells.

PropertyDescription
@gridSets grid dimensions and optional size, fill, gap, transform, border, and layout modifiers.
@sizeSets width and height or a size preset such as a4, poster, or postcard.
@placePositions a cell at a point outside its regular grid placement.
@gapSets row and column gaps, optionally with a drawn rule.
@contentInserts text, generated SVG, nested doodles, or shader content into cells.
@shapeApplies a preset or custom polygon shape to a cell.
@seedSets a seed inside the doodle code, taking precedence over the HTML attribute.
@useReuses rules from CSS custom properties.

`@grid` Modifiers

The @grid directive accepts a dimensions expression followed by optional modifiers.

SyntaxEffect
5x7Five columns and seven rows; a single number produces a square grid.
/ 60vminSets the component’s width and height.
/ #222Sets the background after the size modifier.
_ 4pxSets the gap between cells.
+ 1.2Scales the grid container.
* 15degRotates the grid container.
*h 45degRotates the host’s colors with hue-rotate().
~ 20% 0Translates the container.
^ 1.2Enlarges the container’s dimensions.
∆ 200pxSets perspective for the component.
ß 2px solid #000Sets the host’s border.
| blur(3px)Applies a backdrop filter above the cells.
row, colSwitches to horizontal or vertical flex layout.
p3dEnables preserve-3d for transforms.
noclipLets cells extend beyond the component boundary.

Cell Selectors

Use these selectors as blocks in the doodle language. They select a specific position, conditional group, or generated element.

SelectorDescription
:doodleStyles the <css-doodle> host element.
:containerStyles the internal grid container.
@nth()Selects cells by index.
@at()Selects a column-and-row position.
@row() / @y()Selects rows.
@col() / @x()Selects columns.
@depth() / @z()Selects depth levels in nested grids.
@evenSelects alternating checkerboard cells with odd coordinate sums.
@oddSelects alternating checkerboard cells with even coordinate sums.
@random()Selects cells using a probability or count.
@match()Selects cells for which a condition evaluates true.
@cell()Selects cells through combined index, condition, and random rules.
Nested and pseudo selectorsSupports :hover, ::before, ::after, descendants, and & in selector blocks.
CSS at-rulesSupports rules such as @keyframes, @media, and @font-face.

Functions: Cell Coordinates and Geometry

Cell functions use one-based indices for rows, columns, and the overall cell number.

FunctionDescription
@i, @ICurrent cell index and total number of cells.
@idIdentifier of the current cell.
@x, @XCurrent column and number of columns.
@y, @YCurrent row and number of rows.
@z, @ZCurrent depth and number of depth levels.
@iI, @IiForward and reverse normalized index ratios.
@xX, @XxForward and reverse normalized column ratios.
@yY, @YyForward and reverse normalized row ratios.
@dx, @dy, @dr, @dc, @dm, @da, @dbCell offsets, distances, and angle relative to the grid center or edge.

Functions: Sequences

Sequence functions generate repeated values. Step functions work inside sequence expressions.

FunctionDescription
@m, @M, @repRepeats values, with different joining behavior for generated CSS or SVG.
@n, @N, @nx, @nyStep number, step count, and step coordinates.
@ndOffset from the midpoint of a sequence.
@nN, @NnForward and reverse normalized step ratios, with easing.

Functions: Random Values and Picks

Random outputs follow the active seed. Some pick functions process a list in sequence and others shuffle it.

FunctionDescription
@r, @riRandom range value; @ri produces an integer.
@RSpatially related values from noise; @R.t() varies noise along a shape outline.
@p, @PRandom list selection; @P avoids repeating the preceding choice at that call.
@pn, @pnrPicks in forward or reverse order.
@pdPicks in a shuffled sequence.
@PN, @PNR, @PDSequence-step versions of the ordered and shuffled picks.
@lp, @lrReuses the previous picked or random value.

Functions: Expressions and Values

Use these functions inside CSS declarations or other doodle functions.

FunctionDescription
$() and unit forms such as $px()Evaluates arithmetic and comparison expressions.
@code()Converts UTF-16 code units into characters.
@cycle()Produces rotations of a list for pick functions.
@google-font()Loads a Google font by family name.
@hex()Returns a number in hexadecimal.
@match()Selects a value based on a condition.
@<Math>()Calls JavaScript Math functions or constants, including @sin(), @cos(), and @PI.
@mirror(), @Mirror()Creates forward-and-reverse value sequences.
@once()Evaluates a value once and shares it across the cells.
@palette()Generates a seeded palette along an OKLCH color curve.
@raw()Passes its text through unchanged.
@reverse()Reverses a list or SVG path.
@stripe()Creates hard transitions between colors for gradients.
@var()Preserves a native CSS var() expression.

Functions: SVG and Filters

The SVG family generates SVG markup, paths, and images from doodle expressions.

FunctionDescription
@svg()Creates SVG from XML or CSS-like SVG rules.
@svg-filter()Creates SVG filters for filter or backdrop-filter, including noise displacement.
@svg-pattern()Creates a repeating SVG pattern tile.
@svg-polygon()Creates SVG artwork from polygon and shape commands.
@linearGradient(), @radialGradient()Builds SVG gradients for use inside SVG definitions.
@arc()Generates arc path data and joins consecutive arc segments.
@flip(), @flipH(), @flipV()Mirrors SVG path coordinates.
@invert()Exchanges SVG path axes.

Functions: Shapes and Tiling

@shape() and @tile() are useful when artwork needs custom outlines, clipped geometry, or repeated pieces.

FunctionDescription
@shape()Creates a polygon() from presets or coordinate and polar expressions.
@plot(), @Plot()Generates point positions for shapes; .scatter distributes them within an outline.
@tile()Generates shape tiles; supports different tiling modes and per-cell clipping.

Tiling and Shape Controls

The following public controls belong to shape and tiling expressions. Their availability depends on the selected generator or tile mode.

ControlPurpose
aspectAccounts for nonsquare dimensions in @tile and @plot.scatter.
stretchChanges Voronoi tile proportions.
crackIntroduces cracks between Voronoi tiles.
relaxSmooths the distribution of Voronoi tiles.
roundRounds tile corners.
turn, slidePairs tiling edges for Escher-style patterns.
sizeAdjusts circle packing.
jitterVaries lattice tiles.
densityControls the density of sliced tiles.
round:, edge:Controls rounded outlines and edge details in shape commands.

Custom Shape Commands

@shape() and @plot() accept shape expressions. Coordinates can use the angle t, the point index i, or custom variables defined inside the expression.

CommandPurpose
pointsNumber of generated vertices; from 3 to 3600, with a default of 3.
rRadius expression in polar coordinates. Default: 1.
x, yCoordinate equations as an alternative to r.
rotateRotation angle in degrees.
scaleOne or two scale factors.
moveHorizontal and vertical offset.
frameThickness of a shape outline.
turnNumber of revolutions around the shape. Default: 1.
dirDirection setting, such as auto, reverse, or an angle.
fillevenodd or nonzero polygon fill rule.
roundCorner rounding in shape outlines.
edgeControls the outline edge profile.
Custom variableAny additional property name can define a value used in the shape equations.

Functions: Nested Artwork and Shaders

FunctionDescription
@doodle()Generates a doodle as an image or embeds it through @content.
@pattern()Converts a grid-based pattern language into a GPU shader.
@shaders()Renders GLSL fragment shaders through WebGL.

`@pattern()` Commands

@pattern() generates a GPU-backed pattern from its own compact syntax. Its match expressions select fills for virtual cells.

CommandPurpose
gridSpecifies the virtual pattern grid dimensions.
fillSets a CSS color, gray value, or GLSL vector color.
shapeChooses square, circle, diamond, none, or a custom distance expression.
sizeSets the relative size of a shape within its virtual cell.
match(condition)Applies declarations only when an expression matches; can be paired with else.
repeat(count as index)Repeats a shader expression block with an index.
Custom variableDefines a value available to subsequent shader expressions.

Functions: Time and Pointer Uniforms

FunctionDescription
@t, @ts, @T, @TSRelative time or local-time values in milliseconds and seconds.
@ux, @uyPointer position within the component.
@uw, @uhComponent width and height in pixels.
@udx, @udyDistance from the pointer to the cell’s center on each axis.

JavaScript Element API

All element methods operate on an existing <css-doodle> node. The export() method is asynchronous. grid, seed, use, and diagnostics are JavaScript properties, not constructor options.

Method or PropertyDescription
update([code])Regenerates the doodle, optionally replacing its code with a string.
export([options])Exports SVG markup and optionally generates or downloads a PNG. Returns a Promise.
pause()Pauses CSS, SVG, and shader-driven animation.
resume()Resumes animation after a pause.
autoUpdate([interval])Starts or restarts an update timer; accepts an optional interval.
cancelAutoUpdate()Stops the update timer and removes timer-related attributes.
gridGets grid dimensions or sets the grid attribute.
seedGets the active seed or sets the seed attribute.
useGets or sets the use attribute.
diagnosticsReads warnings from the last render.

`export()` Options

These are the options accepted by doodle.export({ ... }). The returned object always contains width, height, and svg; PNG-related fields (blob, source) are included when a PNG is generated.

OptionType and Behavior
scaleNumber; scales the exported dimensions. Default: 1.
nameString; output file name when downloading. Defaults to a timestamp-based name.
downloadBoolean; generates and downloads a PNG. Default: false.
detailBoolean; generates PNG data and returns additional output details. Default: false.

Package Exports

The npm package exposes modules for the element, generators, parsers, and metadata. The parser exports return internal syntax trees, whose structure is not a stable integration contract.

Module or ExportDescription
css-doodleRegisters the <css-doodle> custom element.
css-doodle/component: CSSDoodleExposes the element class.
css-doodle/component: define(name, element)Registers a custom-element class under a tag name.
css-doodle/component: create(code)Creates a doodle element from code.
css-doodle/generator: svg(code)Returns generated SVG markup as a string.
css-doodle/generator: shape(commands)Returns a CSS polygon() string.
css-doodle/generator: prerender()Produces prerendered HTML for a doodle with declarative Shadow DOM.
css-doodle/parserExposes the language parsers and tokenizer for development tooling.
css-doodle/meta: functions, mathFunctionsFunction metadata and supported math names.
css-doodle/meta: selectors, properties, calcOperatorsSelector, property, and expression-operator metadata.

Parser Exports

css-doodle/parser also exports individual syntax parsers for editor integrations. Their returned AST is an internal representation and can change in another release.

ExportParses
parseCssDoodle CSS-like statements.
parseSvgCSS-like SVG syntax.
parseShapeCommandsShape expressions.
parseGridGrid dimensions and modifiers.
parseValueGroupGrouped values.
parsePatternPattern DSL expressions.
parseShadersShader expressions.
parseSvgPathSVG path syntax.
parseVarVariable syntax.
parseDirectionDirection expressions.
parseCompoundValueCompound values.
tokenizerRaw syntax tokens.

Custom Events

Listen for these events on the <css-doodle> element. click:cell supplies the x, y, and z coordinates, the clicked cell element, and the original browser event through event.detail. A warn event can be canceled to suppress its console message.

EventWhen It Fires
renderInitial render and subsequent completed renders.
beforeUpdateImmediately before an update begins.
updateAfter an update completes.
click:cellA generated cell is clicked.
warnThe parser or generator reports a problem with the doodle code.
const doodle = document.querySelector('css-doodle');
doodle.addEventListener('click:cell', (event) => {
  const { x, y, z } = event.detail;
  console.log('Clicked cell:', x, y, z);
});
doodle.addEventListener('warn', (event) => {
  console.warn(event.detail.message);
});

Styling and Customization

Regular CSS controls the component’s position and layout on the page. The public Shadow DOM parts grid and cell are also available through ::part() for external styling. To change generated cell patterns individually, write rules inside the doodle itself.

css-doodle {
  display: block;
  max-width: 100%;
}
css-doodle::part(cell) {
  transition: transform .3s ease;
}

Alternatives & Related Resources

You Might Be Interested In:


Leave a Reply