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
Body Double: its videos play only on screen, and Live draws nothing until it runs
Adarshc1fd90cf2415
diff --git a/src/components/body-double/body-double-tool.tsx b/src/components/body-double/body-double-tool.tsxindex ffc249fb..945cb638 100644--- a/src/components/body-double/body-double-tool.tsx+++ b/src/components/body-double/body-double-tool.tsx@@ -21,6 +21,7 @@ import { toastError } from "@/lib/refusals";import { postFile } from "@/lib/upload";import { cn } from "@/lib/utils";import { LiveView } from "./live-view";+import { usePlayInView } from "./use-play-in-view";type Mode = "live" | "photo" | "clip";const SUBTITLE = "A 3D body for every person: live from your camera, from a photo, or from a clip as motion capture you can animate with.";@@ -289,6 +290,13 @@ export function BodyDoubleTool({ app }: { app: { name: string; description: stri);}+/** The motion capture example, playing only while it is on screen. */+function DemoClip({ src, poster }: { src: string; poster: string }) {+ const ref = useRef<HTMLVideoElement>(null);+ usePlayInView(ref);+ return <video ref={ref} src={src} poster={poster} muted loop playsInline preload="none" className="block w-full rounded-xl bg-media" />;+}+/** A finished example of the mode: what a run gives back. */function Demo({ mode }: { mode: "photo" | "clip" }) {const [after, setAfter] = useState(true);@@ -296,7 +304,7 @@ function Demo({ mode }: { mode: "photo" | "clip" }) {const d = BODY_DOUBLE_DEMOS.clip;return (<figure className="space-y-2 rounded-2xl border bg-background p-2.5 shadow-soft">- <video src={d.video} poster={d.poster} autoPlay muted loop playsInline className="block w-full rounded-xl bg-media" />+ <DemoClip src={d.video} poster={d.poster} /><figcaption className="px-0.5 text-[12px] text-muted-foreground">A dance in, its motion capture out: the body over every frame, the pose, and an animated 3D body.</figcaption></figure>);diff --git a/src/components/body-double/live-view.tsx b/src/components/body-double/live-view.tsxindex 0835623b..e652f130 100644--- a/src/components/body-double/live-view.tsx+++ b/src/components/body-double/live-view.tsx@@ -9,6 +9,7 @@ import { toast } from "sonner";import { Button } from "@/components/ui/button";import { type DrawBody, N_VERTS } from "@/lib/body-double/hmr";import { BODY_DOUBLE_DEMOS } from "@/lib/catalog/body-double";+import { usePlayInView } from "./use-play-in-view";import { cn } from "@/lib/utils";import type { LiveReply, LiveRequest } from "./hmr.worker";@@ -177,6 +178,13 @@ function drawBody(s: Scenes, last: Found, prev: Found | null, now: number) {s.orbit.r.render(s.orbit.scene, s.orbit.cam);}+/** The finished motion capture on the stage until Live starts, playing only while it is on screen. */+function IdleClip() {+ const ref = useRef<HTMLVideoElement>(null);+ usePlayInView(ref);+ return <video ref={ref} src={BODY_DOUBLE_DEMOS.live.video} poster={BODY_DOUBLE_DEMOS.live.poster} muted loop playsInline preload="none" aria-hidden className="absolute inset-0 size-full object-cover" />;+}+type Source = { kind: "camera" } | { kind: "sample"; src: string };/**@@ -208,17 +216,7 @@ export function LiveView() {const s = makeScenes(overlayRef.current, orbitRef.current, "chrome");scenes.current = s;const r = run.current;- let frame = 0;- const idle = () => {- if (!r.on) {- s.orbit.controls.update();- s.orbit.r.render(s.orbit.scene, s.orbit.cam);- }- frame = requestAnimationFrame(idle);- };- frame = requestAnimationFrame(idle);return () => {- cancelAnimationFrame(frame);r.on = false;worker.current?.terminate();worker.current = null;@@ -416,7 +414,7 @@ export function LiveView() {<div ref={stageRef} className="relative overflow-hidden rounded-2xl bg-media" style={{ aspectRatio: "4 / 3" }}><video ref={videoRef} muted playsInline className={cn("absolute inset-0 size-full object-contain", flipped && "-scale-x-100")} /><canvas ref={overlayRef} className={cn("pointer-events-none absolute inset-0 size-full", flipped && "-scale-x-100")} aria-hidden />- {!live && <video src={BODY_DOUBLE_DEMOS.live.video} poster={BODY_DOUBLE_DEMOS.live.poster} autoPlay muted loop playsInline aria-hidden className="absolute inset-0 size-full object-cover" />}+ {!live && <IdleClip />}{status.kind !== "live" && (<div className={cn("absolute inset-0 grid p-4 text-center", status.kind === "loading" ? "place-items-center bg-black/35" : "items-end justify-items-center bg-gradient-to-t from-black/75 via-black/10 to-transparent pb-6")}>{status.kind === "loading" ? (diff --git a/src/components/body-double/use-play-in-view.ts b/src/components/body-double/use-play-in-view.tsnew file mode 100644index 00000000..6acd38d7--- /dev/null+++ b/src/components/body-double/use-play-in-view.ts@@ -0,0 +1,45 @@+"use client";++import { type RefObject, useEffect } from "react";++/**+ * A looping demo video that plays only while a quarter of it is on screen and the tab is visible, and rests (paused,+ * nothing decoding) otherwise, so it never costs the page frames it doesn't show. Under reduced motion it stays on its+ * poster.+ */+export function usePlayInView(ref: RefObject<HTMLVideoElement | null>, enabled = true) {+ useEffect(() => {+ const v = ref.current;+ if (!v || !enabled) return;+ const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;+ if (reduced) return;+ let inView = false;+ const sync = () => {+ if (inView && document.visibilityState === "visible") {+ if (v.preload !== "auto") v.preload = "auto";+ void v.play().catch(() => {});+ } else v.pause();+ };+ const io =+ typeof IntersectionObserver === "undefined"+ ? null+ : new IntersectionObserver(+ ([e]) => {+ inView = e.isIntersecting;+ sync();+ },+ { threshold: 0.25 },+ );+ if (io) io.observe(v);+ else {+ inView = true;+ sync();+ }+ document.addEventListener("visibilitychange", sync);+ return () => {+ io?.disconnect();+ document.removeEventListener("visibilitychange", sync);+ v.pause();+ };+ }, [ref, enabled]);+}diff --git a/src/components/home/body-double-showcase.tsx b/src/components/home/body-double-showcase.tsxindex 822959e2..82b35657 100644--- a/src/components/home/body-double-showcase.tsx+++ b/src/components/home/body-double-showcase.tsx@@ -1,46 +1,54 @@"use client";-import { useState } from "react";+import { useRef, useState } from "react";+import { usePlayInView } from "@/components/body-double/use-play-in-view";import { BODY_DOUBLE_DEMOS, BODY_LOOKS, type BodyLookId } from "@/lib/catalog/body-double";import { cn } from "@/lib/utils";type Shown = "motion" | BodyLookId;+const lookSrc = (look: BodyLookId) => `/samples/body-double/looks/${look}.jpg`;+/** Each look's picture fetched once, the first time its chip is pointed at, so it shows at once when picked. */+const warmed = new Set<string>();+const warm = (look: BodyLookId) => {+ if (warmed.has(look)) return;+ warmed.add(look);+ const img = new Image();+ img.decoding = "async";+ img.src = lookSrc(look);+};+/**- * Home's Body Double picture: a dance and its motion capture, playing; each look's chip shows four models on a rooftop- * as their doubles in that look, over their whole outline. Real results, no requests beyond the files.+ * Home's Body Double picture: a dance and its motion capture, playing only while it is on screen; each look's chip+ * shows four models on a rooftop as their doubles in that look, over their whole outline. One picture at a time, so the+ * page composites a single layer here.*/export function BodyDoubleShowcase({ className }: { className?: string }) {const [shown, setShown] = useState<Shown>("motion");+ const video = useRef<HTMLVideoElement>(null);+ usePlayInView(video, shown === "motion");const chips: { value: Shown; label: string }[] = [{ value: "motion", label: "Motion capture" }, ...BODY_LOOKS.map((l) => ({ value: l.value, label: l.label }))];+ const look = shown === "motion" ? null : BODY_LOOKS.find((l) => l.value === shown)!;return (<div className={cn("space-y-3", className)}><div className="relative aspect-[4/3] overflow-hidden rounded-2xl bg-media shadow-strong">- <video- src={BODY_DOUBLE_DEMOS.clip.video}- poster={BODY_DOUBLE_DEMOS.clip.poster}- autoPlay- muted- loop- playsInline- preload="metadata"- aria-label="A dance beside its Holo body double, with its 3D pose"- className={cn("absolute inset-0 size-full object-cover transition-opacity duration-300", shown === "motion" ? "opacity-100" : "opacity-0")}- />- {BODY_LOOKS.map((l) => (+ {look ? (// eslint-disable-next-line @next/next/no-img-element- <img- key={l.value}- src={`/samples/body-double/looks/${l.value}.jpg`}- alt={`Four models on a rooftop as their ${l.label} body doubles`}- loading="lazy"- decoding="async"- className={cn("absolute inset-0 size-full object-cover transition-opacity duration-300", shown === l.value ? "opacity-100" : "opacity-0")}+ <img key={look.value} src={lookSrc(look.value)} alt={`Four models on a rooftop as their ${look.label} body doubles`} decoding="async" className="absolute inset-0 size-full object-cover" />+ ) : (+ <video+ ref={video}+ src={BODY_DOUBLE_DEMOS.clip.video}+ poster={BODY_DOUBLE_DEMOS.clip.poster}+ muted+ loop+ playsInline+ preload="none"+ aria-label="A dance beside its Holo body double, with its 3D pose"+ className="absolute inset-0 size-full object-cover"/>- ))}- <span className="pointer-events-none absolute top-3 left-3 rounded-full bg-black/55 px-2.5 py-1 text-[11px] font-medium text-white backdrop-blur">- {shown === "motion" ? "Motion capture" : BODY_LOOKS.find((l) => l.value === shown)?.label}- </span>+ )}+ <span className="pointer-events-none absolute top-3 left-3 rounded-full bg-black/55 px-2.5 py-1 text-[11px] font-medium text-white">{look ? look.label : "Motion capture"}</span></div><div className="flex flex-wrap gap-1.5" role="radiogroup" aria-label="Show Body Double">{chips.map((c) => (@@ -50,9 +58,13 @@ export function BodyDoubleShowcase({ className }: { className?: string }) {role="radio"aria-checked={shown === c.value}onClick={() => setShown(c.value)}- onPointerEnter={(e) => e.pointerType === "mouse" && setShown(c.value)}+ onPointerEnter={(e) => {+ if (c.value !== "motion") warm(c.value);+ if (e.pointerType === "mouse") setShown(c.value);+ }}+ onFocus={() => c.value !== "motion" && warm(c.value)}className={cn(- "h-8 rounded-full border px-3 text-[13px] outline-none transition focus-visible:ring-2 focus-visible:ring-ring",+ "h-8 rounded-full border px-3 text-[13px] outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring",shown === c.value ? "border-foreground bg-foreground text-background" : "bg-background text-muted-foreground hover:text-foreground",)}>