40 lines
978 B
TypeScript
40 lines
978 B
TypeScript
import type { ImageDocument } from "@core/document";
|
|
import type { EditorState } from "@editor/state";
|
|
|
|
export type RenderFrame = {
|
|
document: ImageDocument;
|
|
editor: EditorState;
|
|
};
|
|
|
|
export type ImageRenderer = {
|
|
render(frame: RenderFrame): void;
|
|
dispose(): void;
|
|
};
|
|
|
|
export type RendererBackend = "webgl";
|
|
|
|
export function createRenderer(canvas: HTMLCanvasElement, backend: RendererBackend = "webgl"): ImageRenderer {
|
|
if (backend !== "webgl") {
|
|
throw new Error(`Unsupported renderer backend: ${backend}`);
|
|
}
|
|
|
|
const context = canvas.getContext("webgl2");
|
|
if (!context) {
|
|
throw new Error("WebGL2 is not available");
|
|
}
|
|
|
|
return {
|
|
render(frame) {
|
|
const { w, h } = frame.editor.viewport.size;
|
|
|
|
if (canvas.width !== w) canvas.width = w;
|
|
if (canvas.height !== h) canvas.height = h;
|
|
|
|
context.viewport(0, 0, w, h);
|
|
context.clearColor(0, 0, 0, 0);
|
|
context.clear(context.COLOR_BUFFER_BIT);
|
|
},
|
|
dispose() {},
|
|
};
|
|
}
|