Skip to content

Paint: radial-gradient text fill and stroke - #5

Merged
lavrton merged 11 commits into
masterfrom
radial-gradients
Nov 23, 2026
Merged

lavrton merged 11 commits into
masterfrom
radial-gradients

Conversation

@nikigan

@nikigan nikigan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Text with a radial-gradient fill or stroke (background-clip: text, --rt-text-stroke-image) now paints. Before, it was left invisible. polotno's radial gradients (polotno-project/polotno#109) need this for text.

What draws

  • The CSS ending shape (CSS Images 3 §3.2), over the same box a linear gradient uses at that site:
    • circle or ellipse (the default);
    • sized by closest-side, closest-corner, farthest-side, farthest-corner (the default) or lengths, with % of the box for an ellipse;
    • centred at its at position: lengths, %, keywords, or edge offsets such as right 10px bottom 20px; the centre when there is none.
    • An invalid size or position (circle 50%, at 10px left) paints nothing, as in CSS.
    • A repeating radial tiles out to the farthest corner of the box.
  • Lengths: font and viewport units in a gradient resolve on the declaring element, as text-shadow's do. Sizes, positions and stop positions take any length or calc()/min()/max()/clamp(). This also applies to linear gradients.
  • Layers: the first gradient among the background's layers paints, linear or radial. A prefixed -webkit-radial-gradient (another grammar) paints nothing. A single-stop gradient paints.
  • Ellipses: canvas has no elliptical gradient.
    • On a raster canvas an ellipse is painted as a pattern at device resolution (made with createCanvas where given). The image reaches 128px past its box and is released when the draw ends.
    • Vector adapters: an ellipse arrives as the circle of its horizontal radius at its centre, and render-tag then calls setTransform(matrix) on that gradient when the adapter's gradients have one (SVG gradientTransform, a PDF pattern matrix). Without it the adapter keeps that circle. The method is render-tag's own extension; the README's "PDF and other vector adapters" section documents it.
  • Zero-size shapes paint one colour as Chrome does: the last stop, or the average of a repeating one whose % stops collapse.
  • Text on a path: one shape spans every glyph of the declaring element, including a descendant that paints its own. Each glyph carries its declarer's box from layout, so a serialised layout paints the same.
  • Stops before the centre: a stop at a negative offset opens the shape with the colour CSS shows at the centre, so red -100%, blue 100% starts half way. The colours are mixed in premultiplied sRGB for hex, named, rgb() and hsl() colours, with none and math-function channels as CSS Color 4 specifies.

Known deviations

  • in <colorspace> is ignored: colours mix in sRGB.
  • An ellipse at pixel ratio 2 on a large block is slow (47 ms against 11 ms for a circle on 1000×2700 in Chromium): the image is rebuilt each draw and capped at 4096px a side.
  • Ink more than 128px outside an ellipse's box is not painted.
  • Chrome and Firefox antialias pattern-filled glyph edges slightly heavier than gradient-filled ones (about 1.7% more alpha at 96px, edge pixels only; WebKit is identical).

Tests

  • Node: tests/node/radial-gradient.test.ts covers sizes and shapes, positions, invalid grammar, units, zero-size shapes, the ellipse paint (adapter setTransform, the raster pattern against canvas's own scaled ellipse, its image size and release, a rotated path frame), stops, the centre mix, layer order, and the paint sites.
  • Browser: tests/gradient-parity.test.ts compares linear and radial cases against each engine's native rendering on a 600×120 box, including positions, units, single stops and layer order.
  • Engine exceptions: render-tag paints Chrome's result in every browser, so these parity cases are skipped where the engine's own drawing differs: rgb(none 0 0) in WebKit; the zero-size forms in WebKit, and the average-colour ones in Firefox.
  • Results at e469605: typecheck passes; npm test (node + chromium) 2823 passed; test:webkit 300 passed; test:firefox 294 passed. bench:cross-browser: layout consistency unchanged in every lane.

Draft until

  • polotno-project/polotno#109 is reviewed. polotno bumps its render-tag version in its workspace catalog once this is released. For ellipses to keep their shape there, polotno's SVG and PDF exporters implement setTransform on their radial gradients.
  • The CHANGELOG entry is written at release.

A radial-gradient (repeating too) paints background-clip:text and
--rt-text-stroke-image instead of leaving the text invisible. Every form
draws as a circle reaching the farthest corner of the painted box, centred
at its at-position (%, px, keywords; the centre when absent), over the box
a linear gradient uses at that site. On a path one circle spans the
declarer's glyphs as laid out. Linear output is unchanged.
…clarers

A stop before the centre opens the circle with the colour CSS shows there
(`red -100%, blue 100%` starts half way), mixed in premultiplied sRGB for
hex, named, rgb() and hsl() colours. A prelude that opens with a math
function (`calc(20px + 10px) at 20% 80%`) is read as size and position,
not as a stop. A gradient name inside a quoted string or url() is no
longer a call, so it neither paints nor overrides a real layer. On a path
one circle spans every glyph of its declaring element, a descendant that
paints its own included; each circle is read once per draw, and a shadow
mask builds no radial gradient. Linear output is unchanged.
…s CSS does

A missing (none) rgb() channel or alpha takes the other stop's value when
the centre colour is mixed, and channels written with calc()/min()/max()/
clamp() are evaluated instead of falling back to clamping the stop.
@lavrton

lavrton commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Looks like colors drift from what Chrome is rendering. render-tag should follow CSS, then we should adopt from polotno side to make it consistent across the packages/exporters.

Ellipses (the default) and every extent keyword or length now draw as CSS
does instead of one farthest-corner circle. An ellipse is placed through its
gradient's setTransform on a vector adapter that has one, painted as a
device-resolution pattern on a raster canvas, else drawn as the circle of
its horizontal radius. A repeating radial tiles out to the farthest corner.
…e too

A repeating radial whose percentage stops collapse onto a zero-size shape
paints their average, as Chrome does; any other zero-size shape its last
stop. A path's --rt-text-stroke-image keeps such a solid paint instead of
falling back to the stroke colour.
@nikigan
nikigan marked this pull request as ready for review November 23, 2026 16:17
@nikigan
nikigan requested a review from lavrton November 23, 2026 16:18
The first gradient layer paints, linear or radial; a linear layer no longer
beats a radial one above it, and a prefixed -webkit-radial-gradient (another
grammar) paints nothing. `at` reads edge offsets (`right 10px bottom 20px`),
math functions and any length, and an invalid position paints nothing as in
Chrome. Font and viewport lengths in a gradient resolve on the declaring
element, as text-shadow's do, so `circle 2em` and `red 1em` match Chrome; a
stop position takes any length or math function, and a single stop paints.

An ellipse's pattern image reaches 128px past its box instead of half the
box's height (a 1000x2700 block: 34.6ms to 9.6ms a draw in Chromium), is
released when the draw ends, and costs one gradient, not two. A vector
adapter gets the circle at the ellipse's centre, with `setTransform` mapping
it onto the ellipse. On a path each glyph carries its declarer's box from
layout, so a layout that went through JSON paints the same.

Detection reuses the layer splitter and the validator's prelude rule; the
url() blanking and the mask shortcut are gone.
An adapter written before radial gradients threw a TypeError on radial text;
it painted nothing before.
@lavrton
lavrton merged commit 0ab9bab into master Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants