From a7c590d4593842ba2e43cfd01f7ec764dc73efd4 Mon Sep 17 00:00:00 2001 From: Arnost Pleskot Date: Fri, 28 Jul 2023 00:41:44 +0200 Subject: [PATCH] feat: render bold lines in grid (#6779) Co-authored-by: dwelle --- src/renderer/renderScene.ts | 48 ++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/renderer/renderScene.ts b/src/renderer/renderScene.ts index 7d1d5102..f8fa79ed 100644 --- a/src/renderer/renderScene.ts +++ b/src/renderer/renderScene.ts @@ -162,23 +162,52 @@ const fillCircle = ( const strokeGrid = ( context: CanvasRenderingContext2D, gridSize: number, - offsetX: number, - offsetY: number, + scrollX: number, + scrollY: number, + zoom: Zoom, width: number, height: number, ) => { + const BOLD_LINE_FREQUENCY = 5; + + enum GridLineColor { + Bold = "#cccccc", + Regular = "#e5e5e5", + } + + const offsetX = + -Math.round(zoom.value / gridSize) * gridSize + (scrollX % gridSize); + const offsetY = + -Math.round(zoom.value / gridSize) * gridSize + (scrollY % gridSize); + + const lineWidth = Math.min(1 / zoom.value, 1); + + const spaceWidth = 1 / zoom.value; + const lineDash = [lineWidth * 3, spaceWidth + (lineWidth + spaceWidth)]; + context.save(); - context.strokeStyle = "rgba(0,0,0,0.1)"; - context.beginPath(); + context.lineWidth = lineWidth; + for (let x = offsetX; x < offsetX + width + gridSize * 2; x += gridSize) { + const isBold = + Math.round(x - scrollX) % (BOLD_LINE_FREQUENCY * gridSize) === 0; + context.beginPath(); + context.setLineDash(isBold ? [] : lineDash); + context.strokeStyle = isBold ? GridLineColor.Bold : GridLineColor.Regular; context.moveTo(x, offsetY - gridSize); context.lineTo(x, offsetY + height + gridSize * 2); + context.stroke(); } for (let y = offsetY; y < offsetY + height + gridSize * 2; y += gridSize) { + const isBold = + Math.round(y - scrollY) % (BOLD_LINE_FREQUENCY * gridSize) === 0; + context.beginPath(); + context.setLineDash(isBold ? [] : lineDash); + context.strokeStyle = isBold ? GridLineColor.Bold : GridLineColor.Regular; context.moveTo(offsetX - gridSize, y); context.lineTo(offsetX + width + gridSize * 2, y); + context.stroke(); } - context.stroke(); context.restore(); }; @@ -425,12 +454,9 @@ export const _renderScene = ({ strokeGrid( context, appState.gridSize, - -Math.ceil(renderConfig.zoom.value / appState.gridSize) * - appState.gridSize + - (renderConfig.scrollX % appState.gridSize), - -Math.ceil(renderConfig.zoom.value / appState.gridSize) * - appState.gridSize + - (renderConfig.scrollY % appState.gridSize), + renderConfig.scrollX, + renderConfig.scrollY, + renderConfig.zoom, normalizedCanvasWidth / renderConfig.zoom.value, normalizedCanvasHeight / renderConfig.zoom.value, );