mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
grid support (1st iteration) (#1788)
Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
parent
b6bf011d0d
commit
baa8fb6c14
14 changed files with 343 additions and 80 deletions
|
@ -74,6 +74,29 @@ const strokeCircle = (
|
|||
context.stroke();
|
||||
};
|
||||
|
||||
const renderGrid = (
|
||||
context: CanvasRenderingContext2D,
|
||||
gridSize: number,
|
||||
offsetX: number,
|
||||
offsetY: number,
|
||||
width: number,
|
||||
height: number,
|
||||
) => {
|
||||
const origStrokeStyle = context.strokeStyle;
|
||||
context.strokeStyle = "rgba(0,0,0,0.1)";
|
||||
context.beginPath();
|
||||
for (let x = offsetX; x < offsetX + width + gridSize * 2; x += gridSize) {
|
||||
context.moveTo(x, offsetY - gridSize);
|
||||
context.lineTo(x, offsetY + height + gridSize * 2);
|
||||
}
|
||||
for (let y = offsetY; y < offsetY + height + gridSize * 2; y += gridSize) {
|
||||
context.moveTo(offsetX - gridSize, y);
|
||||
context.lineTo(offsetX + width + gridSize * 2, y);
|
||||
}
|
||||
context.stroke();
|
||||
context.strokeStyle = origStrokeStyle;
|
||||
};
|
||||
|
||||
const renderLinearPointHandles = (
|
||||
context: CanvasRenderingContext2D,
|
||||
appState: AppState,
|
||||
|
@ -167,6 +190,22 @@ export const renderScene = (
|
|||
context.translate(zoomTranslationX, zoomTranslationY);
|
||||
context.scale(sceneState.zoom, sceneState.zoom);
|
||||
|
||||
// Grid
|
||||
if (appState.gridSize) {
|
||||
renderGrid(
|
||||
context,
|
||||
appState.gridSize,
|
||||
-Math.ceil(zoomTranslationX / sceneState.zoom / appState.gridSize) *
|
||||
appState.gridSize +
|
||||
(sceneState.scrollX % appState.gridSize),
|
||||
-Math.ceil(zoomTranslationY / sceneState.zoom / appState.gridSize) *
|
||||
appState.gridSize +
|
||||
(sceneState.scrollY % appState.gridSize),
|
||||
normalizedCanvasWidth / sceneState.zoom,
|
||||
normalizedCanvasHeight / sceneState.zoom,
|
||||
);
|
||||
}
|
||||
|
||||
// Paint visible elements
|
||||
const visibleElements = elements.filter((element) =>
|
||||
isVisibleElement(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue