2025-09-10 18:09:33 +02:00
|
|
|
import { BrushRenderer } from "rkgk/brush-renderer.js";
|
|
|
|
|
2024-09-08 12:09:14 +02:00
|
|
|
export class BrushPreview extends HTMLElement {
|
2025-09-10 18:09:33 +02:00
|
|
|
connectedCallback() {
|
|
|
|
this.canvas = this.appendChild(document.createElement("canvas"));
|
|
|
|
this.gl = this.canvas.getContext("webgl2");
|
2025-06-19 13:48:07 +02:00
|
|
|
|
2025-09-10 18:09:33 +02:00
|
|
|
this.brushRenderer = new BrushRenderer(this.gl, this.canvasSource());
|
|
|
|
|
|
|
|
let resizeObserver = new ResizeObserver(() => this.#updateCanvasSize());
|
|
|
|
resizeObserver.observe(this);
|
|
|
|
this.#updateCanvasSize();
|
2024-09-08 12:09:14 +02:00
|
|
|
}
|
|
|
|
|
2025-09-10 18:09:33 +02:00
|
|
|
#updateCanvasSize() {
|
|
|
|
this.canvas.width = this.clientWidth;
|
|
|
|
this.canvas.height = this.clientHeight;
|
|
|
|
this.dispatchEvent(new Event(".pixmapLost"));
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasSource() {
|
|
|
|
let canvas = this.canvas;
|
|
|
|
return {
|
|
|
|
useCanvas(gl, id) {
|
|
|
|
let viewport = { x: 0, y: 0, width: canvas.width, height: canvas.height };
|
|
|
|
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
|
|
|
|
return viewport;
|
|
|
|
},
|
|
|
|
|
|
|
|
resetCanvas(gl) {},
|
|
|
|
};
|
2024-09-08 12:09:14 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 13:53:29 +02:00
|
|
|
async #renderBrushInner(haku) {
|
2025-09-10 18:09:33 +02:00
|
|
|
let gl = this.gl;
|
|
|
|
|
|
|
|
gl.clearColor(0, 0, 0, 0);
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
let canvas = this.canvas;
|
|
|
|
let result = await haku.evalBrush({
|
|
|
|
runDotter: async () => {
|
|
|
|
return {
|
|
|
|
fromX: canvas.width / 2,
|
|
|
|
fromY: canvas.height / 2,
|
|
|
|
toX: canvas.width / 2,
|
|
|
|
toY: canvas.height / 2,
|
|
|
|
num: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
runScribble: async (renderToCanvas) => {
|
|
|
|
return renderToCanvas(this.brushRenderer, 0, 0, 0);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.status != "ok") {
|
|
|
|
return { status: "error", phase: "eval", result };
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:09:14 +02:00
|
|
|
return { status: "ok" };
|
|
|
|
}
|
|
|
|
|
2024-09-08 13:53:29 +02:00
|
|
|
async renderBrush(haku) {
|
2024-09-08 12:12:33 +02:00
|
|
|
this.unsetErrorFlag();
|
2024-09-08 13:53:29 +02:00
|
|
|
let result = await this.#renderBrushInner(haku);
|
2024-09-08 12:09:14 +02:00
|
|
|
if (result.status == "error") {
|
2025-06-19 13:48:07 +02:00
|
|
|
console.error(result);
|
2024-09-08 12:12:33 +02:00
|
|
|
this.setErrorFlag();
|
2024-09-08 12:09:14 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2024-09-08 12:12:33 +02:00
|
|
|
|
|
|
|
unsetErrorFlag() {
|
|
|
|
this.classList.remove("error");
|
|
|
|
}
|
|
|
|
|
|
|
|
setErrorFlag() {
|
|
|
|
this.classList.add("error");
|
|
|
|
}
|
2024-09-08 12:09:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("rkgk-brush-preview", BrushPreview);
|