Skip to content

Visualize Your Ideas With Code

feat: make exporting concurrent

aarthificial4f9ef8d40d9d
diff --git a/packages/core/src/Project.ts b/packages/core/src/Project.ts
index 32961732..8fd4bbe0 100644
--- a/packages/core/src/Project.ts
+++ b/packages/core/src/Project.ts
@@ -5,6 +5,9 @@ import {Size, CanvasColorSpace, CanvasOutputMimeType} from './types';
import {AudioManager} from './media';
import {ifHot} from './utils';
+const EXPORT_FRAME_LIMIT = 256;
+const EXPORT_RETRY_DELAY = 1000;
+
export const ProjectSize: Record<string, Size> = {
FullHD: new Size(1920, 1080),
};
@@ -132,6 +135,7 @@ export class Project {
private background: string | false;
private canvas: HTMLCanvasElement;
private context: CanvasRenderingContext2D;
+ private exportCounter = 0;
private width: number;
private height: number;
@@ -236,6 +240,12 @@ export class Project {
if (this.currentScene.current) {
await this.currentScene.current.reset(this.previousScene);
}
+ if (
+ !this.currentScene.current ||
+ this.currentScene.current.isAfterTransitionIn()
+ ) {
+ this.previousScene = null;
+ }
}
}
@@ -292,7 +302,7 @@ export class Project {
return finished;
}
- public async export() {
+ public async export(isStill = false) {
const frame = this.frame;
if (this.renderLookup[frame]) {
@@ -300,25 +310,24 @@ export class Project {
return;
}
- await ifHot(
- hot =>
- new Promise<void>((resolve, reject) => {
- setTimeout(() => {
- delete this.renderLookup[frame];
- reject(`Connection timeout when exporting frame no. ${frame}`);
- }, 1000);
- this.renderLookup[frame] = () => {
- delete this.renderLookup[frame];
- resolve();
- };
- hot.send('motion-canvas:export', {
- frame,
- data: this.canvas.toDataURL(this._fileType, this._quality),
- mimeType: this._fileType,
- project: this.name,
- });
- }),
- );
+ await ifHot(async hot => {
+ while (this.exportCounter > EXPORT_FRAME_LIMIT) {
+ await new Promise(resolve => setTimeout(resolve, EXPORT_RETRY_DELAY));
+ }
+
+ this.exportCounter++;
+ this.renderLookup[frame] = () => {
+ this.exportCounter--;
+ delete this.renderLookup[frame];
+ };
+ hot.send('motion-canvas:export', {
+ frame,
+ isStill,
+ data: this.canvas.toDataURL(this._fileType, this._quality),
+ mimeType: this._fileType,
+ project: this.name,
+ });
+ });
}
public syncAudio(frameOffset = 0) {
diff --git a/packages/core/src/player/Player.ts b/packages/core/src/player/Player.ts
index f40ac33f..7c43c8f9 100644
--- a/packages/core/src/player/Player.ts
+++ b/packages/core/src/player/Player.ts
@@ -224,6 +224,10 @@ export class Player {
this.updateState({muted: !value});
}
+ public async exportCurrentFrame() {
+ await this.project.export(true);
+ }
+
private async run() {
const commands = this.consumeCommands();
const state = {...this.state.current};
diff --git a/packages/core/src/vite-env.d.ts b/packages/core/src/vite-env.d.ts
index c6788edf..c776d726 100644
--- a/packages/core/src/vite-env.d.ts
+++ b/packages/core/src/vite-env.d.ts
@@ -11,6 +11,7 @@ declare module 'vite/types/customEvent' {
data: string;
mimeType: string;
project: string;
+ isStill: boolean;
};
'motion-canvas:export-ack': {frame: number};
'motion-canvas:assets': {urls: string[]};
diff --git a/packages/ui/src/components/controls/Controls.module.scss b/packages/ui/src/components/controls/Controls.module.scss
index de1f1d4a..a1164c68 100644
--- a/packages/ui/src/components/controls/Controls.module.scss
+++ b/packages/ui/src/components/controls/Controls.module.scss
@@ -172,6 +172,9 @@
.pause:after {
-webkit-mask-image: url('../../img/icons/pause.svg');
}
+.photoCamera:after {
+ -webkit-mask-image: url('../../img/icons/photo_camera.svg');
+}
.play:after {
-webkit-mask-image: url('../../img/icons/play_arrow.svg');
}
diff --git a/packages/ui/src/components/controls/IconType.ts b/packages/ui/src/components/controls/IconType.ts
index 0f626319..5d774ad3 100644
--- a/packages/ui/src/components/controls/IconType.ts
+++ b/packages/ui/src/components/controls/IconType.ts
@@ -2,6 +2,7 @@ export enum IconType {
dragIndicator = 'dragIndicator',
motionCanvas = 'motionCanvas',
pause = 'pause',
+ photoCamera = 'photoCamera',
play = 'play',
repeat = 'repeat',
skipNext = 'skipNext',
diff --git a/packages/ui/src/components/playback/PlaybackControls.tsx b/packages/ui/src/components/playback/PlaybackControls.tsx
index 301f1dde..1ad64265 100644
--- a/packages/ui/src/components/playback/PlaybackControls.tsx
+++ b/packages/ui/src/components/playback/PlaybackControls.tsx
@@ -93,6 +93,10 @@ export function PlaybackControls() {
<Input size={4} readOnly value={`${framerate} FPS`} />
)}
/>
+ <IconButton
+ icon={IconType.photoCamera}
+ onClick={() => player.exportCurrentFrame()}
+ />
</div>
);
}
diff --git a/packages/ui/src/img/icons/photo_camera.svg b/packages/ui/src/img/icons/photo_camera.svg
new file mode 100644
index 00000000..330c5e78
--- /dev/null
+++ b/packages/ui/src/img/icons/photo_camera.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"><path d="M0 0h24v24H0z" fill="none"/><circle cx="12" cy="12" r="3.2"/><path d="M9 2L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2h-3.17L15 2H9zm3 15c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"/></svg>
\ No newline at end of file
diff --git a/packages/vite-plugin/src/main.ts b/packages/vite-plugin/src/main.ts
index 5dff274e..a2d332cd 100644
--- a/packages/vite-plugin/src/main.ts
+++ b/packages/vite-plugin/src/main.ts
@@ -305,10 +305,12 @@ export default ({
});
server.ws.on(
'motion-canvas:export',
- async ({frame, mimeType, data, project}, client) => {
- const name = frame.toString().padStart(6, '0');
+ async ({frame, mimeType, data, project, isStill}, client) => {
const extension = mime.extension(mimeType);
- const file = path.join(outputPath, project, name + '.' + extension);
+ const name = frame.toString().padStart(6, '0') + '.' + extension;
+ const file = isStill
+ ? path.join(outputPath, 'still', project, name)
+ : path.join(outputPath, project, name);
const directory = path.dirname(file);
if (!fs.existsSync(directory)) {