/
Stage.ts
112 lines (95 loc) · 3.37 KB
/
Stage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {Scene} from '../scenes';
import {unwrap} from '../signals';
import type {Color} from '../types';
import {CanvasColorSpace, Vector2} from '../types';
import {getContext} from '../utils';
export interface StageSettings {
size: Vector2;
resolutionScale: number;
colorSpace: CanvasColorSpace;
background: Color | string | null;
}
/**
* Manages canvases on which an animation can be displayed.
*/
export class Stage {
// TODO Consider adding pooling for canvases.
private background: string | null = null;
private resolutionScale = 1;
private colorSpace: CanvasColorSpace = 'srgb';
private size = Vector2.zero;
public readonly finalBuffer: HTMLCanvasElement;
private readonly currentBuffer: HTMLCanvasElement;
private readonly previousBuffer: HTMLCanvasElement;
private context: CanvasRenderingContext2D;
private currentContext: CanvasRenderingContext2D;
private previousContext: CanvasRenderingContext2D;
private get canvasSize() {
return this.size.scale(this.resolutionScale);
}
public constructor() {
this.finalBuffer = document.createElement('canvas');
this.currentBuffer = document.createElement('canvas');
this.previousBuffer = document.createElement('canvas');
const colorSpace = this.colorSpace;
this.context = getContext({colorSpace}, this.finalBuffer);
this.currentContext = getContext({colorSpace}, this.currentBuffer);
this.previousContext = getContext({colorSpace}, this.previousBuffer);
}
public configure({
colorSpace = this.colorSpace,
size = this.size,
resolutionScale = this.resolutionScale,
background = this.background,
}: Partial<StageSettings>) {
if (colorSpace !== this.colorSpace) {
this.colorSpace = colorSpace;
this.context = getContext({colorSpace}, this.finalBuffer);
this.currentContext = getContext({colorSpace}, this.currentBuffer);
this.previousContext = getContext({colorSpace}, this.previousBuffer);
}
if (
!size.exactlyEquals(this.size) ||
resolutionScale !== this.resolutionScale
) {
this.resolutionScale = resolutionScale;
this.size = size;
this.resizeCanvas(this.context);
this.resizeCanvas(this.currentContext);
this.resizeCanvas(this.previousContext);
}
this.background =
typeof background === 'string'
? background
: background?.serialize() ?? null;
}
public async render(currentScene: Scene, previousScene: Scene | null) {
const previousOnTop = previousScene
? unwrap(currentScene.previousOnTop)
: false;
if (previousScene) {
await previousScene.render(this.previousContext);
}
await currentScene.render(this.currentContext);
const size = this.canvasSize;
this.context.clearRect(0, 0, size.width, size.height);
if (this.background) {
this.context.save();
this.context.fillStyle = this.background;
this.context.fillRect(0, 0, size.width, size.height);
this.context.restore();
}
if (previousScene && !previousOnTop) {
this.context.drawImage(this.previousBuffer, 0, 0);
}
this.context.drawImage(this.currentBuffer, 0, 0);
if (previousOnTop) {
this.context.drawImage(this.previousBuffer, 0, 0);
}
}
public resizeCanvas(context: CanvasRenderingContext2D) {
const size = this.canvasSize;
context.canvas.width = size.width;
context.canvas.height = size.height;
}
}