Skip to content

Commit 2b1c95a

Browse files
authored
fix: keep the editor still while captureFrame extracts (#233)
1 parent b6012b8 commit 2b1c95a

2 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎src/components/canvas/shotstack-canvas.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,19 +335,21 @@ export class Canvas {
335335
): Promise<string> {
336336
const container = this.getViewportContainer();
337337
// Neutralise the editor's zoom/pan so the extract is a 1:1 output-resolution frame. The
338-
// ticker is paused around the swap so the on-screen view never renders in this transient
339-
// state; position and scale (and the ticker) are restored in `finally`.
338+
// Application draws the stage from this same ticker, so it stops for the swap: any frame drawn
339+
// while the extract is awaited would put the moved view on screen.
340+
const { ticker } = this.application;
341+
const wasRunning = ticker.started;
340342
const { x: posX, y: posY } = container.position;
341343
const { x: scaleX, y: scaleY } = container.scale;
342-
this.pauseTicker();
344+
ticker.stop();
343345
container.position.set(0, 0);
344346
container.scale.set(1, 1);
345347
try {
346348
return await this.extractBase64(container, new pixi.Rectangle(0, 0, this.edit.size.width, this.edit.size.height), options);
347349
} finally {
348350
container.position.set(posX, posY);
349351
container.scale.set(scaleX, scaleY);
350-
this.resumeTicker();
352+
if (wasRunning) ticker.start();
351353
}
352354
}
353355

‎tests/capture-frame.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import { Canvas } from "@canvas/shotstack-canvas";
6+
7+
// pixi.js ships untransformed ESM that jest can't parse; captureFrame only needs the renderer stubbed below.
8+
jest.mock("pixi.js", () => ({
9+
Application: jest.fn(),
10+
Container: jest.fn(),
11+
Graphics: jest.fn(),
12+
Rectangle: jest.fn()
13+
}));
14+
jest.mock("pixi.js/app", () => ({}));
15+
jest.mock("pixi.js/events", () => ({}));
16+
jest.mock("pixi.js/graphics", () => ({}));
17+
jest.mock("pixi.js/text", () => ({}));
18+
jest.mock("pixi.js/text-html", () => ({}));
19+
jest.mock("pixi.js/sprite-tiling", () => ({}));
20+
jest.mock("pixi.js/filters", () => ({}));
21+
jest.mock("pixi.js/mesh", () => ({}));
22+
23+
const point = (x: number, y: number) => ({
24+
x,
25+
y,
26+
set(nextX: number, nextY: number) {
27+
this.x = nextX;
28+
this.y = nextY;
29+
}
30+
});
31+
32+
describe("Canvas.captureFrame", () => {
33+
function setup({ started = true, extractFails = false } = {}) {
34+
const ticker = {
35+
started,
36+
stop: jest.fn(() => {
37+
ticker.started = false;
38+
}),
39+
start: jest.fn(() => {
40+
ticker.started = true;
41+
}),
42+
add: jest.fn(),
43+
remove: jest.fn()
44+
};
45+
const viewportContainer = { position: point(40, 30), scale: point(0.5, 0.5) };
46+
// What the screen would show if Pixi drew a frame while the extract is in flight.
47+
const duringExtract: { drawing: boolean; x: number; scale: number }[] = [];
48+
const base64 = jest.fn(async () => {
49+
duringExtract.push({ drawing: ticker.started, x: viewportContainer.position.x, scale: viewportContainer.scale.x });
50+
if (extractFails) throw new Error("extract failed");
51+
return "data:image/jpeg;base64,AAAA";
52+
});
53+
const canvas = Object.create(Canvas.prototype) as Canvas;
54+
Object.assign(canvas, {
55+
edit: { size: { width: 1920, height: 1080 } },
56+
application: { ticker, renderer: { extract: { base64 } } },
57+
viewportContainer,
58+
onTickBound: jest.fn()
59+
});
60+
return { canvas, ticker, viewportContainer, duringExtract };
61+
}
62+
63+
it("stops Pixi drawing while the viewport is moved for the extract", async () => {
64+
const { canvas, ticker, duringExtract } = setup();
65+
66+
await expect(canvas.captureFrame({ format: "jpeg" })).resolves.toBe("data:image/jpeg;base64,AAAA");
67+
68+
expect(duringExtract).toEqual([{ drawing: false, x: 0, scale: 1 }]);
69+
expect(ticker.started).toBe(true);
70+
});
71+
72+
it("restores the viewport and restarts drawing when the extract fails", async () => {
73+
const { canvas, ticker, viewportContainer } = setup({ extractFails: true });
74+
75+
await expect(canvas.captureFrame()).rejects.toThrow("extract failed");
76+
77+
expect(viewportContainer.position).toMatchObject({ x: 40, y: 30 });
78+
expect(viewportContainer.scale).toMatchObject({ x: 0.5, y: 0.5 });
79+
expect(ticker.started).toBe(true);
80+
});
81+
82+
it("leaves a stopped ticker stopped", async () => {
83+
const { canvas, ticker } = setup({ started: false });
84+
85+
await canvas.captureFrame();
86+
87+
expect(ticker.start).not.toHaveBeenCalled();
88+
expect(ticker.started).toBe(false);
89+
});
90+
});

0 commit comments

Comments
 (0)