Skip to content

Visualize Your Ideas With Code

feat: time events

aarthificialf47cc666f64e
diff --git a/src/Project.ts b/src/Project.ts
index ce969e7e..a7480849 100644
--- a/src/Project.ts
+++ b/src/Project.ts
@@ -55,11 +55,11 @@ export class Project extends Stage {
size: ProjectSize = ProjectSize.FullHD,
config: Partial<StageConfig> = {},
) {
- //@ts-ignore
super({
width: Sizes[size][0],
height: Sizes[size][1],
listening: false,
+ container: null,
...config,
});
@@ -91,7 +91,11 @@ export class Project extends Stage {
console.warn('Runner without a name: ', scene);
}
const handle = new Scene(this, scene);
- this.sceneLookup[scene.name] = handle;
+ if (this.sceneLookup[scene.name]) {
+ console.warn('Duplicated scene name: ', scene.name);
+ handle.name(scene.name + Math.random());
+ }
+ this.sceneLookup[handle.name()] = handle;
handle.threadsCallback = thread => {
if (this.currentScene === handle) {
this.currentThread = thread;
@@ -101,7 +105,7 @@ export class Project extends Stage {
}
}
- draw(): this {
+ public draw(): this {
this.previousScene?.drawScene();
this.currentScene?.drawScene();
@@ -112,7 +116,6 @@ export class Project extends Stage {
for (const runner of runners) {
this.sceneLookup[runner.name]?.reload(runner);
}
- this.scenesChanged.dispatch(this.scenes);
}
public async next(speed: number = 1): Promise<boolean> {
@@ -120,8 +123,6 @@ export class Project extends Stage {
await this.previousScene.next();
if (!this.currentScene || this.currentScene.isAfterTransitionIn()) {
this.previousScene.remove();
- this.previousScene.lastFrame = this.frame;
- this.scenesChanged.dispatch(this.scenes);
this.previousScene = null;
}
}
@@ -135,12 +136,8 @@ export class Project extends Stage {
this.currentScene = this.getNextScene(this.previousScene);
if (this.currentScene) {
await this.currentScene.reset(this.previousScene);
- this.currentScene.firstFrame = this.frame;
- this.scenesChanged.dispatch(this.scenes);
this.add(this.currentScene);
this.foreground.moveToTop();
- } else {
- this.previousScene.lastFrame = this.frame;
}
}
}
@@ -149,28 +146,46 @@ export class Project extends Stage {
}
public async recalculate() {
+ const initialScene = this.currentScene;
this.previousScene?.remove();
this.previousScene = null;
- this.currentScene?.remove();
- this.currentScene = this.findBestScene(Infinity);
-
- this.frame = this.currentScene.firstFrame ?? 0;
- this.add(this.currentScene);
- this.foreground.moveToTop();
- await this.currentScene.reset();
+ this.currentScene = null;
+
+ this.frame = 0;
+ let offset = 0;
+ for (const scene of this.scenes) {
+ if (scene.cached) {
+ scene.firstFrame += offset;
+ this.frame = scene.lastFrame;
+ } else {
+ this.currentScene = scene;
+ await scene.reset();
+ scene.firstFrame = this.frame;
+ scene.transitionDuration = -1;
+ while (!scene.canTransitionOut()) {
+ if (scene.transitionDuration < 0 && scene.isAfterTransitionIn()) {
+ scene.transitionDuration = this.frame - scene.firstFrame;
+ }
+ this.frame++;
+ await scene.next();
+ }
+ offset += this.frame - scene.lastFrame;
+ scene.lastFrame = this.frame;
+ scene.cached = true;
- let finished = false;
- while (!finished) {
- finished = await this.next(1);
+ await new Promise(resolve => setTimeout(resolve, 0));
+ }
}
+
+ this.currentScene = initialScene;
+ this.scenesChanged.dispatch(this.scenes);
}
public async seek(frame: number, speed: number = 1): Promise<boolean> {
if (
frame <= this.frame ||
!this.currentScene ||
- (this.currentScene.lastFrame !== null &&
- this.currentScene.lastFrame <= frame)
+ (this.currentScene.cached && this.currentScene.lastFrame < frame)
) {
const scene = this.findBestScene(frame);
if (scene !== this.currentScene) {
@@ -179,14 +194,14 @@ export class Project extends Stage {
this.currentScene?.remove();
this.currentScene = scene;
- this.frame = this.currentScene.firstFrame ?? 0;
+ this.frame = this.currentScene.firstFrame;
this.add(this.currentScene);
this.foreground.moveToTop();
await this.currentScene.reset();
} else if (this.frame >= frame) {
this.previousScene?.remove();
this.previousScene = null;
- this.frame = this.currentScene.firstFrame ?? 0;
+ this.frame = this.currentScene.firstFrame;
await this.currentScene.reset();
}
}
@@ -202,7 +217,7 @@ export class Project extends Stage {
private findBestScene(frame: number): Scene {
let lastScene = null;
for (const scene of Object.values(this.sceneLookup)) {
- if (scene.lastFrame === null || scene.lastFrame > frame) {
+ if (!scene.cached || scene.lastFrame > frame) {
return scene;
}
lastScene = scene;
diff --git a/src/Scene.ts b/src/Scene.ts
index 9e666011..00424744 100644
--- a/src/Scene.ts
+++ b/src/Scene.ts
@@ -12,7 +12,8 @@ import {Group} from 'konva/lib/Group';
import {Shape} from 'konva/lib/Shape';
import {SceneTransition} from './transitions';
import {decorate, threadable} from './decorators';
-import {PROJECT} from './symbols';
+import {PROJECT, SCENE} from './symbols';
+import {SimpleEventDispatcher} from 'strongly-typed-events';
export interface SceneRunner {
(layer: Scene, project: Project): ThreadGenerator;
@@ -25,12 +26,39 @@ export enum SceneState {
Finished,
}
+export interface TimeEvent {
+ name: string;
+ initialFrame: number;
+ offset: number;
+}
+
export class Scene extends Layer {
- public firstFrame: number = null;
- public lastFrame: number = null;
public threadsCallback: ThreadsCallback = null;
+ public cached = false;
+ public firstFrame: number = 0;
+ public transitionDuration: number = 0;
+ public duration: number = 0;
+
+ public get lastFrame() {
+ return this.firstFrame + this.duration;
+ }
+ public set lastFrame(value: number) {
+ this.duration = value - this.firstFrame;
+ }
+
+ public get timeEvents(): TimeEvent[] {
+ return Object.values(this.timeEventLookup);
+ }
+
+ public get TimeEventsChanged() {
+ return this.timeEventsChanged.asEvent();
+ }
private readonly debugNode: Debug;
+ private readonly storageKey: string;
+ private readonly timeEventsChanged = new SimpleEventDispatcher<TimeEvent[]>();
+ private timeEventLookup: Record<string, TimeEvent> = {};
+ private storedEventLookup: Record<string, TimeEvent> = {};
private previousScene: Scene = null;
private runner: ThreadGenerator;
private state: SceneState = SceneState.Initial;
@@ -48,12 +76,25 @@ export class Scene extends Layer {
this.add(this.debugNode);
this.debugNode.hide();
decorate(runnerFactory, threadable());
+
+ this.storageKey = `scene-${this.name()}`;
+ const storedEvents = localStorage.getItem(this.storageKey);
+ if (storedEvents) {
+ this.storedEventLookup = JSON.parse(storedEvents);
+ }
}
- public reload(runnerFactory: SceneRunner) {
- this.runnerFactory = runnerFactory;
+ public reload(runnerFactory?: SceneRunner) {
+ if (runnerFactory) {
+ this.runnerFactory = runnerFactory;
+ }
this.debug(null);
- this.lastFrame = null;
+ this.cached = false;
+ this.storedEventLookup = {
+ ...this.storedEventLookup,
+ ...this.timeEventLookup,
+ };
+ this.timeEventLookup = {};
}
public async reset(previousScene: Scene = null) {
@@ -78,6 +119,8 @@ export class Scene extends Layer {
result = this.runner.next(value);
} else if (result.value === PROJECT) {
result = this.runner.next(this.project);
+ } else if (result.value === SCENE) {
+ result = this.runner.next(this);
} else {
console.log('Invalid value: ', result.value);
result = this.runner.next();
@@ -124,7 +167,10 @@ export class Scene extends Layer {
}
public canTransitionOut(): boolean {
- return this.state === SceneState.CanTransitionOut || this.state === SceneState.Finished;
+ return (
+ this.state === SceneState.CanTransitionOut ||
+ this.state === SceneState.Finished
+ );
}
public add(...children: (Shape | Group)[]): this {
@@ -137,4 +183,47 @@ export class Scene extends Layer {
this.debugNode.target(node);
this.debugNode.visible(node !== null);
}
+
+ public getFrameEvent(name: string): number {
+ const initialFrame = this.project.frame - this.firstFrame;
+ if (this.timeEventLookup[name] === undefined) {
+ this.timeEventLookup[name] = {
+ name,
+ initialFrame,
+ offset: this.storedEventLookup[name]?.offset ?? 0,
+ };
+ this.timeEventsChanged.dispatch(this.timeEvents);
+ } else if (this.timeEventLookup[name].initialFrame !== initialFrame) {
+ this.timeEventLookup[name] = {
+ ...this.timeEventLookup[name],
+ initialFrame,
+ };
+ this.timeEventsChanged.dispatch(this.timeEvents);
+ }
+
+ return (
+ this.firstFrame +
+ this.timeEventLookup[name].initialFrame +
+ this.timeEventLookup[name].offset
+ );
+ }
+
+ public setFrameEvent(name: string, frame: number) {
+ if (
+ !this.timeEventLookup[name] ||
+ this.timeEventLookup[name].offset === frame
+ ) {
+ return;
+ }
+ this.cached = false;
+ this.storedEventLookup[name] = this.timeEventLookup[name] = {
+ ...this.timeEventLookup[name],
+ offset: frame,
+ };
+ localStorage.setItem(
+ this.storageKey,
+ JSON.stringify(this.storedEventLookup),
+ );
+ this.timeEventsChanged.dispatch(this.timeEvents);
+ }
}
diff --git a/src/animations/scheduling.ts b/src/animations/scheduling.ts
index da0cac22..3a1c970c 100644
--- a/src/animations/scheduling.ts
+++ b/src/animations/scheduling.ts
@@ -1,16 +1,21 @@
import type {Project} from '../Project';
+import type {Scene} from '../Scene';
import {decorate, threadable} from '../decorators';
-import {PROJECT} from '../symbols';
+import {PROJECT, SCENE} from '../symbols';
import {ThreadGenerator} from '../threading';
decorate(waitUntil, threadable());
export function* waitUntil(
- targetTime = 0,
+ targetTime: number | string = 0,
after?: ThreadGenerator,
): ThreadGenerator {
- const project = (yield PROJECT) as Project;
- const frames = project.secondsToFrames(targetTime);
- while (project.frame < frames) {
+ const scene: Scene = yield SCENE;
+ const frames =
+ typeof targetTime === 'string'
+ ? scene.getFrameEvent(targetTime)
+ : scene.project.secondsToFrames(targetTime);
+
+ while (scene.project.frame < frames) {
yield;
}
diff --git a/src/player/Player.ts b/src/player/Player.ts
index 6c970095..7e09a9d0 100644
--- a/src/player/Player.ts
+++ b/src/player/Player.ts
@@ -148,7 +148,7 @@ export class Player {
const state = JSON.parse(savedState) as PlayerState;
this.state.paused = state.paused;
this.state.startFrame = state.startFrame;
- this.state.endFrame = state.endFrame;
+ this.state.endFrame = state.endFrame ?? Infinity;
this.state.loop = state.loop;
this.state.speed = state.speed;
this.state.muted = state.muted;
@@ -210,6 +210,7 @@ export class Player {
// Recalculate
if (commands.recalculate) {
+ console.time('reload');
await this.project.recalculate();
const duration = this.project.frame;
const finished = await this.project.seek(this.frame);
@@ -217,11 +218,16 @@ export class Player {
this.updateState({
duration,
finished,
+ endFrame:
+ state.endFrame !== Infinity && state.endFrame > duration
+ ? duration
+ : state.endFrame,
});
if (this.frame + 1 !== this.project.frame) {
this.updateFrame(this.project.frame);
}
this.request();
+ console.timeEnd('reload');
return;
}
diff --git a/src/symbols.ts b/src/symbols.ts
index d8121eee..1bf3218c 100644
--- a/src/symbols.ts
+++ b/src/symbols.ts
@@ -1,2 +1,3 @@
export const ANIMATE = Symbol('ANIMATE');
export const PROJECT = Symbol('PROJECT');
+export const SCENE = Symbol('SCENE');