Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Sharpen shot thumbnails

Bitti5515c41d8d12
diff --git a/src/components/workspace/ShotThumbnail.test.tsx b/src/components/workspace/ShotThumbnail.test.tsx
index eebcd6d..18a7d97 100644
--- a/src/components/workspace/ShotThumbnail.test.tsx
+++ b/src/components/workspace/ShotThumbnail.test.tsx
@@ -14,7 +14,11 @@ afterEach(cleanup);
* actually draws. jsdom does none of that, so it is stood in for — and every
* seek the component asks for is recorded, because WHICH frame each card gets
* is the whole point of this component. */
-async function withFakeFile(file: { seconds: number }, body: (seen: { seeks: number[]; reads: () => number }) => Promise<void>) {
+async function withFakeFile(file: { seconds: number }, body: (seen: {
+ seeks: number[];
+ reads: () => number;
+ posters: () => Array<{ width: number; height: number; type: string; quality: number | undefined }>;
+}) => Promise<void>) {
const { invoke } = await import("@tauri-apps/api/core");
(window as unknown as Record<string, unknown>).__TAURI_INTERNALS__ = {};
vi.mocked(invoke).mockResolvedValue(new ArrayBuffer(8));
@@ -30,9 +34,11 @@ async function withFakeFile(file: { seconds: number }, body: (seen: { seeks: num
HTMLMediaElement.prototype.pause = vi.fn();
HTMLCanvasElement.prototype.getContext = (() => ({ drawImage: () => undefined })) as unknown as typeof getContext;
const seeks: number[] = [];
+ const posters: Array<{ width: number; height: number; type: string; quality: number | undefined }> = [];
// The drawn frame is named after the moment it was taken from, so a test can
// tell one card's picture from another's.
- HTMLCanvasElement.prototype.toDataURL = function () {
+ HTMLCanvasElement.prototype.toDataURL = function (this: HTMLCanvasElement, type?: string, quality?: number) {
+ posters.push({ width: this.width, height: this.height, type: type ?? "", quality });
return `data:image/jpeg;base64,frame-${seeks.length === 0 ? 0 : seeks[seeks.length - 1]}`;
} as unknown as typeof toDataURL;
const create = document.createElement.bind(document);
@@ -60,7 +66,7 @@ async function withFakeFile(file: { seconds: number }, body: (seen: { seeks: num
return element;
});
try {
- await body({ seeks, reads: () => vi.mocked(invoke).mock.calls.length });
+ await body({ seeks, reads: () => vi.mocked(invoke).mock.calls.length, posters: () => posters });
} finally {
spy.mockRestore();
HTMLMediaElement.prototype.load = load;
@@ -98,7 +104,7 @@ describe("a shot's picture", () => {
const folder = "C:\\Exact first frame";
const job = rendered(folder);
await withFakeFile({ seconds: 9 }, async (seen) => {
- render(<ShotThumbnail folderPath={folder} job={job} seconds={0} shotNumber={1} posterOffsetSeconds={0} />);
+ render(<ShotThumbnail folderPath={folder} job={job} seconds={0} shotNumber={1} />);
const control = await screen.findByRole("button", { name: "Play shot 1 of First scene" });
const picture = control.querySelector("img") as HTMLImageElement;
expect(seen.seeks).toEqual([]);
@@ -124,11 +130,14 @@ describe("a shot's picture", () => {
const pictures = drawn().map((control) => control.querySelector("img") as HTMLImageElement);
// The file was read once, for both cards.
expect(seen.reads()).toBe(1);
- /* And each card got ITS shot's frame: a quarter of a second past the cut,
- which is inside the shot and past a fade-in that would otherwise hand
- the first card a black rectangle. */
- expect(seen.seeks).toEqual([0.25, 4.75]);
+ // Each card gets the exact frame where its shot starts. Frame zero is
+ // already loaded, so only shot 2 requires an explicit seek.
+ expect(seen.seeks).toEqual([4.5]);
expect(pictures[0].src).not.toBe(pictures[1].src);
+ expect(seen.posters()).toEqual([
+ { width: 640, height: 360, type: "image/jpeg", quality: 0.92 },
+ { width: 640, height: 360, type: "image/jpeg", quality: 0.92 },
+ ]);
});
});
diff --git a/src/components/workspace/ShotThumbnail.tsx b/src/components/workspace/ShotThumbnail.tsx
index b2186b8..f0d160c 100644
--- a/src/components/workspace/ShotThumbnail.tsx
+++ b/src/components/workspace/ShotThumbnail.tsx
@@ -21,19 +21,16 @@ import { sceneDurationSeconds, type GenerationJob } from "../../lib/project";
* As in MediaThumbnail, no FFmpeg and no WebCodecs: the <video> element already
* owns a hardware decoder and one still frame is all this needs. */
-const THUMB_WIDTH = 320;
+// Two physical pixels per typical 320px card keeps posters sharp on high-DPI
+// displays without retaining full-resolution render frames in the cache.
+const THUMB_WIDTH = 640;
/** How long a seek may take before the frame it was going to draw is given up
* on. A seek that neither lands nor errors settles nothing, and awaiting it
* would leave every card behind it in the queue spinning for the life of the
* panel. */
const SEEK_LIMIT_MS = 4_000;
-/** How far INTO a shot its picture is taken from. The frame exactly on a cut is
- * the first frame of the shot, which on a fade-in is a black rectangle — and a
- * black card reads as a broken thumbnail rather than as a shot that opens
- * dark. A quarter of a second is inside every shot there can be, because cuts
- * move in half-second steps (`STEP_SECONDS`), and is still the shot's own
- * picture rather than the one before it. */
-const POSTER_OFFSET_SECONDS = 0.25;
+/** Posters represent the cut itself: every shot card shows the exact frame at
+ * its own startSeconds. */
/** Small JPEGs, but not free. Bounded like MediaThumbnail's own cache. */
const CACHE_LIMIT = 96;
const PLAY_EVENT = "polstudio:play-generated-shot";
@@ -105,7 +102,7 @@ function tryDraw(video: HTMLVideoElement): string | null {
const context = canvas.getContext("2d");
if (!context) return null;
context.drawImage(video, 0, 0, canvas.width, canvas.height);
- return canvas.toDataURL("image/jpeg", 0.72);
+ return canvas.toDataURL("image/jpeg", 0.92);
} catch {
return null;
}
@@ -239,7 +236,7 @@ function placeholder(job: GenerationJob, failed: boolean, cancelling: boolean):
}
}
-export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDurationSeconds(job), shotNumber, posterOffsetSeconds = POSTER_OFFSET_SECONDS, cancelling = false }: {
+export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDurationSeconds(job), shotNumber, posterOffsetSeconds = 0, cancelling = false }: {
folderPath: string;
/** The scene this shot belongs to: it owns the file and the status. */
job: GenerationJob;
@@ -248,7 +245,7 @@ export function ShotThumbnail({ folderPath, job, seconds, endSeconds = sceneDura
/** The next cut (or scene end). Playback never crosses this point. */
endSeconds?: number;
shotNumber: number;
- /** Scene cards use frame zero; shot cards offset past a possible fade-in. */
+ /** Optional caller-selected offset from the exact cut. */
posterOffsetSeconds?: number;
/** Cancel was requested but the engine has not reported its terminal state. */
cancelling?: boolean;