Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance stroke and fill handling in inspector components

konstantin-paulusbdbe9124091b
diff --git a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
index 7a0bdf35..77e2a86a 100644
--- a/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/fill-picker.tsx
@@ -31,7 +31,7 @@ import {
SolidPaint,
VideoPaint,
} from "@diffusionstudio/reconciler";
-import { BlendMode, BlendModeType, Cache, ELLIPTICAL_GRADIENT_DEFAULTS, EllipticalGradient, Paint, PaintType, Rotation, ScaleMode, ScaleModeType } from "@diffusionstudio/runtime";
+import { BlendMode, BlendModeType, Cache, Color, ELLIPTICAL_GRADIENT_DEFAULTS, EllipticalGradient, Paint, PaintType, Rotation, ScaleMode, ScaleModeType, Stroke, getParentEntity } from "@diffusionstudio/runtime";
import { useEditor } from "@/engine/hooks";
import { BLEND_MODE_ORDER, BLEND_MODE_SEPARATORS, blendModeName, displayBlendMode } from "./blend-modes";
import { readStopProps } from "./gradient-stops";
@@ -77,7 +77,6 @@ export type FillPickerProps = {
node: Entity;
fill: Entity;
onClose(): void;
- /** The picker replaced the fill element; this one is the paint now. */
onReplace(next: Entity): void;
tabs?: FillTab[];
};
@@ -109,6 +108,10 @@ export function FillPicker(props: FillPickerProps) {
const world = useWorld();
const editor = useEditor();
+ // Whose box the paint is placed in: a stroke has none, so its paints are
+ // placed in its parent's, and that is where the gradient's handles go.
+ const box = () => (props.node.has(Stroke) ? getParentEntity(props.node) : null) ?? props.node;
+
const paint = useTrait(() => props.fill, Paint);
const paintType = () => paint()?.value ?? PaintType.SOLID;
@@ -125,6 +128,18 @@ export function FillPicker(props: FillPickerProps) {
*/
const replaceFill = (element: () => unknown) => {
const fills = props.node.get(Cache)?.fills ?? [];
+
+ // A stroke's own `color` is the paint beneath all its children: the new
+ // paint takes that place, the bottom of the stack, and the color is unset.
+ if (props.fill === props.node) {
+ const [next] = editor.insertElement(props.node, element, fills[0]);
+ if (!next) return;
+
+ editor.editProperty(props.node, "color", false);
+ props.onReplace(next);
+ return;
+ }
+
const index = fills.indexOf(props.fill);
const anchor = index === -1 ? undefined : fills[index + 1];
@@ -139,6 +154,16 @@ export function FillPicker(props: FillPickerProps) {
setCurrentTab(tab);
if (tab === "solid" && paintType() !== PaintType.SOLID) {
+ // A stroke drawn by this paint alone goes back to its own `color`, the
+ // shorthand for a solid paint, rather than gaining a <solidPaint>.
+ const fills = props.node.get(Cache)?.fills ?? [];
+ if (props.node.has(Stroke) && !props.node.has(Color) && fills.length === 1) {
+ editor.editProperty(props.node, "color", DEFAULT_SOLID_COLOR);
+ editor.remove(props.fill);
+ props.onReplace(props.node);
+ return;
+ }
+
replaceFill(() => <SolidPaint color={DEFAULT_SOLID_COLOR} />);
return;
}
@@ -237,7 +262,7 @@ export function FillPicker(props: FillPickerProps) {
<SolidFillPicker fill={props.fill} />
</Show>
<Show when={currentTab() === "gradient"}>
- <GradientFillPicker node={props.node} fill={props.fill} onChangeKind={handleGradientKindChange} />
+ <GradientFillPicker node={box()} fill={props.fill} onChangeKind={handleGradientKindChange} />
</Show>
<Show when={currentTab() === "asset"}>
<AssetFillPicker node={props.node} fill={props.fill} onSelectAsset={handleSelectAsset} />
diff --git a/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx b/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx
index dfaaa678..a4634e69 100644
--- a/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/stroke-inspector.tsx
@@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { createMemo, createSignal } from "solid-js";
+import { Show, createMemo, createSignal } from "solid-js";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { Icon } from "@/components/ui/icon";
@@ -14,15 +14,17 @@ import {
FloatingInspectorTitle,
} from "@/components/ui/floating-inspector";
import { ControlRow } from "@/components/ui/control-group";
-import { ColorOpacityRow } from "@/components/ui/color-opacity-row";
-import { ColorOpacityPicker } from "@/components/ui/color-opacity-picker";
+import { FillItem } from "@/components/ui/fill-item";
import { ControlledTextField } from "@/components/ui/text-field";
import { SegmentedIconTabs } from "@/components/ui/segmented-icon-tabs";
import { Keyframe } from "@/components/ui/keyframe";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
-import { Computed, StrokeJoin, StrokeStyle, colorToHex } from "@diffusionstudio/runtime";
+import { Cache, Computed, StrokeJoin, StrokeStyle } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
import { syncKeyframe } from "@/engine/keyframes";
+import { FillPicker, type FillTab } from "./fill-picker";
+
+import type { Accessor } from "solid-js";
import type { StrokeJoin as StrokeJoinName } from "@diffusionstudio/jsx";
import type { Entity } from "koota";
@@ -39,11 +41,27 @@ const JOIN_NAMES: Record<StrokeJoin, StrokeJoinName> = {
[StrokeJoin.ROUND]: "round",
};
+/** A line is drawn with a color or a gradient, never a picture. */
+const STROKE_TABS: FillTab[] = ["solid", "gradient"];
+
/** `<stroke>`'s defaults; a control left at one of these unsets its prop. */
-const DEFAULT_OPACITY = 1;
const DEFAULT_WIDTH = 1;
const DEFAULT_MITER_LIMIT = 10;
+// Stable identity, so a stroke without paint children does not resample every tick.
+const NO_PAINTS: Entity[] = [];
+
+/**
+ * The paint the inspector shows for `stroke`: its topmost paint child, or,
+ * without one, the stroke itself, whose `color` is its own solid paint. A
+ * stroke stacking several paint children is edited through its top one.
+ */
+export function useStrokePaint(stroke: Accessor<Entity>): Accessor<Entity> {
+ // Cache is derived state, written without change events.
+ const paints = useDerived(() => stroke().get(Cache)?.fills ?? NO_PAINTS);
+ return createMemo(() => paints().at(-1) ?? stroke());
+}
+
type StrokeInspectorProps = {
stroke: Entity;
anchorRef: HTMLElement;
@@ -51,22 +69,21 @@ type StrokeInspectorProps = {
};
/**
- * One `<stroke>`: its paint (color and opacity) and its line style
- * (`width`/`join`/`miterLimit`). A stroke is a solid color and takes no paint
- * children, so the color half is the color picker alone, with no gradient or
- * asset tab. `cap` has no control yet: it only shows on open paths (text
- * glyphs) and there are no icons for it.
+ * One `<stroke>`: its paint and its line style (`width`/`join`/`miterLimit`).
+ * The paint is the stroke's own `color` or a gradient paint child, picked in
+ * the fill picker without its asset tab; a gradient is placed in the box of
+ * the stroke's parent, so that is where its handles go. `cap` has no control
+ * yet: it only shows on open paths (text glyphs) and there are no icons for it.
*/
export function StrokeInspector(props: StrokeInspectorProps) {
const world = useWorld();
const editor = useEditor();
- let colorRowRef: HTMLDivElement | undefined;
+ let inspectorRef: HTMLDivElement | undefined;
- const [pickingColor, setPickingColor] = createSignal(false);
+ const [pickingPaint, setPickingPaint] = createSignal(false);
- const color = useDerived(() => props.stroke.get(Computed)?.color ?? 0);
- const opacity = useDerived(() => props.stroke.get(Computed)?.opacity ?? DEFAULT_OPACITY);
+ const paint = useStrokePaint(() => props.stroke);
const width = useDerived(() => props.stroke.get(Computed)?.strokeWidth ?? DEFAULT_WIDTH);
const style = useTrait(() => props.stroke, StrokeStyle);
@@ -74,19 +91,6 @@ export function StrokeInspector(props: StrokeInspectorProps) {
const join = createMemo(() => JOIN_NAMES[style()?.join ?? StrokeJoin.MITER]);
const miterLimit = () => style()?.miterLimit ?? DEFAULT_MITER_LIMIT;
- /** A stroke's color is required, never unset. */
- const editColor = (value: number) => {
- const hex = colorToHex(value);
- editor.editProperty(props.stroke, "color", hex);
- syncKeyframe(world, editor, props.stroke, "color", hex);
- };
-
- const editOpacity = (value: number) => {
- const next = Math.round(value * 100) / 100;
- editor.editProperty(props.stroke, "opacity", next === DEFAULT_OPACITY ? false : next);
- syncKeyframe(world, editor, props.stroke, "opacity", next);
- };
-
const editWidth = (value: number) => {
// Unlike a node's width this is the line width, not `resizeEntity`, so
// there is no track of its own to mint and the sync belongs after.
@@ -107,13 +111,13 @@ export function StrokeInspector(props: StrokeInspectorProps) {
};
const handleClose = () => {
- setPickingColor(false);
+ setPickingPaint(false);
props.onClose();
};
return (
<>
- <FloatingInspector open anchorRef={props.anchorRef} width={248}>
+ <FloatingInspector open anchorRef={props.anchorRef} width={248} ref={inspectorRef}>
<FloatingInspectorHeader class="items-center justify-between">
<FloatingInspectorTitle>Stroke</FloatingInspectorTitle>
<Tooltip>
@@ -131,15 +135,8 @@ export function StrokeInspector(props: StrokeInspectorProps) {
</FloatingInspectorHeader>
<FloatingInspectorSeparator />
<FloatingInspectorContent class="flex flex-col gap-2 p-4">
- <ControlRow label="Color" ref={colorRowRef}>
- <ColorOpacityRow
- color={color()}
- onChangeColor={editColor}
- opacity={opacity()}
- onChangeOpacity={editOpacity}
- onClick={() => setPickingColor(true)}
- keyframe={<Keyframe target={props.stroke} property="opacity" />}
- />
+ <ControlRow label="Paint">
+ <FillItem fill={paint()} onClick={() => setPickingPaint(true)} />
</ControlRow>
<ControlRow label="Weight">
@@ -177,34 +174,16 @@ export function StrokeInspector(props: StrokeInspectorProps) {
</FloatingInspectorContent>
</FloatingInspector>
- <FloatingInspector open={pickingColor} anchorRef={() => colorRowRef} offset={26}>
- <FloatingInspectorHeader>
- <FloatingInspectorTitle>Color</FloatingInspectorTitle>
- <div class="ml-auto">
- <Tooltip>
- <TooltipTrigger
- as={Button}
- size="icon"
- variant="ghost"
- class="text-muted-foreground"
- onClick={() => setPickingColor(false)}
- >
- <Icon name="close-remove" class="size-6" />
- </TooltipTrigger>
- <TooltipContent>Close</TooltipContent>
- </Tooltip>
- </div>
- </FloatingInspectorHeader>
- <FloatingInspectorContent class="p-0">
- <ColorOpacityPicker
- color={color()}
- opacity={opacity()}
- onColorChange={editColor}
- onOpacityChange={editOpacity}
- keyframeTarget={props.stroke}
- />
- </FloatingInspectorContent>
- </FloatingInspector>
+ <Show when={pickingPaint()}>
+ <FillPicker
+ anchorRef={inspectorRef!}
+ node={props.stroke}
+ fill={paint()}
+ tabs={STROKE_TABS}
+ onClose={() => setPickingPaint(false)}
+ onReplace={() => {}}
+ />
+ </Show>
</>
);
}
diff --git a/apps/web/src/components/sidebar-right/inspector/strokes.tsx b/apps/web/src/components/sidebar-right/inspector/strokes.tsx
index 71093e28..bb38e468 100644
--- a/apps/web/src/components/sidebar-right/inspector/strokes.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/strokes.tsx
@@ -15,17 +15,24 @@ import {
import { Icon } from "@/components/ui/icon";
import { ItemRow } from "@/components/ui/item-row";
import { PanelSection } from "@/components/ui/panel-section";
-import { useHas } from "@diffusionstudio/koota-solid";
+import { useHas, useTrait } from "@diffusionstudio/koota-solid";
import { Stroke as StrokeElement } from "@diffusionstudio/reconciler";
-import { Cache, Computed, Hidden, colorToHex } from "@diffusionstudio/runtime";
+import { Cache, Color, Computed, Hidden, Paint, PaintType, colorToHex } from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
-import { StrokeInspector } from "./stroke-inspector";
+import { StrokeInspector, useStrokePaint } from "./stroke-inspector";
import type { Entity } from "koota";
-/** What "Add stroke" authors; `<stroke>`'s own default color. */
+/** What "Add stroke" authors. */
const DEFAULT_COLOR = "#000000";
+/** What a row says for a stroke drawn with a gradient, in place of a color. */
+const GRADIENT_NAMES: Partial<Record<PaintType, string>> = {
+ [PaintType.LINEAR_GRADIENT]: "Linear",
+ [PaintType.RADIAL_GRADIENT]: "Radial",
+ [PaintType.ANGULAR_GRADIENT]: "Angular",
+};
+
// Stable identity, so a node without strokes does not resample every tick.
const NO_STROKES: Entity[] = [];
@@ -36,8 +43,8 @@ type StrokesSettingsProps = {
/**
* The `<stroke>` children of the selected node, in paint order (the list is
* shown topmost first, so the last element in the file is the first row).
- * A row opens the stroke's own inspector; what it shows is the color, since
- * that is the one thing a stroke always says. The line style
+ * A row opens the stroke's own inspector; what it shows is the paint the
+ * inspector edits (see `useStrokePaint`): its color, or which gradient. The line style
* (`width`/`join`/`miterLimit`) is the stroke's own and not the node's, so it
* lives in that inspector rather than under every row.
*/
@@ -139,8 +146,17 @@ type StrokeRowProps = {
function StrokeRow(props: StrokeRowProps) {
const editor = useEditor();
- const color = useDerived(() => props.stroke.get(Computed)?.color ?? 0);
+ const paint = useStrokePaint(() => props.stroke);
+ const paintType = useTrait(paint, Paint);
+ const color = useDerived(() => paint().get(Computed)?.color ?? 0);
const hidden = useHas(() => props.stroke, Hidden);
+ const hasColor = useHas(() => props.stroke, Color);
+
+ // A stroke with neither a `color` nor a paint child draws nothing.
+ const value = () => {
+ if (paint() === props.stroke && !hasColor()) return "None";
+ return GRADIENT_NAMES[paintType()?.value ?? PaintType.SOLID] ?? colorToHex(color()).replace("#", "");
+ };
const toggleHidden = () => {
editor.editProperty(props.stroke, "hidden", !hidden());
@@ -151,7 +167,7 @@ function StrokeRow(props: StrokeRowProps) {
<ContextMenuTrigger>
<ItemRow
label="Stroke"
- value={colorToHex(color()).replace("#", "")}
+ value={value()}
icon={<Icon name="rectangle-small" />}
onClick={props.onSelect}
disabled={hidden()}
diff --git a/docs/reference/jsx/elements.md b/docs/reference/jsx/elements.md
index e221e5aa..d6b9bc51 100644
--- a/docs/reference/jsx/elements.md
+++ b/docs/reference/jsx/elements.md
@@ -46,7 +46,7 @@ Sub-entity children of the node (or paint) that holds them. See [styles.md](./st
| Element | What it is |
| ------- | ---------- |
-| `<stroke>` | An outline of the parent's box or glyphs. Several stack. |
+| `<stroke>` | An outline of the parent's box or glyphs, in its `color` or its solid and gradient paint children. Several stack. |
| `<shadow>` | A drop shadow beneath the parent's silhouette. Several stack. |
| `<effect>` | A CSS-style filter over the parent's rendered pixels. Several stack. |
| `<mask>` | A matte limiting the `<effect>` holding it: the alpha of a tracked frame sequence. Under an `opacity` effect, the cut-out. See [styles.md](./styles.md#mask). |
diff --git a/docs/reference/jsx/paints.md b/docs/reference/jsx/paints.md
index 9d5013c3..a6b3efda 100644
--- a/docs/reference/jsx/paints.md
+++ b/docs/reference/jsx/paints.md
@@ -11,7 +11,7 @@ Internally a node's fill is not a property but a **paint child**: a sub-entity a
</rect>
```
-Paint elements are valid inside any filled visual element (`<rect>`, `<text>`, `<textRange>`, `<video>`, `<image>`, `<html>`, `<surface>`, and a `<scene>`); a `<group>` has no fill of its own, so it takes none. Multiple paints stack in document order; later paints render on top, and a paint child on a `<video>`/`<image>` draws over the media paint created by `src`.
+Paint elements are valid inside any filled visual element (`<rect>`, `<text>`, `<textRange>`, `<video>`, `<image>`, `<html>`, `<surface>`, and a `<scene>`); a `<group>` has no fill of its own, so it takes none. A [`<stroke>`](./styles.md#stroke) takes solid and gradient paints too, and draws its line with them. Multiple paints stack in document order; later paints render on top, and a paint child on a `<video>`/`<image>` draws over the media paint created by `src`.
| Element | Props | Meaning |
| ------- | ----- | ------- |
@@ -31,7 +31,7 @@ Colors accept any CSS color; alpha is ignored (use `opacity`). `color`, `opacity
## Placing a gradient
-Gradients are placed the way SVG places them by default (`gradientUnits="objectBoundingBox"`): in fractions of the parent's box, not px. `0`–`1` spans the box's width horizontally and its height vertically, so a gradient keeps its place when the box is resized. Values outside `0`–`1` reach past the edges.
+Gradients are placed the way SVG places them by default (`gradientUnits="objectBoundingBox"`): in fractions of the parent's box, not px. Under a `<stroke>`, the box is the stroke's parent's. `0`–`1` spans the box's width horizontally and its height vertically, so a gradient keeps its place when the box is resized. Values outside `0`–`1` reach past the edges.
A `<linearGradientPaint>` draws stop `0` at (`x1`, `y1`) and stop `1` at (`x2`, `y2`), SVG's names:
diff --git a/docs/reference/jsx/styles.md b/docs/reference/jsx/styles.md
index f8b03dfc..3885f28b 100644
--- a/docs/reference/jsx/styles.md
+++ b/docs/reference/jsx/styles.md
@@ -12,19 +12,32 @@ Three sub-entity children a node takes alongside its [paints](./paints.md): an o
## `<stroke>`
-An outline of the parent's box — or of its glyphs, on a `<text>` or a `<textRange>`. `color`/`opacity` are its paint, the rest its line style.
+An outline of the parent's box — or of its glyphs, on a `<text>` or a `<textRange>`. `color` is its paint, the rest its line style.
| Prop | Type | Default | Meaning |
| ---- | ---- | ------- | ------- |
-| `color` | `string` | **required** | Any CSS color; alpha is ignored (use `opacity`). |
+| `color` | `string` | none | Any CSS color; alpha is ignored (use `opacity`). Without it the stroke is drawn by its paint children alone. |
| `width` | `number` | `1` | Line width, px. A `width` [keyframe track](./keyframes.md) under a stroke drives this, not a box. |
| `join` | `"miter" \| "round" \| "bevel"` | `"miter"` | How the stroke turns corners. |
| `cap` | `"butt" \| "round" \| "square"` | `"butt"` | How the stroke ends open paths (text glyphs). |
| `miterLimit` | `number` | `10` | Miter length limit, as a ratio of the width. |
-| `opacity` | `number` | `1` | `0`–`1`. |
+| `opacity` | `number` | `1` | `0`–`1`, over `color` and every paint child. |
| `blendMode` | `BlendMode` | `"sourceOver"` | How the stroke composites. |
| `hidden` | `boolean` | absent | Excludes the stroke without removing it. |
+`color` is shorthand for a solid paint, as `fill` is on a node. To draw the line with a gradient, put the [paint](./paints.md) inside the stroke instead: `<solidPaint>`, `<linearGradientPaint>`, `<radialGradientPaint>` and `<angularGradientPaint>` are allowed, and any other paint is an error. Paint children stack over `color` in document order, each through the same line, and take their own `opacity` and `blendMode`. A gradient is placed in the box of the stroke's parent, the same box its fills use, so a fill and a stroke with the same gradient line up. The line is centered on the box's edge, so its outer half reaches past `rx={0.5}`.
+
+```tsx
+<rect width={640} height={360} cornerRadius={24} fill="#111111">
+ <stroke width={8} join="round">
+ <angularGradientPaint rotation={-90}>
+ <colorStop offset={0} color="#FF0055" />
+ <colorStop offset={1} color="#0055FF" />
+ </angularGradientPaint>
+ </stroke>
+</rect>
+```
+
## `<shadow>`
A drop shadow beneath the parent's silhouette: a blurred, offset copy of it in `color`.
diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.ts
index 5bcafe70..71c2e806 100644
--- a/packages/jsx/src/types.ts
+++ b/packages/jsx/src/types.ts
@@ -572,10 +572,15 @@ export type RectProps = CommonProps & FillProps & {
/**
* `<stroke>` — an outline of the parent's box (or glyphs), a sub-entity like a
- * paint: `color`/`opacity` are its paint, `width`/`join`/`cap`/`miterLimit`
- * its line style. Several stack in document order, later ones on top.
+ * paint: `color` is its own solid paint, `width`/`join`/`cap`/`miterLimit` its
+ * line style, and paint children (`<SolidPaint>`, `<LinearGradientPaint>`,
+ * `<RadialGradientPaint>`, `<AngularGradientPaint>`) draw through the same
+ * line over `color`, placed in the parent's box. `opacity` and `blendMode`
+ * apply to all of it. Several stack in document order, later ones on top.
*/
-export type StrokeProps = ColorProps & PaintProps & TrackChildren & {
+export type StrokeProps = Partial<ColorProps> & PaintProps & {
+ /** Paint children, stacked over `color`; `<KeyframeTrack>` children. */
+ children?: SolidJSX.Element;
/** Line width, px. Default 1. */
width?: number;
/** How the stroke turns corners. Default "miter". */
@@ -697,6 +702,7 @@ export type KeyframeProps = {
export type SolidPaintProps = ColorProps & PaintProps & TrackChildren;
+/** A gradient's box is its parent's; under a `<stroke>`, the stroke's parent's. */
export type GradientPaintProps = PaintProps & {
/** `<ColorStop>` children — the gradient's color stops. */
children?: SolidJSX.Element;
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index 8f3e3e06..219a6a1c 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -661,7 +661,6 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
entity.add(Stroke);
entity.add(Paint);
entity.set(Paint, { value: PaintType.SOLID });
- entity.add(Color);
entity.add(StrokeStyle);
break;
}
diff --git a/packages/runtime/src/actions/cache.ts b/packages/runtime/src/actions/cache.ts
index 88bb7722..bdf5769f 100644
--- a/packages/runtime/src/actions/cache.ts
+++ b/packages/runtime/src/actions/cache.ts
@@ -75,7 +75,8 @@ export function rebuildCaches(world: World, entity: Entity, parent: Entity | nul
// A geometry carrying Paint is not a fill of its parent: that is its own
// intrinsic paint (see getIntrinsicPaint). Nor is a stroke: its Paint is
- // what the outline is drawn with.
+ // the outline's own. The paints under a stroke are filed here too, under
+ // the stroke, which draws them through its line.
if (entity.has(Paint) && !entity.has(Geometry) && !entity.has(Stroke)) {
cache.fills[pid] = collect(Paint, Not(Geometry), Not(Stroke), ChildOf(parent))
.sort(sortByItemIndex);
diff --git a/packages/runtime/src/systems/render.ts b/packages/runtime/src/systems/render.ts
index 5a7b6817..e7089d9e 100644
--- a/packages/runtime/src/systems/render.ts
+++ b/packages/runtime/src/systems/render.ts
@@ -566,12 +566,14 @@ function renderShadows(world: World, entity: Entity): void {
function renderStrokes(world: World, entity: Entity): void {
const ctx = getCtx(world);
const eid = entity.id();
- const strokes = store(world, Cache).strokes[eid];
+ const cache = store(world, Cache);
+ const strokes = cache.strokes[eid];
if (!strokes) return;
const computed = store(world, Computed);
const blendMode = store(world, BlendMode);
- const paintStore = store(world, Paint);
+ const w = computed.width[eid]!;
+ const h = computed.height[eid]!;
for (const stroke of strokes) {
if (stroke.has(Hidden)) continue;
@@ -585,24 +587,22 @@ function renderStrokes(world: World, entity: Entity): void {
}
applyStrokeStyle(ctx, world, stroke);
- ctx.globalAlpha = savedAlpha * computed.opacity[sid]!;
-
- const paintType = paintStore.value[sid];
- if (paintType === PaintType.LINEAR_GRADIENT) {
- const w = computed.width[eid]!;
- const h = computed.height[eid]!;
- strokeLinearGradient(world, stroke, ctx, w, h);
- } else if (paintType === PaintType.RADIAL_GRADIENT) {
- const w = computed.width[eid]!;
- const h = computed.height[eid]!;
- strokeRadialGradient(world, stroke, ctx, w, h);
- } else if (paintType === PaintType.ANGULAR_GRADIENT) {
- const w = computed.width[eid]!;
- const h = computed.height[eid]!;
- strokeAngularGradient(world, stroke, ctx, w, h);
- } else {
- ctx.strokeStyle = colorToHex(computed.color[sid]!);
- ctx.stroke();
+ const strokeAlpha = savedAlpha * computed.opacity[sid]!;
+ const strokeCO = ctx.globalCompositeOperation;
+
+ // The stroke's own paint (its `color`) is intrinsic, beneath its paint
+ // children; each child draws through the same line at its own opacity.
+ ctx.globalAlpha = strokeAlpha;
+ strokeWithPaint(world, ctx, stroke, w, h);
+
+ for (const paint of cache.fills[sid] ?? NO_STROKE_PAINTS) {
+ if (paint.has(Hidden)) continue;
+ const pid = paint.id();
+ const pbi = blendMode.value[pid] ?? 0;
+
+ ctx.globalCompositeOperation = pbi !== 0 ? COMPOSITE_OPERATIONS[pbi]! : strokeCO;
+ ctx.globalAlpha = strokeAlpha * computed.opacity[pid]!;
+ strokeWithPaint(world, ctx, paint, w, h);
}
ctx.globalCompositeOperation = savedCO;
@@ -610,6 +610,30 @@ function renderStrokes(world: World, entity: Entity): void {
}
}
+// Stable empty list for strokes without paint children, so the render loop allocates none.
+const NO_STROKE_PAINTS: Entity[] = [];
+
+/**
+ * Strokes the current path, in the line style already set on `ctx`, with a
+ * paint in a `w` × `h` box: a stroke's own paint or one of its paint
+ * children. A solid paints only with a `Color`, so a stroke whose color is
+ * unset draws nothing of its own.
+ */
+function strokeWithPaint(world: World, ctx: Ctx2D, paint: Entity, w: number, h: number): void {
+ const paintType = paint.has(Paint) ? store(world, Paint).value[paint.id()] : undefined;
+
+ if (paintType === PaintType.LINEAR_GRADIENT) {
+ strokeLinearGradient(world, paint, ctx, w, h);
+ } else if (paintType === PaintType.RADIAL_GRADIENT) {
+ strokeRadialGradient(world, paint, ctx, w, h);
+ } else if (paintType === PaintType.ANGULAR_GRADIENT) {
+ strokeAngularGradient(world, paint, ctx, w, h);
+ } else if (paint.has(Color)) {
+ ctx.strokeStyle = colorToHex(store(world, Computed).color[paint.id()]!);
+ ctx.stroke();
+ }
+}
+
/**
* What covers a node's fills while they cannot be shown: the pulse while a
* generation is filling it in, the still fill when its source failed.
diff --git a/packages/runtime/src/traits/node.ts b/packages/runtime/src/traits/node.ts
index 037c0b32..37c2c4c7 100644
--- a/packages/runtime/src/traits/node.ts
+++ b/packages/runtime/src/traits/node.ts
@@ -38,9 +38,10 @@ export const IsClipPath = trait();
// sub-entities in ChildOf queries).
export const Shadow = trait();
-// Tag for stroke sub-entities: the entity is the stroke's paint (Paint/Color/
-// Opacity/BlendMode like a fill; a missing Paint reads as solid) and carries
-// its own StrokeStyle.
+// Tag for stroke sub-entities: the entity is the stroke's intrinsic paint
+// (Paint/Color/Opacity/BlendMode like a fill; a solid without Color paints
+// nothing), carries its own StrokeStyle, and takes paint children of its own
+// (Cache.fills of the stroke), drawn through the same line above it.
export const Stroke = trait();
export const Hidden = trait();
diff --git a/packages/runtime/src/utils/text.ts b/packages/runtime/src/utils/text.ts
index e9a3905f..513fc4cc 100644
--- a/packages/runtime/src/utils/text.ts
+++ b/packages/runtime/src/utils/text.ts
@@ -311,6 +311,8 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
// Radial and angular paints' patterns, made on first use.
let patterns: EllipticalPatterns | null = null;
+ const cache = store(world, Cache);
+
// Draw all text shadows
{
ctx.save();
@@ -371,15 +373,15 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
ctx.textAlign = 'start';
ctx.textBaseline = 'top';
+ const w = computed.width[eid]!;
+ const h = computed.height[eid]!;
+
for (const word of words) {
const strokes = getStrokes(world, entity, word.ranges);
if (!strokes.length) continue;
applyFont(ctx, world, entity, word.ranges);
- const w = computed.width[eid]!;
- const h = computed.height[eid]!;
-
// Draw strokes (if any)
for (const stroke of strokes) {
if (stroke.has(Hidden)) continue;
@@ -391,18 +393,38 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (blendMode !== 0) {
ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode]!;
}
- ctx.globalAlpha = savedAlpha * (stroke.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
+ const strokeAlpha = savedAlpha * (stroke.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
+ const strokeCO = ctx.globalCompositeOperation;
applyStrokeStyle(ctx, world, stroke);
- const paintType = paintStore.value[sid];
- if (paintType === PaintType.LINEAR_GRADIENT) {
- ctx.strokeStyle = createLinearGradient(world, stroke, ctx, w, h);
- } else if (paintType === PaintType.RADIAL_GRADIENT || paintType === PaintType.ANGULAR_GRADIENT) {
- ctx.strokeStyle = getEllipticalPattern(world, ctx, patterns ??= new Map(), stroke, paintType, w, h);
- } else {
- ctx.strokeStyle = colorToHex(colorStore.value[sid] ?? 0x000000);
+ // The stroke's own paint (its `color`, index -1) is intrinsic,
+ // beneath its paint children; each child draws through the same
+ // line at its own opacity and blend mode.
+ const paints = cache.fills[sid] ?? NO_PAINTS;
+ for (let i = -1; i < paints.length; i++) {
+ const own = i < 0;
+ const paint = own ? stroke : paints[i]!;
+ if (!own && paint.has(Hidden)) continue;
+ const pid = paint.id();
+
+ const paintType = paint.has(Paint) ? paintStore.value[pid] : undefined;
+ if (paintType === PaintType.LINEAR_GRADIENT) {
+ ctx.strokeStyle = createLinearGradient(world, paint, ctx, w, h);
+ } else if (paintType === PaintType.RADIAL_GRADIENT || paintType === PaintType.ANGULAR_GRADIENT) {
+ ctx.strokeStyle = getEllipticalPattern(world, ctx, patterns ??= new Map(), paint, paintType, w, h);
+ } else if (paint.has(Color)) {
+ ctx.strokeStyle = colorToHex(colorStore.value[pid] ?? 0x000000);
+ } else {
+ // A solid without a Color: a stroke whose color is unset.
+ continue;
+ }
+
+ const paintBlendMode = !own && paint.has(BlendMode) ? blendStore.value[pid] ?? 0 : 0;
+ ctx.globalCompositeOperation = paintBlendMode !== 0 ? COMPOSITE_OPERATIONS[paintBlendMode]! : strokeCO;
+ ctx.globalAlpha = own ? strokeAlpha : strokeAlpha * (paint.has(Opacity) ? opacityStore.value[pid] ?? 1 : 1);
+ ctx.strokeText(word.chars, word.x, word.y);
}
- ctx.strokeText(word.chars, word.x, word.y);
+
ctx.globalCompositeOperation = savedCO;
}
}
@@ -612,6 +634,9 @@ function getFills(world: World, entity: Entity, ranges: Entity[]): Entity[] {
return value;
}
+// Stable empty list for strokes without paint children, so the render loop allocates none.
+const NO_PAINTS: Entity[] = [];
+
function getStrokes(world: World, entity: Entity, ranges: Entity[]): Entity[] {
const cache = store(world, Cache);
let value = cache.strokes[entity.id()] ?? [];