diff --git a/renderer/renderer.ts b/renderer/renderer.ts index 4740686..cde4798 100644 --- a/renderer/renderer.ts +++ b/renderer/renderer.ts @@ -23,16 +23,50 @@ export function createRenderer(canvas: HTMLCanvasElement, backend: RendererBacke throw new Error("WebGL2 is not available"); } + const clearRect = (x: number, y: number, w: number, h: number, color: [number, number, number, number]) => { + context.scissor(x, canvas.height - y - h, w, h); + context.clearColor(...color); + context.clear(context.COLOR_BUFFER_BIT); + }; + return { render(frame) { const { w, h } = frame.editor.viewport.size; + const { center, zoom } = frame.editor.viewport; 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.disable(context.SCISSOR_TEST); + context.clearColor(0.18, 0.18, 0.2, 1); context.clear(context.COLOR_BUFFER_BIT); + context.enable(context.SCISSOR_TEST); + + for (const artboard of frame.document.artboards) { + const x = Math.round(w / 2 + (artboard.bounds.x - center.x) * zoom); + const y = Math.round(h / 2 + (artboard.bounds.y - center.y) * zoom); + const width = Math.max(0, Math.round(artboard.bounds.w * zoom)); + const height = Math.max(0, Math.round(artboard.bounds.h * zoom)); + + if (artboard.backgroundColor === "transparent") { + const squareSize = Math.max(4, Math.round(12 * zoom)); + for (let py = y; py < y + height; py += squareSize) { + for (let px = x; px < x + width; px += squareSize) { + const sx = Math.max(0, px); + const sy = Math.max(0, py); + const sw = Math.min(px + squareSize, x + width, w) - sx; + const sh = Math.min(py + squareSize, y + height, h) - sy; + if (sw <= 0 || sh <= 0) continue; + + const checker = (Math.floor((px - x) / squareSize) + Math.floor((py - y) / squareSize)) % 2 === 0; + clearRect(sx, sy, sw, sh, checker ? [0.82, 0.82, 0.86, 1] : [0.94, 0.94, 0.97, 1]); + } + } + } + } + + context.disable(context.SCISSOR_TEST); }, dispose() {}, };