-
Notifications
You must be signed in to change notification settings - Fork 0
/
LayeredRenderer.ts
157 lines (128 loc) · 4.49 KB
/
LayeredRenderer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { RenderObject } from "canvas/render/RenderObject";
import { ZBuffer } from "canvas/render/ZBuffer";
import { CanvasLayer } from "./CanvasLayer";
import type { ViewportHolder } from "./Viewport";
export class LayeredRenderer {
readonly dpr: number;
private buffer: ZBuffer<RenderObject> = new ZBuffer();
private canvasLayers: CanvasLayer[] = [];
private isRendering = false;
private renderingRequested = false;
private forceFullRendering = false;
constructor(
private window: Window,
private targetElement: HTMLElement,
private contextHolder: ViewportHolder,
) {
this.dpr = window.devicePixelRatio || 1;
this.addCanvasLayer();
}
public requestFullRender(): void {
this.forceFullRendering = true;
if (!this.renderingRequested) {
this.requestAnimationFrame();
}
}
public addObjects(objects: RenderObject[], z?: number): void {
this.buffer.addMany(objects, z);
this.updateCanvasLayers();
this.markDirtyObjects(objects);
}
public addObjectsAbove(
objects: RenderObject[],
lastBelow: RenderObject,
): void {
this.addObjects(objects, this.buffer.getIndex(lastBelow) + 1);
}
public addObjectsBelow(
objects: RenderObject[],
firstOnTop: RenderObject,
): void {
this.addObjects(objects, this.buffer.getIndex(firstOnTop));
}
public removeObjects(objects: RenderObject[]): void {
this.buffer.removeMany(objects);
this.updateCanvasLayers();
this.markDirtyObjects(objects);
}
public moveObjectsTo(objects: RenderObject[], z?: number): void {
this.buffer.moveManyTo(objects, z);
this.updateCanvasLayers();
this.markDirtyObjects(objects);
}
/**
* Move objects (while maintaining their relative ordering to each other)
* on top of another existing object.
*/
public moveObjectsAbove(
objects: RenderObject[],
lastBelow: RenderObject,
): void {
this.moveObjectsTo(objects, this.buffer.getIndex(lastBelow) + 1);
}
public moveObjectsBelow(
objects: RenderObject[],
firstOnTop: RenderObject,
): void {
this.moveObjectsTo(objects, this.buffer.getIndex(firstOnTop));
}
public reorderManyAbove(
sortedObjects: RenderObject[],
lastBelow: RenderObject,
): void {
this.buffer.reorderManyTo(
sortedObjects,
this.buffer.getIndex(lastBelow) + 1,
);
this.updateCanvasLayers();
this.markDirtyObjects(sortedObjects);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public markDirtyObjects(objects: RenderObject[]): void {
// Ignore objects for now, dirty objects cause a complete redraw.
// TODO: In future implementations, only specific layers or regions
// could be redrawn.
this.requestFullRender();
}
public get objects(): RenderObject[] {
return this.buffer.sorted();
}
private requestAnimationFrame(): void {
if (!this.renderingRequested) {
this.renderingRequested = true;
this.window.requestAnimationFrame((timestamp: number) => {
this.renderingRequested = false;
this.render(timestamp);
});
}
}
private render(timestamp: number): void {
if (this.isRendering) {
console.error("Rendering invoked during rendering.");
}
/* *** Start of rendering *** */
this.isRendering = true;
for (const layer of this.canvasLayers) {
if (this.forceFullRendering || layer.needsRendering) {
layer.render(timestamp, this.contextHolder.viewport);
}
}
// Reset force full rendering
this.forceFullRendering = false;
this.isRendering = false;
/* *** End of rendering *** */
// Request next animation frame if a layer needs re-rendering
if (this.canvasLayers.some(l => l.needsRendering)) {
this.requestAnimationFrame();
}
}
private addCanvasLayer() {
const canvasElement = document.createElement("canvas");
this.targetElement.appendChild(canvasElement);
const ctx = canvasElement.getContext("2d");
this.canvasLayers.push(new CanvasLayer(ctx, this.dpr));
}
private updateCanvasLayers() {
this.canvasLayers[0].updateObjects(this.objects);
}
}