editor/examples/03-ticker.tsx
konstantin-paulus 90d0959a91 Refactor JSX elements to replace <scene> with <rect> using the scene property for root promotion
Updated multiple files to transition from using `<scene>` elements to `<rect>` elements with the `scene` property, promoting rectangle geometries to mount roots. This change enhances the JSX API by clarifying the role of scenes and ensuring consistency across examples and documentation. Additionally, modified related documentation to reflect the new usage and constraints of the `scene` property.
2026-07-24 12:01:41 +02:00

67 lines
2.2 KiB
TypeScript

/* @jsxImportSource @diffusionstudio/jsx */
/* Declarative animation: useTicker exposes the playhead as reactive
* accessors, createMemo derives values from it, and props consume the memos.
*
* dapi mount examples/03-ticker.tsx
*
* No timers, no imperative draw loop: each memo recomputes once per tick and
* every consumer below reads the cached value. Press play; scrubbing works
* too, and because the playhead is the only clock, exports and captures
* render the exact same frames.
*/
import { createMemo } from "solid-js";
import { useTicker } from "@diffusionstudio/jsx";
const LOOP = 4; // seconds per revolution
export default function Ticker() {
const { time, frame } = useTicker();
// derived values: computed once per tick, shared by every consumer
const progress = createMemo(() => (time() % LOOP) / LOOP); // 0..1 over the loop
const angle = createMemo(() => progress() * Math.PI * 2);
const hue = createMemo(() => Math.round(progress() * 360));
const eased = createMemo(() => 0.5 - Math.cos(angle()) / 2); // 0..1..0, smooth
return (
<rect scene="example-ticker" name="Ticker" width={1920} height={1080} fill="#101014">
{/* center square: rotation and corner radius both derive from progress */}
<rect
x={860}
y={440}
width={200}
height={200}
rotation={progress() * 360}
cornerRadius={16 + eased() * 84}
fill={`hsl(${hue()} 90% 60%)`}
/>
{/* orbiting dot */}
<rect
x={910 + Math.cos(angle()) * 420}
y={490 + Math.sin(angle()) * 300}
width={100}
height={100}
cornerRadius={50}
fill={`hsl(${(hue() + 120) % 360} 90% 65%)`}
/>
{/* loop progress bar */}
<rect x={80} y={980} width={1760} height={12} cornerRadius={6} fill="#ffffff" opacity={0.12} />
<rect
x={80}
y={980}
width={Math.max(progress() * 1760, 12)}
height={12}
cornerRadius={6}
fill={`hsl(${hue()} 90% 60%)`}
/>
<text x={80} y={64} width={1200} height={60} fontSize={44} fontFamily="Inter">
t = {time().toFixed(2)}s / frame {frame()} / loop {Math.round(progress() * 100)}%
</text>
</rect>
);
}