The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Home: a row's clips start apart however long each takes to load
Adarsh1e5191ebb5a3
diff --git a/src/components/home/home-islands.tsx b/src/components/home/home-islands.tsxindex cbd6a024..9e2f4918 100644--- a/src/components/home/home-islands.tsx+++ b/src/components/home/home-islands.tsx@@ -77,7 +77,7 @@ export function StudioCard({line,card,zoom,- delay,+ place,sizes,}: {href: string;@@ -85,14 +85,14 @@ export function StudioCard({line: string;card: PresetCardData;zoom?: boolean;- delay?: number;+ place?: number;sizes?: string;}) {const { ref, playing, asked, handlers } = usePreviewPlayback<HTMLAnchorElement>(card.id, card.preview, { auto: "touch" });return (<IntentLink ref={ref} href={href} {...handlers} className="group/studio flex min-w-0 flex-col gap-2 rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-ring"><StudioPicture zoom={zoom}>- <PresetPreview preview={card.preview} hue={card.hue} playing={playing} asked={asked} delay={delay} sizes={sizes} />+ <PresetPreview preview={card.preview} hue={card.hue} playing={playing} asked={asked} place={place} sizes={sizes} /></StudioPicture><StudioName label={label} line={line} /></IntentLink>diff --git a/src/components/home/home-sections.tsx b/src/components/home/home-sections.tsxindex 6fc2cffa..2745ddb7 100644--- a/src/components/home/home-sections.tsx+++ b/src/components/home/home-sections.tsx@@ -88,7 +88,7 @@ function Cards({ items, className, wide = items.length, lg = wide }: { items: Ho<HomeRail className={cn("gap-0 scroll-px-2.5 max-sm:px-2.5 sm:-mx-1.5", className)}>{items.map(({ card, caption }, i) => (<div key={card.id} className={cn(RAIL_ITEM, i >= wide && "sm:hidden", i >= lg && "lg:max-xl:hidden")}>- <PresetCard preset={card} href={presetHref(card.id)} caption={caption} delay={staggered(i)} sizes={CARD_SIZES} bare />+ <PresetCard preset={card} href={presetHref(card.id)} caption={caption} place={staggered(i)} sizes={CARD_SIZES} bare /></div>))}</HomeRail>@@ -214,7 +214,7 @@ export function StudiosSection({ facts, studios, audioTools, pills }: Pick<HomeS{studios.map((s, i) => (<div key={s.href} className={RAIL_ITEM}>{s.card ? (- <StudioCard href={s.href} label={s.label} line={s.line} card={s.card} zoom={!s.card.preview.video} delay={staggered(i)} sizes={STUDIO_SIZES} />+ <StudioCard href={s.href} label={s.label} line={s.line} card={s.card} zoom={!s.card.preview.video} place={staggered(i)} sizes={STUDIO_SIZES} />) : (<AudioCard href={s.href} label={s.label} line={s.line} tools={audioTools} />)}diff --git a/src/components/home/showcase-tile.tsx b/src/components/home/showcase-tile.tsxindex 618ac745..3740b435 100644--- a/src/components/home/showcase-tile.tsx+++ b/src/components/home/showcase-tile.tsx@@ -41,7 +41,7 @@ export function ShowcaseTile({ card, kind, caption, href, index }: { card: Presehue={card.hue}playing={playing}asked={asked}- delay={staggered(index)}+ place={staggered(index)}sizes={SIZES}loading={index < 4 ? "eager" : "lazy"}fetchPriority={index === 0 ? "high" : undefined}diff --git a/src/components/preset-gallery.tsx b/src/components/preset-gallery.tsxindex 924880eb..8f826c42 100644--- a/src/components/preset-gallery.tsx+++ b/src/components/preset-gallery.tsx@@ -37,7 +37,7 @@ export const PresetCard = memo(function PresetCard({compact,bare,caption,- delay,+ place,sizes,engine,gpu,@@ -50,8 +50,8 @@ export const PresetCard = memo(function PresetCard({bare?: boolean;/** A short line in place of the description (Home's rows); the description stays the tooltip. */caption?: string;- /** How long (ms) its clip waits on the still before it plays by itself (PresetPreview). */- delay?: number;+ /** Where in the shared cycle its clip starts when it plays by itself (PresetPreview). */+ place?: number;/** How wide its picture shows (PresetPreview). */sizes?: string;/** Settings → Engine mode: with one engine locked in, the other's chip would suggest a route the user turned off. */@@ -72,7 +72,7 @@ export const PresetCard = memo(function PresetCard({className="group/card flex min-w-0 flex-col gap-2 rounded-xl p-1.5 transition-colors outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring"><div className="relative aspect-[4/3] overflow-hidden rounded-lg bg-muted">- <PresetPreview preview={preset.preview} hue={preset.hue} playing={playing} asked={asked} delay={delay} sizes={sizes} flip={previewFlip(preset)} />+ <PresetPreview preview={preset.preview} hue={preset.hue} playing={playing} asked={asked} place={place} sizes={sizes} flip={previewFlip(preset)} />{!bare && (<><span className="pointer-events-none absolute inset-x-0 bottom-0 h-12 bg-gradient-to-t from-black/40 to-transparent" />diff --git a/src/components/preset-preview.tsx b/src/components/preset-preview.tsxindex 0d3cac28..19552bd2 100644--- a/src/components/preset-preview.tsx+++ b/src/components/preset-preview.tsx@@ -18,7 +18,7 @@ import {} from "@/lib/preset-cards";import { releaseVideo } from "@/lib/media-release";import { glyphFor } from "@/lib/move-glyphs";-import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind, type AutoPlay } from "@/lib/preview-playback";+import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind, untilPlace, type AutoPlay } from "@/lib/preview-playback";import { cn } from "@/lib/utils";/** Which previews play, shared by every card and the preset header (rules in lib/preview-playback.ts). */@@ -279,6 +279,9 @@ function Fx({ fx, scene }: { fx: NonNullable<Preview["fx"]>; scene: PreviewScene}}+/** When the clips' shared cycle began (performance.now()), set by the first clip ready to play by itself (Clip). */+let cycleStart: number | undefined;+/*** A real cover's clip, mounted only while it plays (a browser allows a page only so many live* players, a phone fewer), over the poster until its first frame is ready. The poster is that@@ -287,35 +290,47 @@ function Fx({ fx, scene }: { fx: NonNullable<Preview["fx"]>; scene: PreviewScene* A card resting on the clip's peak frame (`rest`) starts from that frame when it plays by itself,* and shows once the seek there is done, with a short fade. Its loop comes back to just before the* change starts (`rest.from`), not to the clip's still opening, and a hover or tap (`asked`) starts- * there, so the change builds up at once. `delay` (ms) holds a clip that plays by itself on the still- * for a moment first, so the clips of a row don't all loop in step.+ * there, so the change builds up at once.+ *+ * `place` (staggered) holds a clip that plays by itself on its first frame, once it's ready, until+ * the shared clock reaches that place in its cycle (untilPlace): the clips of a row start apart+ * however long each one takes to load, so they never loop in step. The clock starts with the first+ * clip ready, which plays at once. A hover or tap never waits.*/-function Clip({ src, rest, asked, delay = 0 }: { src: string; rest?: Preview["rest"]; asked: boolean; delay?: number }) {+function Clip({ src, rest, asked, place }: { src: string; rest?: Preview["rest"]; asked: boolean; place?: number }) {const ref = useRef<HTMLVideoElement>(null);const [ready, setReady] = useState(false);- const [due, setDue] = useState(!delay);- // Where it starts, fixed when it does: asked for, at once; by itself, once its delay is over.- const [start, setStart] = useState<number>();- if (start === undefined && (asked || due)) setStart(rest ? (asked ? rest.from : rest.at) : 0);- useEffect(() => {- if (due) return;- const t = setTimeout(() => setDue(true), delay);- return () => clearTimeout(t);- }, [due, delay]);- const started = start !== undefined;+ // Where it starts, fixed when it mounts: asked for, where the change starts; by itself, the frame the card rests on.+ const [start] = useState(() => (rest ? (asked ? rest.from : rest.at) : 0));+ const [due, setDue] = useState(place === undefined || asked);+ if (!due && asked) setDue(true);// A layout effect: a page kept hidden after navigating away (Activity) keeps its elements, and a// video only stops when told to. The source is set here too, so a page shown again plays again.+ // It plays at once, which is what makes a phone load it; a clip with a place to wait for holds below.useLayoutEffect(() => {const v = ref.current;- if (!v || !started) return;+ if (!v) return;v.src = src;void v.play().catch(() => {});return () => {releaseVideo(v);setReady(false);};- }, [src, started]);- const seeks = !!start;+ }, [src]);+ useEffect(() => {+ const v = ref.current;+ if (!v || !ready) return;+ if (due) {+ if (v.paused) void v.play().catch(() => {});+ return;+ }+ v.pause();+ const now = performance.now();+ cycleStart ??= now - place!;+ const t = setTimeout(() => setDue(true), untilPlace(place!, now - cycleStart));+ return () => clearTimeout(t);+ }, [ready, due, place]);+ const seeks = start > 0;return (<videoref={ref}@@ -351,7 +366,7 @@ export const PresetPreview = memo(function PresetPreview({hue,playing,asked = false,- delay,+ place,flip,loading = "lazy",fetchPriority,@@ -362,8 +377,8 @@ export const PresetPreview = memo(function PresetPreview({playing: boolean;/** It plays because the viewer asked (usePreviewPlayback's `asked`): a clip resting on its peak starts where the change starts. */asked?: boolean;- /** How long (ms) a clip that plays by itself waits on its still first (Clip). */- delay?: number;+ /** Where (ms into the shared cycle, staggered) a clip that plays by itself starts, holding its first frame till then (Clip). */+ place?: number;/** Mirror the still (see previewFlip). */flip?: boolean;/** A real cover's still: "eager" above the fold (Home's wall), lazy everywhere else. */@@ -397,7 +412,7 @@ export const PresetPreview = memo(function PresetPreview({className="absolute inset-0 size-full object-cover"aria-hidden/>- {preview.video && playing && <Clip src={preview.video} rest={preview.rest} asked={asked} delay={delay} />}+ {preview.video && playing && <Clip src={preview.video} rest={preview.rest} asked={asked} place={place} />}</>);}diff --git a/src/lib/preview-playback.ts b/src/lib/preview-playback.tsindex 8f6d96b1..b02d9a4b 100644--- a/src/lib/preview-playback.ts+++ b/src/lib/preview-playback.ts@@ -52,12 +52,21 @@ export const NARROW = 640;export const maxPlaying = (viewportWidth: number) => (viewportWidth < NARROW ? MAX_PLAYING.narrow : MAX_PLAYING.wide);+/** The cycle a row's clips start on (Home's wall and rows): eight places, 350 ms apart. */+export const STAGGER_CYCLE = 2800;++/**+ * The place (ms into STAGGER_CYCLE) where the clip of a row's `i`th card starts playing by itself: the clips of a row+ * start apart, so they never loop in step (every effect clip is 4 s long) and some card is always at its height.+ */+export const staggered = (i: number) => (i % 8) * (STAGGER_CYCLE / 8);+/**- * How long (ms) the clip of a row's `i`th card waits on its still before it plays by itself (Home's wall and rows):- * the clips picked together start apart, so they never loop in step (every effect clip is 4 s long) and some card- * is always at its height.+ * How long (ms) from `now` (performance.now()) until the shared clock next reaches `place` in the cycle. A clip waits+ * for that once it's ready, not for a fixed time from when it was asked for, so clips that take longer or shorter to+ * load still start at their own places.*/-export const staggered = (i: number) => (i % 8) * 350;+export const untilPlace = (place: number, now: number) => (((place - now) % STAGGER_CYCLE) + STAGGER_CYCLE) % STAGGER_CYCLE;/** What a preview shows: a real cover's clip, a procedural animation, or a still. */export type PreviewKind = "clip" | "procedural" | "still";diff --git a/tests/shell-logic.test.ts b/tests/shell-logic.test.tsindex 84d5368e..1e14bbd9 100644--- a/tests/shell-logic.test.ts+++ b/tests/shell-logic.test.ts@@ -30,7 +30,9 @@ import {playsWhenAsked,previewKind,SCROLL_SETTLE,+ STAGGER_CYCLE,staggered,+ untilPlace,type PlaybackEnv,type PlaybackStore,} from "@/lib/preview-playback";@@ -487,11 +489,23 @@ describe("preview playback store", () => {expect([store.isPlaying("r0c0"), store.isAsked("r0c0")]).toEqual([true, false]);});- it("starts the clips of a row apart (Home), each within three seconds", () => {- const waits = Array.from({ length: 8 }, (_, i) => staggered(i));- expect(waits[0]).toBe(0);- expect(new Set(waits).size).toBe(8);- expect(Math.max(...waits)).toBeLessThan(3000);+ it("starts the clips of a row at their own places in a shared cycle, however long each took to load (Home)", () => {+ const places = Array.from({ length: 8 }, (_, i) => staggered(i));+ expect(places[0]).toBe(0);+ expect(new Set(places).size).toBe(8);+ expect(Math.max(...places)).toBeLessThan(STAGGER_CYCLE);+ // Ready at any moment, each clip waits for its place: they start apart by its distance from the others'.+ for (const now of [0, 1234.5, 99_999.9]) {+ const starts = places.map((p) => now + untilPlace(p, now));+ for (const [i, s] of starts.entries()) {+ expect(s - now).toBeGreaterThanOrEqual(0);+ expect(s - now).toBeLessThan(STAGGER_CYCLE);+ expect((((s - places[i]) % STAGGER_CYCLE) + STAGGER_CYCLE) % STAGGER_CYCLE).toBeCloseTo(0, 6);+ }+ }+ // One ready late waits for its next turn, not a fixed time.+ expect(untilPlace(350, 4 * STAGGER_CYCLE + 350)).toBe(0);+ expect(untilPlace(350, 4 * STAGGER_CYCLE + 400)).toBeCloseTo(STAGGER_CYCLE - 50, 6);});it("with reduced motion a hover or tap plays a clip, but a procedural preview keeps its poster", () => {