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
Covers show whole on cards of another shape
Adarshfe00f0ba6db5
diff --git a/src/app/globals.css b/src/app/globals.cssindex eba0503f..f7b6eecc 100644--- a/src/app/globals.css+++ b/src/app/globals.css@@ -699,6 +699,13 @@ html {[data-live-stage] img, [data-live-demo] img { animation: none !important; transition: none !important; }}+/* A preset cover shown whole (cover-framing.ts) sits over a blurred copy of itself, drawn only when the frame isn't+ the cover's own 4:3 shape: in a 4:3 frame the picture covers it, so it is neither fetched nor painted. */+.cover-whole { container-type: size; }+@container (aspect-ratio < 33/25) or (aspect-ratio > 27/20) {+ .cover-whole-backdrop { background-image: var(--cover-bg); }+}+/* Menus and popovers open and close without their fade, zoom and slide when the system asks for less motion. */@media (prefers-reduced-motion: reduce) {[data-slot="dropdown-menu-content"], [data-slot="dropdown-menu-sub-content"], [data-slot="popover-content"] { animation: none !important; }diff --git a/src/components/nav/cover-thumb.tsx b/src/components/nav/cover-thumb.tsxindex 44eb41e7..73adf2b1 100644--- a/src/components/nav/cover-thumb.tsx+++ b/src/components/nav/cover-thumb.tsx@@ -62,22 +62,36 @@ export function CoverThumb({}) {const cover = useNavCover(id);const s = COVER_SIZES[size];+ // How the cover sits in a box of another shape (cover-framing.ts): a square keeps its top part by default, where faces are.+ const whole = cover?.framing?.fit === "whole";+ const img = cover && (+ // eslint-disable-next-line @next/next/no-img-element -- versioned static covers, sized by the box (no optimizer round trip)+ <img+ src={cover.thumb}+ srcSet={s.src === "both" ? `${cover.thumb} 320w, ${cover.poster} 512w` : undefined}+ sizes={s.src === "both" ? s.sizes : undefined}+ width={s.w}+ height={s.h}+ alt=""+ loading={eager ? "eager" : "lazy"}+ decoding="async"+ draggable={false}+ className="absolute inset-0 size-full object-cover"+ style={{ objectPosition: cover.framing?.focus ?? "50% 25%" }}+ />+ );return (<span aria-hidden data-cover={cover ? id : undefined} className={cn("relative block shrink-0 overflow-hidden bg-muted", s.box, className)}>- {cover ? (- // eslint-disable-next-line @next/next/no-img-element -- versioned static covers, sized by the box (no optimizer round trip)- <img- src={cover.thumb}- srcSet={s.src === "both" ? `${cover.thumb} 320w, ${cover.poster} 512w` : undefined}- sizes={s.src === "both" ? s.sizes : undefined}- width={s.w}- height={s.h}- alt=""- loading={eager ? "eager" : "lazy"}- decoding="async"- draggable={false}- className="absolute inset-0 size-full object-cover object-[50%_25%]"- />+ {cover && whole ? (+ <>+ {/* eslint-disable-next-line @next/next/no-img-element -- the same small copy, blurred behind the whole picture */}+ <img src={cover.thumb} alt="" loading={eager ? "eager" : "lazy"} decoding="async" draggable={false} className="absolute inset-0 size-full scale-125 object-cover blur-md brightness-[0.7] saturate-[1.3]" />+ <span className="absolute inset-0 grid place-items-center [container-type:size]">+ <span className="relative aspect-[4/3] w-[min(100cqw,133.334cqh)] overflow-hidden">{img}</span>+ </span>+ </>+ ) : cover ? (+ img) : icon ? (<NavIcon name={icon} className="absolute inset-0 m-auto size-[42%] max-h-6 max-w-6 text-muted-foreground" />) : null}diff --git a/src/components/preset-preview.tsx b/src/components/preset-preview.tsxindex 19552bd2..9506530e 100644--- a/src/components/preset-preview.tsx+++ b/src/components/preset-preview.tsx@@ -1,6 +1,6 @@"use client";-import { memo, useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";+import { memo, useEffect, useLayoutEffect, useRef, useState, useSyncExternalStore, type CSSProperties } from "react";import {CAMERA_MOVES,isCover,@@ -16,6 +16,7 @@ import {type PreviewLayer,type PreviewScene,} from "@/lib/preset-cards";+import { coverFraming } from "@/lib/cover-framing";import { releaseVideo } from "@/lib/media-release";import { glyphFor } from "@/lib/move-glyphs";import { autoplays, browserPlaybackEnv, createPlaybackStore, playsWhenAsked, previewKind, untilPlace, type AutoPlay } from "@/lib/preview-playback";@@ -297,7 +298,7 @@ let cycleStart: number | undefined;* 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, place }: { src: string; rest?: Preview["rest"]; asked: boolean; place?: number }) {+function Clip({ src, rest, asked, place, focus }: { src: string; rest?: Preview["rest"]; asked: boolean; place?: number; focus?: string }) {const ref = useRef<HTMLVideoElement>(null);const [ready, setReady] = useState(false);// Where it starts, fixed when it mounts: asked for, where the change starts; by itself, the frame the card rests on.@@ -350,6 +351,7 @@ function Clip({ src, rest, asked, place }: { src: string; rest?: Preview["rest"]: undefined}className={cn("absolute inset-0 size-full object-cover", rest && "transition-opacity duration-150", !ready && "opacity-0")}+ style={focus ? { objectPosition: focus } : undefined}aria-hidden/>);@@ -395,7 +397,10 @@ export const PresetPreview = memo(function PresetPreview({// mirroring (and no move glyph: the picture shows the preset itself), with its clip over it while it plays.if (preview.video || isCover(preview)) {const still = preview.rest?.src ?? preview.poster;- return (+ // How the cover sits in a frame of another shape (cover-framing.ts): a focus point, or the whole picture.+ const framing = coverFraming(still);+ const focus = framing?.focus;+ const cover = (<>{/* eslint-disable-next-line @next/next/no-img-element */}<img@@ -410,11 +415,27 @@ export const PresetPreview = memo(function PresetPreview({fetchPriority={fetchPriority}decoding={fetchPriority ? undefined : "async"}className="absolute inset-0 size-full object-cover"+ style={focus ? { objectPosition: focus } : undefined}aria-hidden/>- {preview.video && playing && <Clip src={preview.video} rest={preview.rest} asked={asked} place={place} />}+ {preview.video && playing && <Clip src={preview.video} rest={preview.rest} asked={asked} place={place} focus={focus} />}</>);+ if (framing?.fit !== "whole") return cover;+ // The whole 4:3 picture, as large as the frame allows and centred (the frame's size in container units), over+ // the still filling the frame, blurred and dimmed. That backdrop is a background only a frame of another shape+ // draws (.cover-whole in globals.css): in a 4:3 frame the picture covers it, and it is neither fetched nor painted.+ return (+ <span className="cover-whole absolute inset-0 grid place-items-center" aria-hidden>+ <span+ className="cover-whole-backdrop absolute inset-0 scale-125 bg-cover bg-center blur-xl brightness-[0.7] saturate-[1.3]"+ style={{ "--cover-bg": `url(${sizes ? thumbOf(still) : still})` } as CSSProperties}+ />+ <span className="relative aspect-[4/3] w-[min(100cqw,133.334cqh)] overflow-hidden shadow-[0_10px_30px_-6px_rgb(0_0_0/0.6)] ring-1 ring-white/10">+ {cover}+ </span>+ </span>+ );}const spec: MoveSpec | undefined = preview.move ? CAMERA_MOVES[preview.move] : undefined;diff --git a/src/components/studio/preset-chips.tsx b/src/components/studio/preset-chips.tsxindex 7a3df6b3..3de22366 100644--- a/src/components/studio/preset-chips.tsx+++ b/src/components/studio/preset-chips.tsx@@ -16,7 +16,8 @@ import { Chip, ClearableChip } from "./chip";/*** Camera-motion / effect picker and Mix. Loaded as its own chunk (with the preset icons and the slim* covers map), and the preset lists come from the full catalog chunk, fetched on first hover or open.- * A preset shows its real cover, as in the galleries, else its drawn tile. Both list them as the galleries do+ * A preset shows its real cover, as in the galleries and in their shape (4:3, so it shows whole), else its drawn tile.+ * Both list them as the galleries do* (featuredAll), with the category tabs in catalog order.*/@@ -31,7 +32,7 @@ function PresetTile({ preset: p, picked, onPick }: { preset: Preset; picked: boocover={presetCover(p.id)}playing={playing}picked={picked}- className="aspect-video w-full rounded-md"+ className="aspect-[4/3] w-full rounded-md"fallback={<span className="absolute inset-0 grid place-items-center text-white" style={{ background: `linear-gradient(135deg, ${p.look.from}, ${p.look.to})` }}><PI className="size-5 transition-transform duration-300 group-hover:scale-125" />@@ -78,7 +79,7 @@ export function PresetGrid({ list, value, onPick, noneLabel }: { list: Preset[]<CoverTilecover={undefined}picked={!value}- className="aspect-video w-full rounded-md"+ className="aspect-[4/3] w-full rounded-md"fallback={<span className="absolute inset-0 grid place-items-center text-muted-foreground"><Ban className="size-5" />@@ -105,7 +106,7 @@ const prefetch = () => void loadFullCatalog();/*** A picker opens on the side of its chip with more of the screen (below a chip in the upper half), and is as tall as* that room allows: its scroll box gives way (min-h-0), the categories and Mix's sliders keep their height, and the- * covers get up to 28rem, four rows. The side is picked as it opens, so a list still loading (a short box) doesn't keep+ * covers get up to 28rem, three rows and part of a fourth. The side is picked as it opens, so a list still loading (a short box) doesn't keep* it on the side with less room.*/const POPOVER_HEIGHT = "max-h-[var(--radix-popover-content-available-height)]";diff --git a/src/lib/cover-framing.ts b/src/lib/cover-framing.tsnew file mode 100644index 00000000..97fd1a67--- /dev/null+++ b/src/lib/cover-framing.ts@@ -0,0 +1,49 @@+/**+ * How a preset cover sits in a frame of another shape. Every preset cover is 4:3, and a few tiles aren't: the Mixed+ * Media looks are 4:5, the Canvas templates 16:9, the navigation's tiles and pills square. Such a tile fills its frame+ * from the cover's centre, which loses the edges; for a cover whose subject isn't centred, or spans the whole frame,+ * that cuts it. Hand-kept, keyed by the preset id whose cover it is (preset-covers.generated.ts): it holds wherever+ * that cover shows, the preset's own card, a template tile that shows its cover (peak/), a picker's small copy+ * (thumbs/) or a nav tile. A frame of the cover's own shape shows it whole, whatever is set here.+ *+ * - `focus`: the point of the cover kept in view (CSS object-position, "x% y%"), for a subject off centre.+ * - `fit: "whole"`: the whole cover, centred, over a blurred and darkened copy of itself filling the frame: for a+ * picture whose content reaches its edges (a car crossing the frame, panels side by side), until it has a cover of+ * the tile's shape.+ */+export interface CoverFraming {+ focus?: `${number}% ${number}%`;+ fit?: "whole";+}++export const COVER_FRAMING: Record<string, CoverFraming> = {+ // Mixed Media looks (4:5): the subject off centre.+ "effect-comic": { focus: "5% 50%" },+ "effect-akrill": { focus: "80% 50%" },+ "effect-paper": { focus: "40% 50%" },+ "effect-particles": { focus: "40% 50%" },+ // Mixed Media looks (4:5): content across the whole width.+ "effect-tracking": { fit: "whole" },+ "effect-windows": { fit: "whole" },+ "effect-multiverse": { fit: "whole" },+ // Canvas templates (16:9): the top of the subject.+ "effect-agamemnon": { focus: "50% 0%" },+ "effect-dolphin-ride": { focus: "50% 30%" },+ "effect-pearl-earring": { focus: "50% 0%" },+ // Canvas templates (16:9): content over the whole height.+ "effect-skatedog": { fit: "whole" },+ // The navigation's square tiles and pills: a reference inset at the right, a lineup across the frame.+ "app-steal-the-look": { focus: "80% 25%" },+ "app-outfit-swap": { focus: "85% 25%" },+ "app-character-swap": { focus: "85% 25%" },+ "app-ai-influencer": { focus: "13% 25%" },+};++/** The preset id of a preset cover's URL: its poster, peak frame or small copy (with or without its version). */+const PRESET_COVER = /^\/previews\/presets\/(?:peak\/)?(?:thumbs\/)?([a-z0-9-]+)\.(?:jpg|mp4)(?:\?|$)/;++/** A cover's framing by its URL (a still or a clip), or undefined: a cover of the tile's shape, or one centred. */+export function coverFraming(url: string | undefined): CoverFraming | undefined {+ const id = url && PRESET_COVER.exec(url)?.[1];+ return id && Object.hasOwn(COVER_FRAMING, id) ? COVER_FRAMING[id] : undefined;+}diff --git a/src/lib/nav-covers.ts b/src/lib/nav-covers.tsindex 02f33388..8d57bc66 100644--- a/src/lib/nav-covers.ts+++ b/src/lib/nav-covers.ts@@ -3,6 +3,7 @@* client chunk carries the covers map for the bar, the launcher, the sheets, the goal row or the next steps.*/import "server-only";+import { coverFraming, type CoverFraming } from "./cover-framing";import { coverIds } from "./nav-model";import { presetCover } from "./preset-covers";@@ -11,6 +12,8 @@ export interface NavCover {thumb: string;/** The full still, for the 4:3 cards. */poster: string;+ /** How it sits in a square tile or pill (cover-framing.ts), when it isn't centred there. */+ framing?: CoverFraming;}/** Keyed by preset id. */@@ -25,7 +28,8 @@ export function navCovers(): NavCovers {for (const id of coverIds()) {const c = presetCover(id);if (!c) throw new Error(`The navigation names ${id}, which has no cover`);- out[id] = { thumb: c.thumb, poster: c.poster };+ const framing = coverFraming(c.poster);+ out[id] = { thumb: c.thumb, poster: c.poster, ...(framing && { framing }) };}cached = out;return out;diff --git a/tests/cover-framing.test.ts b/tests/cover-framing.test.tsnew file mode 100644index 00000000..49f115b4--- /dev/null+++ b/tests/cover-framing.test.ts@@ -0,0 +1,167 @@+import fs from "node:fs";+import path from "node:path";+import { createElement, type ReactNode } from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { describe, expect, it } from "vitest";+import { CoverTile } from "@/components/cover-tile";+import { CoverThumb } from "@/components/nav/cover-thumb";+import { NavUiProvider } from "@/components/nav/nav-ui";+import { LookCard } from "@/components/shorts/preset-card";+import { PresetGrid } from "@/components/studio/preset-chips";+import { CoverArt } from "@/components/template-cover";+import { EFFECTS, MOTIONS } from "@/lib/catalog";+import { MIXED_PRESETS, mixedPreview } from "@/lib/catalog/mixed-media";+import { COVER_FRAMING, coverFraming } from "@/lib/cover-framing";+import { navCovers } from "@/lib/nav-covers";+import { presetCover, thumbOf } from "@/lib/preset-covers";+import { PRESET_COVERS } from "@/lib/preset-covers.generated";+import { templateCover } from "@/lib/template-covers";++/*+ * How a 4:3 preset cover sits in a tile of another shape (src/lib/cover-framing.ts): every entry names a real cover,+ * and every surface that shows a cover in another shape reads it (the Mixed Media looks, the Canvas templates, the+ * pickers, the navigation's squares), on the still and on the clip.+ */++const root = process.cwd();+const fileOf = (url: string) => path.join(root, "public", url.split("?")[0]);++/** A baseline JPEG's size, from its first start-of-frame marker. */+function jpegSize(file: string): { w: number; h: number } {+ const b = fs.readFileSync(file);+ let i = 2;+ while (i < b.length) {+ const marker = b[i + 1];+ const len = b.readUInt16BE(i + 2);+ if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) return { h: b.readUInt16BE(i + 5), w: b.readUInt16BE(i + 7) };+ i += 2 + len;+ }+ throw new Error(`${file}: no frame header`);+}++const style = (focus: string) => `object-position:${focus}`;++describe("the cover framing map", () => {+ const entries = Object.entries(COVER_FRAMING);++ it("names real preset covers only, each with a focus point or the whole fit", () => {+ expect(entries.length).toBeGreaterThan(0);+ for (const [id, f] of entries) {+ expect(Object.hasOwn(PRESET_COVERS, id), `${id} has a cover`).toBe(true);+ const cover = presetCover(id)!;+ expect(fs.statSync(fileOf(cover.poster), { throwIfNoEntry: false })?.size, id).toBeGreaterThan(0);+ expect(f.focus !== undefined || f.fit === "whole", `${id} sets something`).toBe(true);+ if (f.focus) {+ const m = /^(\d{1,3})% (\d{1,3})%$/.exec(f.focus);+ expect(m, `${id} focus is "x% y%"`).not.toBeNull();+ for (const n of [m![1], m![2]]) expect(Number(n)).toBeLessThanOrEqual(100);+ }+ }+ });++ it("fits whole only 4:3 covers (the whole fit draws a 4:3 box)", () => {+ for (const [id, f] of entries) {+ if (f.fit !== "whole") continue;+ const { w, h } = jpegSize(fileOf(presetCover(id)!.poster));+ expect(w * 3, `${id} is ${w}x${h}`).toBe(h * 4);+ }+ });++ it("finds a cover's framing from any of its URLs: poster, peak frame, small copies, clip", () => {+ const f = COVER_FRAMING["effect-comic"];+ for (const url of [+ "/previews/presets/effect-comic.jpg?v=12345678",+ "/previews/presets/thumbs/effect-comic.jpg?v=12345678",+ "/previews/presets/peak/effect-comic.jpg?v=12345678",+ "/previews/presets/peak/thumbs/effect-comic.jpg?v=12345678",+ "/previews/presets/effect-comic.mp4?v=12345678",+ "/previews/presets/effect-comic.jpg",+ ])+ expect(coverFraming(url), url).toBe(f);+ // A cover of its own (a tile's own shape) and a cover without an entry keep the plain centred fill.+ expect(coverFraming("/previews/templates/mixed-media/effect-comic.jpg?v=12345678")).toBeUndefined();+ expect(coverFraming("/previews/presets/effect-comic-2.jpg?v=12345678")).toBeUndefined();+ expect(coverFraming(undefined)).toBeUndefined();+ });+});++describe("the Mixed Media looks (4:5)", () => {+ const card = (id: string) => {+ const p = MIXED_PRESETS.find((m) => m.id === id)!;+ expect(p, id).toBeDefined();+ return renderToStaticMarkup(createElement(LookCard, { id: p.id, name: p.name, description: p.description, preview: mixedPreview(p), hue: p.hue, selected: false, onSelect: () => {} }));+ };++ it("keeps an off-centre subject in view", () => {+ for (const [id, focus] of [+ ["mixed-comic", "5% 50%"],+ ["mixed-akrill", "80% 50%"],+ ["mixed-paper", "40% 50%"],+ ]) {+ expect(templateCover("mixed-media", id), id).toBeDefined();+ expect(card(id), id).toContain(style(focus));+ }+ });++ it("shows a cover that spans the frame whole, in a 4:3 box over a blurred copy of itself", () => {+ for (const id of ["mixed-tracking", "mixed-windows", "mixed-multiverse"]) {+ const html = card(id);+ const poster = templateCover("mixed-media", id)!.poster;+ expect(html, id).toContain("cover-whole-backdrop");+ expect(html, id).toContain("aspect-[4/3]");+ // The blurred copy is a background the stylesheet draws only in a frame that isn't 4:3: one picture in the markup.+ expect(html, id).toContain(`--cover-bg:url(${thumbOf(poster)})`);+ expect(html.split("<img").length - 1, id).toBe(1);+ }+ });++ it("leaves a centred cover as it was", () => {+ const html = card("mixed-sketch");+ expect(html).not.toContain("object-position");+ expect(html).not.toContain("cover-whole");+ });+});++describe("the Canvas templates (16:9)", () => {+ const art = (id: string) => renderToStaticMarkup(createElement(CoverArt, { cover: templateCover("canvas", id)!, sizes: "100vw" }));++ it("keeps the top of a tall subject, and shows a full-height picture whole", () => {+ expect(art("effect-chain-agamemnon")).toContain(style("50% 0%"));+ expect(art("effect-chain-dolphin-ride")).toContain(style("50% 30%"));+ expect(art("effect-chain-pearl-earring")).toContain(style("50% 0%"));+ expect(art("effect-chain-skatedog")).toContain("aspect-[4/3]");+ expect(art("effect-chain-superstar")).not.toContain("object-position");+ });+});++describe("the pickers", () => {+ it("show covers in their own 4:3 shape, so nothing is cut", () => {+ const html = renderToStaticMarkup(createElement(PresetGrid, { list: [...MOTIONS.slice(0, 6), ...EFFECTS.slice(0, 6)], noneLabel: "None", onPick: () => {} }));+ expect(html).toContain("aspect-[4/3] w-full");+ expect(html).not.toContain("aspect-video");+ });++ it("frame a picker tile's small copy as the card does", () => {+ const html = renderToStaticMarkup(createElement(CoverTile, { cover: presetCover("effect-comic"), className: "aspect-[4/5]" }));+ expect(html).toContain(style("5% 50%"));+ });+});++describe("the navigation's squares", () => {+ const covers = navCovers();+ const Nav = NavUiProvider as (p: { covers: typeof covers; children?: ReactNode }) => ReactNode;+ const thumb = (id: string) => renderToStaticMarkup(createElement(Nav, { covers }, createElement(CoverThumb, { id, size: "tile" })));++ it("pass each cover's framing down from the server", () => {+ for (const id of ["app-steal-the-look", "app-outfit-swap", "app-character-swap", "app-ai-influencer"]) {+ expect(covers[id]?.framing, id).toBe(COVER_FRAMING[id]);+ }+ });++ it("keep a framed cover's subject in the square, and the top part of the others", () => {+ expect(thumb("app-steal-the-look")).toContain(style("80% 25%"));+ expect(thumb("app-ai-influencer")).toContain(style("13% 25%"));+ const plain = Object.keys(covers).find((id) => !covers[id].framing)!;+ expect(thumb(plain)).toContain(style("50% 25%"));+ });+});diff --git a/tests/picker-covers.test.ts b/tests/picker-covers.test.tsindex cf9092ce..616271a4 100644--- a/tests/picker-covers.test.ts+++ b/tests/picker-covers.test.ts@@ -99,6 +99,8 @@ describe("the Mix picker", () => {it("shows the cover of every motion and effect that has one, small posters only", () => {const html = grid(mixable);for (const p of covered(mixable)) expect(html, p.id).toContain(`src="${presetCover(p.id)!.thumb}"`);+ // A cover shown whole (cover-framing.ts) has one picture too: its blurred fill is a background drawn only in a+ // frame that isn't 4:3, and a picker tile is 4:3.expect(count(html, "<img")).toBe(covered(mixable).length);expect(count(html, "linear-gradient")).toBe(mixable.length - covered(mixable).length);expect(html).not.toContain("<video");