Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor keyframe handling for paint properties

konstantin-paulus4e64fd822953
diff --git a/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx b/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx
index 38efc714..09dfdf86 100644
--- a/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/solid-picker.tsx
@@ -40,7 +40,6 @@ export function SolidFillPicker(props: SolidFillPickerProps) {
opacity={opacity()}
onColorChange={updateColor}
onOpacityChange={updateOpacity}
- keyframeTarget={props.fill}
/>
);
}
diff --git a/apps/web/src/components/ui/fill-item.tsx b/apps/web/src/components/ui/fill-item.tsx
index f1bfe343..9d534f51 100644
--- a/apps/web/src/components/ui/fill-item.tsx
+++ b/apps/web/src/components/ui/fill-item.tsx
@@ -5,7 +5,7 @@
import { createEffect, createMemo, createSignal, onCleanup, Show } from "solid-js";
import { AssetThumbnail } from "@/components/ui/asset-thumbnail";
import { Icon } from "@/components/ui/icon";
-import { Keyframe } from "@/components/ui/keyframe";
+import { PaintKeyframe } from "@/components/ui/keyframe";
import { OpacitySwatch } from "@/components/ui/opacity-swatch";
import { useTrait, useWorld } from "@diffusionstudio/koota-solid";
import { AssetId, Computed, Paint, PaintType, colorToHex, parseColor } from "@diffusionstudio/runtime";
@@ -29,7 +29,8 @@ type FillItemProps = {
* One paint, as the fills panel shows it: a swatch of what it paints, its
* color (a solid) or its name (everything else), and an opacity that doubles
* as a slider across the row. Both values are props of the paint element, so
- * they are written through the editor and keyframed after.
+ * they are written through the editor and keyframed after. The row's keyframe
+ * is the paint's one keyframe, for all of its props at once.
*/
export function FillItem(props: FillItemProps) {
const world = useWorld();
@@ -357,7 +358,7 @@ export function FillItem(props: FillItemProps) {
class="z-20 min-w-2"
onFocusIn={(e) => e.stopPropagation()}
>
- <Keyframe property="opacity" target={props.fill} />
+ <PaintKeyframe paint={props.fill} />
</div>
</div>
</div>
diff --git a/apps/web/src/components/ui/keyframe.tsx b/apps/web/src/components/ui/keyframe.tsx
index e343e303..35e66bef 100644
--- a/apps/web/src/components/ui/keyframe.tsx
+++ b/apps/web/src/components/ui/keyframe.tsx
@@ -7,7 +7,7 @@ import { useHas, useWorld } from "@diffusionstudio/koota-solid";
import { Cache, Source } from "@diffusionstudio/runtime";
import { cx } from "@/lib/cva";
import { useDerived, useEditor } from "@/engine/hooks";
-import { findKeyframeAt, findKeyframeTrack, keyframeFrame, toggleKeyframe } from "@/engine/keyframes";
+import { findKeyframeAt, findKeyframeTrack, findPaintKeyframeTracks, keyframeFrame, toggleKeyframe, togglePaintKeyframe } from "@/engine/keyframes";
import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip";
import type { AnimatableProperty } from "@diffusionstudio/jsx";
@@ -20,52 +20,105 @@ type KeyframeProps = {
class?: string;
};
+type KeyframeState = { available: boolean; hasTrack: boolean; isActive: boolean };
+
+const sameState = (a: KeyframeState, b: KeyframeState) =>
+ a.available === b.available && a.hasTrack === b.hasTrack && a.isActive === b.isActive;
+
export function Keyframe(props: KeyframeProps) {
const world = useWorld();
const editor = useEditor();
- const hasSource = useHas(() => props.target, Source);
+ const state = useDerived((): KeyframeState => {
+ const frame = keyframeFrame(props.target);
+ if (frame === null) return { available: false, hasTrack: false, isActive: false };
+ const track = findKeyframeTrack(world, props.target, props.property);
+ const keyframes = track?.get(Cache)?.keyframes.length ?? 0;
+ return {
+ available: true,
+ hasTrack: track !== null && keyframes > 0,
+ isActive: track !== null && findKeyframeAt(track, frame) !== null,
+ };
+ }, sameState);
- const state = useDerived(
- () => {
- const frame = keyframeFrame(props.target);
- if (frame === null) return { available: false, hasTrack: false, isActive: false };
- const track = findKeyframeTrack(world, props.target, props.property);
- const keyframes = track?.get(Cache)?.keyframes.length ?? 0;
- return {
- available: true,
- hasTrack: track !== null && keyframes > 0,
- isActive: track !== null && findKeyframeAt(track, frame) !== null,
- };
- },
- (a, b) => a.available === b.available && a.hasTrack === b.hasTrack && a.isActive === b.isActive,
+ return (
+ <KeyframeToggle
+ target={props.target}
+ state={state()}
+ onToggle={() => toggleKeyframe(world, editor, props.target, props.property)}
+ disabled={props.disabled}
+ class={props.class}
+ />
);
+}
+
+type PaintKeyframeProps = {
+ paint: Entity;
+ disabled?: boolean;
+ class?: string;
+};
+
+/** One keyframe for every animatable prop of a paint; a gradient's stops keep their own. */
+export function PaintKeyframe(props: PaintKeyframeProps) {
+ const world = useWorld();
+ const editor = useEditor();
- const toggle = () => toggleKeyframe(world, editor, props.target, props.property);
+ const state = useDerived((): KeyframeState => {
+ const frame = keyframeFrame(props.paint);
+ if (frame === null) return { available: false, hasTrack: false, isActive: false };
+ let hasTrack = false;
+ for (const track of findPaintKeyframeTracks(world, props.paint)) {
+ if (findKeyframeAt(track, frame) !== null) return { available: true, hasTrack: true, isActive: true };
+ hasTrack ||= (track.get(Cache)?.keyframes.length ?? 0) > 0;
+ }
+ return { available: true, hasTrack, isActive: false };
+ }, sameState);
+
+ return (
+ <KeyframeToggle
+ target={props.paint}
+ state={state()}
+ onToggle={() => togglePaintKeyframe(world, editor, props.paint)}
+ disabled={props.disabled}
+ class={props.class}
+ />
+ );
+}
+
+type KeyframeToggleProps = {
+ target: Entity;
+ state: KeyframeState;
+ onToggle(): void;
+ disabled?: boolean;
+ class?: string;
+};
+
+function KeyframeToggle(props: KeyframeToggleProps) {
+ const hasSource = useHas(() => props.target, Source);
return (
- <Show when={hasSource() && state().available}>
+ <Show when={hasSource() && props.state.available}>
<Tooltip openDelay={600} disabled={props.disabled}>
<TooltipTrigger
as="button"
type="button"
class={cx("flex items-center size-6 justify-center transition-colors group disabled:cursor-not-allowed disabled:opacity-50", props.class)}
- onClick={toggle}
+ onClick={props.onToggle}
disabled={props.disabled}
>
<div
class={cx(
"size-1.5 rotate-45 border transition-colors",
- state().isActive
+ props.state.isActive
? "bg-primary"
: "bg-transparent group-hover:border-foreground",
- state().hasTrack
+ props.state.hasTrack
? "border-primary"
: "border-muted-foreground"
)}
/>
</TooltipTrigger>
- <TooltipContent>{state().isActive ? "Remove keyframe" : "Add keyframe"}</TooltipContent>
+ <TooltipContent>{props.state.isActive ? "Remove keyframe" : "Add keyframe"}</TooltipContent>
</Tooltip>
</Show>
)
diff --git a/apps/web/src/engine/keyframes.tsx b/apps/web/src/engine/keyframes.tsx
index 8040ef99..3d0923d1 100644
--- a/apps/web/src/engine/keyframes.tsx
+++ b/apps/web/src/engine/keyframes.tsx
@@ -25,6 +25,8 @@ import {
framesToSeconds,
getNodeLocalFrame,
getPropertyPaths,
+ Paint,
+ PaintType,
} from "@diffusionstudio/runtime";
import type { AnimatableProperty } from "@diffusionstudio/jsx";
@@ -141,3 +143,52 @@ export function removeKeyframeTrack(world: World, editor: DocumentEditor, target
const track = findKeyframeTrack(world, target, property);
if (track) editor.remove(track);
}
+
+/** The props a paint's keyframe holds, by what the paint is. A gradient's stops keep their own. */
+function paintProperties(paint: Entity): AnimatableProperty[] {
+ switch (paint.get(Paint)?.value ?? PaintType.SOLID) {
+ case PaintType.SOLID:
+ return ["color", "opacity"];
+ case PaintType.LINEAR_GRADIENT:
+ return ["opacity", "x1", "y1", "x2", "y2"];
+ case PaintType.RADIAL_GRADIENT:
+ case PaintType.ANGULAR_GRADIENT:
+ return ["opacity", "cx", "cy", "rx", "ry", "rotation"];
+ default:
+ return ["opacity"];
+ }
+}
+
+/**
+ * The tracks behind a paint's keyframe. Only its own props count: a stroke
+ * standing in as its own paint also holds a `width` track, which is not one.
+ */
+export function findPaintKeyframeTracks(world: World, paint: Entity): Entity[] {
+ const paths: (string | undefined)[] = paintProperties(paint).map((property) => trackPropertyPath(paint, property));
+ return [...world.query(KeyframeTrack, ChildOf(paint))].filter((track) => paths.includes(track.get(KeyframeTrack)!.property));
+}
+
+/**
+ * A paint has one keyframe for all its props. With any of them keyed at the
+ * current frame, those keyframes are removed (each track with its last);
+ * otherwise every prop is keyed at its shown value.
+ */
+export function togglePaintKeyframe(world: World, editor: DocumentEditor, paint: Entity): void {
+ const frame = keyframeFrame(paint);
+ if (frame === null) return;
+
+ const existing: Entity[] = [];
+ for (const track of findPaintKeyframeTracks(world, paint)) {
+ const keyframe = findKeyframeAt(track, frame);
+ if (!keyframe) continue;
+ const last = (track.get(Cache)?.keyframes.length ?? 0) <= 1;
+ existing.push(last ? track : keyframe);
+ }
+
+ if (existing.length > 0) {
+ editor.remove(existing);
+ return;
+ }
+
+ for (const property of paintProperties(paint)) writeKeyframe(world, editor, paint, property);
+}
diff --git a/packages/runtime/src/media/caption/guinea.ts b/packages/runtime/src/media/caption/guinea.ts
index dd9ea1bc..1c08a1f8 100644
--- a/packages/runtime/src/media/caption/guinea.ts
+++ b/packages/runtime/src/media/caption/guinea.ts
@@ -4,7 +4,7 @@
import { store } from '../../world/store';
import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, Caption, ItemIndex, TextRange, TextStyle } from '../../traits';
+import { Paint, Color, Caption, Computed, ItemIndex, TextRange, TextStyle } from '../../traits';
import { renderText } from '../../utils/text';
import { groupBy, findActiveGroup, splitSequence, clearTextRanges, resolveTranscript, setChars } from './utils';
import { placeCaption } from './position';
@@ -136,7 +136,9 @@ export class GuineaCaptionDecoder implements CaptionDecoder {
const index = PSEUDO_RANDOM_SEQUENCE[this.colorIndex % PSEUDO_RANDOM_SEQUENCE.length]!;
if (this.fill) {
+ // Text draws Computed, which a direct store write does not mirror to.
store(world, Color).value[this.fill.id()] = colors[index]!;
+ store(world, Computed).color[this.fill.id()] = colors[index]!;
}
renderText(world, entity);
diff --git a/packages/runtime/src/media/caption/spotlight.ts b/packages/runtime/src/media/caption/spotlight.ts
index 3d1973c2..d66df4f4 100644
--- a/packages/runtime/src/media/caption/spotlight.ts
+++ b/packages/runtime/src/media/caption/spotlight.ts
@@ -4,7 +4,7 @@
import { store } from '../../world/store';
import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, Caption, TextRange } from '../../traits';
+import { Paint, Color, Caption, Computed, TextRange } from '../../traits';
import { renderText } from '../../utils/text';
import { groupBy, findActiveGroup, clearTextRanges, resolveTranscript, setChars } from './utils';
import { placeCaption } from './position';
@@ -118,7 +118,9 @@ export class SpotlightCaptionDecoder implements CaptionDecoder {
const highlightColor = entity.get(Caption)?.colors?.[0] ?? HIGHLIGHT_COLOR;
if (this.fill) {
+ // Text draws Computed, which a direct store write does not mirror to.
store(world, Color).value[this.fill.id()] = highlightColor;
+ store(world, Computed).color[this.fill.id()] = highlightColor;
}
renderText(world, entity);
diff --git a/packages/runtime/src/utils/text.ts b/packages/runtime/src/utils/text.ts
index 513fc4cc..0b898f38 100644
--- a/packages/runtime/src/utils/text.ts
+++ b/packages/runtime/src/utils/text.ts
@@ -10,7 +10,7 @@ import {
TextAlign, TextBaseline, TextCase,
} from '../constants';
import {
- Size, Hidden, Paint, Color, Blur, Offset, Opacity, BlendMode,
+ Size, Hidden, Paint, Color, BlendMode,
Chars, TextStyle, TextRange, TextCache, Cache, Computed, Camera,
RenderSurface, Root,
} from '../traits';
@@ -299,10 +299,6 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
const words = lines.flat();
const computed = store(world, Computed);
- const offsetStore = store(world, Offset);
- const blurStore = store(world, Blur);
- const colorStore = store(world, Color);
- const opacityStore = store(world, Opacity);
const blendStore = store(world, BlendMode);
const paintStore = store(world, Paint);
@@ -342,14 +338,14 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (shadow.has(Hidden)) continue;
const sid = shadow.id();
- // Recycled-id safety: optional traits only behind has().
- const hasOffset = shadow.has(Offset);
- ctx.shadowOffsetX = (hasOffset ? offsetStore.x[sid] ?? 0 : 0) * shadowScale;
- ctx.shadowOffsetY = (hasOffset ? offsetStore.y[sid] ?? 0 : 0) * shadowScale;
- ctx.shadowBlur = (shadow.has(Blur) ? blurStore.value[sid] ?? 0 : 0) * shadowScale;
- ctx.shadowColor = colorToHex(colorStore.value[sid] ?? 0x000000);
- ctx.fillStyle = colorToHex(colorStore.value[sid] ?? 0x000000);
- ctx.globalAlpha = savedAlpha * (shadow.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
+ // Computed, not the authored traits: keyframes animate these.
+ const color = colorToHex(computed.color[sid]!);
+ ctx.shadowOffsetX = computed.offsetX[sid]! * shadowScale;
+ ctx.shadowOffsetY = computed.offsetY[sid]! * shadowScale;
+ ctx.shadowBlur = computed.blur[sid]! * shadowScale;
+ ctx.shadowColor = color;
+ ctx.fillStyle = color;
+ ctx.globalAlpha = savedAlpha * computed.opacity[sid]!;
if (widest !== null) {
ctx.strokeText(word.chars, word.x, word.y);
@@ -393,7 +389,7 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (blendMode !== 0) {
ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode]!;
}
- const strokeAlpha = savedAlpha * (stroke.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
+ const strokeAlpha = savedAlpha * computed.opacity[sid]!;
const strokeCO = ctx.globalCompositeOperation;
applyStrokeStyle(ctx, world, stroke);
@@ -413,7 +409,7 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
} 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);
+ ctx.strokeStyle = colorToHex(computed.color[pid]!);
} else {
// A solid without a Color: a stroke whose color is unset.
continue;
@@ -421,7 +417,7 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
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.globalAlpha = own ? strokeAlpha : strokeAlpha * computed.opacity[pid]!;
ctx.strokeText(word.chars, word.x, word.y);
}
@@ -466,7 +462,7 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (blendMode !== 0) {
ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode]!;
}
- ctx.globalAlpha = savedAlpha * (fill.has(Opacity) ? opacityStore.value[fid] ?? 1 : 1);
+ ctx.globalAlpha = savedAlpha * computed.opacity[fid]!;
const paintType = paintStore.value[fid];
if (paintType === PaintType.LINEAR_GRADIENT) {
@@ -474,7 +470,7 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
} else if (paintType === PaintType.RADIAL_GRADIENT || paintType === PaintType.ANGULAR_GRADIENT) {
ctx.fillStyle = getEllipticalPattern(world, ctx, patterns ??= new Map(), fill, paintType, w, h);
} else {
- ctx.fillStyle = colorToHex(colorStore.value[fid] ?? 0x000000);
+ ctx.fillStyle = colorToHex(computed.color[fid]!);
}
ctx.fillText(word.chars, word.x, word.y);
ctx.globalCompositeOperation = savedCO;