refactor(renderer): extract artboard checkerboard drawing

This commit is contained in:
syntaxbullet
2026-07-03 09:56:03 +02:00
parent 3d32e8fbef
commit fb0c424745
7 changed files with 75 additions and 26 deletions

26
renderer/checkerboard.ts Normal file
View File

@@ -0,0 +1,26 @@
import { clearScreenRect } from "./clear-rect";
import type { ScreenRect, WebGlRendererContext } from "./types";
const darkChecker = [0.82, 0.82, 0.86, 1] as const;
const lightChecker = [0.94, 0.94, 0.97, 1] as const;
export function renderCheckerboard(context: WebGlRendererContext, rect: ScreenRect, squareSize: number) {
const clampedSquareSize = Math.max(1, squareSize);
const canvasWidth = context.canvas.width;
const canvasHeight = context.canvas.height;
for (let py = rect.y; py < rect.y + rect.h; py += clampedSquareSize) {
for (let px = rect.x; px < rect.x + rect.w; px += clampedSquareSize) {
const x = Math.max(0, px);
const y = Math.max(0, py);
const w = Math.min(px + clampedSquareSize, rect.x + rect.w, canvasWidth) - x;
const h = Math.min(py + clampedSquareSize, rect.y + rect.h, canvasHeight) - y;
if (w <= 0 || h <= 0) continue;
const checker =
(Math.floor((px - rect.x) / clampedSquareSize) + Math.floor((py - rect.y) / clampedSquareSize)) % 2 === 0;
clearScreenRect(context, { x, y, w, h }, checker ? darkChecker : lightChecker);
}
}
}