-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34fbf46
commit 432ed1d
Showing
2 changed files
with
50 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,53 @@ | ||
import Konva from 'konva'; | ||
import { useRef, useEffect } from 'react'; | ||
import { Layer } from 'react-konva'; | ||
import { Size } from '@/core/model'; | ||
import React, { useMemo } from 'react'; | ||
import { Layer, Line } from 'react-konva'; | ||
|
||
const gridSize = 40; | ||
|
||
export const drawGrid = ( | ||
layer: Konva.Layer, | ||
scale: number, | ||
stageRef: React.RefObject<Konva.Stage> | ||
) => { | ||
const gridSpacing = gridSize * scale; | ||
const width = stageRef.current?.width() ?? 0; | ||
const height = stageRef.current?.height() ?? 0; | ||
// Memoized component for a grid line | ||
const GridLine = React.memo( | ||
({ points }: { points: number[] }) => ( | ||
<Line points={points} stroke="rgba(0, 0, 0, 0.1)" strokeWidth={1} /> | ||
), | ||
(prevProps, nextProps) => prevProps.points.join() === nextProps.points.join() | ||
); | ||
|
||
// Clear any previous grid lines | ||
layer.find('.grid-line').forEach(line => line.destroy()); | ||
interface Props { | ||
scale: number; | ||
canvasSize: Size; | ||
} | ||
|
||
// Vertical lines | ||
for (let x = 0; x < width; x += gridSpacing) { | ||
layer.add( | ||
new Konva.Line({ | ||
name: 'grid-line', | ||
points: [x, 0, x, height], | ||
stroke: 'rgba(0, 0, 0, 0.1)', | ||
strokeWidth: 1, | ||
// dash: [4, 6], // Optional dash pattern for grid lines | ||
}) | ||
export const CanvasGridLayer: React.FC<Props> = props => { | ||
const { scale, canvasSize } = props; | ||
const gridSpacing = gridSize * scale; | ||
const width = canvasSize.width; | ||
const height = canvasSize.height; | ||
|
||
// Memoize the grid lines computation to avoid unnecessary recalculations | ||
const { verticalLines, horizontalLines } = useMemo(() => { | ||
const verticalLines = Array.from( | ||
{ length: Math.ceil(width / gridSpacing) }, | ||
(_, i) => [i * gridSpacing, 0, i * gridSpacing, height] | ||
); | ||
} | ||
|
||
// Horizontal lines | ||
for (let y = 0; y < height; y += gridSpacing) { | ||
layer.add( | ||
new Konva.Line({ | ||
name: 'grid-line', | ||
points: [0, y, width, y], | ||
stroke: 'rgba(0, 0, 0, 0.1)', | ||
strokeWidth: 1, | ||
// sdash: [4, 6], // Optional dash pattern for grid lines | ||
}) | ||
const horizontalLines = Array.from( | ||
{ length: Math.ceil(height / gridSpacing) }, | ||
(_, i) => [0, i * gridSpacing, width, i * gridSpacing] | ||
); | ||
} | ||
|
||
layer.batchDraw(); | ||
return { verticalLines, horizontalLines }; | ||
}, [width, height, gridSpacing]); | ||
|
||
return ( | ||
<Layer> | ||
{/* Render vertical lines */} | ||
{verticalLines.map((points, index) => ( | ||
<GridLine key={`v-line-${index}`} points={points} /> | ||
))} | ||
{/* Render horizontal lines */} | ||
{horizontalLines.map((points, index) => ( | ||
<GridLine key={`h-line-${index}`} points={points} /> | ||
))} | ||
</Layer> | ||
); | ||
}; | ||
|
||
const CanvasGrid = ({ | ||
scale, | ||
stageRef, | ||
}: { | ||
scale: number; | ||
stageRef: React.RefObject<Konva.Stage>; | ||
}) => { | ||
const layerRef = useRef<Konva.Layer>(null); | ||
|
||
useEffect(() => { | ||
if (layerRef.current) { | ||
drawGrid(layerRef.current, scale, stageRef); | ||
} | ||
}, [scale, stageRef]); | ||
|
||
return <Layer ref={layerRef} />; | ||
}; | ||
|
||
export default CanvasGrid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters