Visualize Your Ideas With Code
feat: configurable framerate and resolution
aarthificiala715f5c1acd2
diff --git a/src/Project.ts b/src/Project.tsindex c24a6701..a6d0dcba 100644--- a/src/Project.ts+++ b/src/Project.ts@@ -14,11 +14,11 @@ Konva.autoDrawEnabled = false;export const ProjectSize = {FullHD: {width: 1920, height: 1080},-}+};interface ProjectConfig extends Partial<StageConfig> {- scenes: SceneRunner[],- background: string | false,+ scenes: SceneRunner[];+ background: string | false;}export class Project extends Stage {@@ -30,7 +30,6 @@ export class Project extends Stage {public readonly master: Layer;public readonly center: Vector2d;public threadsCallback: ThreadsCallback;- public framesPerSeconds = 30;public frame: number = 0;public get time(): number {@@ -45,15 +44,27 @@ export class Project extends Stage {return this.currentThread;}+ public get framerate(): number {+ return this.framesPerSeconds;+ }++ public set framerate(value: number) {+ this.framesPerSeconds = value;+ this.reloadAll();+ }++ public set resolutionScale(value: number) {+ this.master.canvas.setPixelRatio(value);+ }++ private framesPerSeconds = 30;private readonly scenesChanged = new SimpleEventDispatcher<Scene[]>();private readonly sceneLookup: Record<string, Scene> = {};private previousScene: Scene = null;private currentScene: Scene = null;private currentThread: Thread = null;- public constructor(- config: ProjectConfig,- ) {+ public constructor(config: ProjectConfig) {const {scenes, ...rest} = config;super({listening: false,@@ -78,7 +89,6 @@ export class Project extends Stage {});this.master = new Layer({name: 'master'});- this.master.canvas.pixelRatio = 1;this.master.add(this.background);this.add(this.master);@@ -115,6 +125,12 @@ export class Project extends Stage {}}+ public reloadAll() {+ for (const scene of Object.values(this.sceneLookup)) {+ scene.reload();+ }+ }+public async next(speed: number = 1): Promise<boolean> {if (this.previousScene) {await this.previousScene.next();diff --git a/src/Scene.ts b/src/Scene.tsindex b4acce45..f8c1dfed 100644--- a/src/Scene.ts+++ b/src/Scene.ts@@ -134,7 +134,7 @@ export class Scene extends Group {const value = await result.value;result = this.runner.next(value);} else {- console.log('Invalid value: ', result.value);+ console.warn('Invalid value: ', result.value);result = this.runner.next();}this.updateLayout();diff --git a/src/player/Player.ts b/src/player/Player.tsindex 36ae8273..3ea4390f 100644--- a/src/player/Player.ts+++ b/src/player/Player.ts@@ -18,6 +18,8 @@ export interface PlayerState {speed: number;render: boolean;muted: boolean;+ fps: number;+ scale: number;}export interface PlayerTime {@@ -95,6 +97,8 @@ export class Player {speed: 1,render: false,muted: true,+ fps: 30,+ scale: 1,};private frame: number = 0;@@ -108,9 +112,6 @@ export class Player {public updateState(newState: Partial<PlayerState>) {let changed = false;for (const prop in newState) {- if (prop === 'frame') {- continue;- }// @ts-ignoreif (newState[prop] !== this.state[prop]) {changed = true;@@ -155,14 +156,15 @@ export class Player {const savedState = localStorage.getItem(STORAGE_KEY);if (savedState) {const state = JSON.parse(savedState) as PlayerState;- this.state.paused = state.paused;- this.state.startFrame = state.startFrame;- this.state.endFrame = state.endFrame ?? Infinity;- this.state.loop = state.loop;- this.state.speed = state.speed;- this.state.muted = state.muted;+ for (const key in this.state) {+ //@ts-ignore+ this.state[key] = state[key] ?? this.state[key];+ }}+ this.project.framerate = this.state.fps;+ this.project.resolutionScale = this.state.scale;+if (audio) {this.audioElement = new Audio(audio.src);}@@ -184,6 +186,24 @@ export class Player {this.audio.meta = meta;}+ public setFramerate(fps: number) {+ const ratio = fps / this.state.fps;+ this.project.framerate = fps;+ this.updateState({+ fps,+ startFrame: Math.floor(this.state.startFrame * ratio),+ endFrame: Math.floor(this.state.endFrame * ratio),+ });+ this.requestSeek(Math.floor(this.frame * ratio));+ this.reload();+ }++ public setScale(scale: number) {+ this.project.resolutionScale = scale;+ this.updateState({scale});+ this.project.draw();+ }+public requestNextFrame(): void {this.commands.seek = this.frame + 1;}@@ -193,7 +213,7 @@ export class Player {}public requestSeek(value: number): void {- this.commands.seek = this.inRange(value, this.state);+ this.commands.seek = this.clampRange(value, this.state);}public togglePlayback(value: boolean = this.state.paused): void {@@ -226,7 +246,8 @@ export class Player {const startTime = performance.now();await this.project.recalculate();const duration = this.project.frame;- const finished = await this.project.seek(this.frame);+ const frame = commands.seek < 0 ? this.frame : commands.seek;+ const finished = await this.project.seek(frame);this.project.draw();this.updateState({duration,@@ -289,7 +310,7 @@ export class Player {// Seek to the given frameif (commands.seek >= 0 || !this.isInRange(this.project.frame, state)) {const seekFrame = commands.seek < 0 ? this.project.frame : commands.seek;- const clampedFrame = this.inRange(seekFrame, state);+ const clampedFrame = this.clampRange(seekFrame, state);console.time('seek time');state.finished = await this.project.seek(clampedFrame, state.speed);console.timeEnd('seek time');@@ -342,7 +363,7 @@ export class Player {this.request();}- private inRange(frame: number, state: PlayerState): number {+ private clampRange(frame: number, state: PlayerState): number {return frame > state.endFrame? state.endFrame: frame < state.startFrame@@ -374,7 +395,7 @@ export class Player {private request() {this.requestId = requestAnimationFrame(async time => {- if (time - this.renderTime >= 990 / this.project.framesPerSeconds) {+ if (time - this.renderTime >= 990 / this.state.fps) {this.renderTime = time;try {await this.run();