Skip to content

Commit

Permalink
feat: GridRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyLnd committed Oct 7, 2019
1 parent 2a20a8b commit 7e12648
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/container/GraphView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useStoreState, useStoreActions } from 'easy-peasy';

import NodeRenderer from '../NodeRenderer';
import EdgeRenderer from '../EdgeRenderer';
import GridRenderer from '../GridRenderer';
import UserSelection from '../../components/UserSelection';
import NodesSelection from '../../components/NodesSelection';
import useKeyPress from '../../hooks/useKeyPress';
Expand Down Expand Up @@ -67,6 +68,7 @@ const GraphView = memo(({

return (
<div className="react-flow__renderer" ref={rendererNode}>
<GridRenderer />
<NodeRenderer
nodeTypes={nodeTypes}
onElementClick={onElementClick}
Expand Down
33 changes: 33 additions & 0 deletions src/container/GridRenderer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, {memo} from 'react';
import {useStoreState} from 'easy-peasy';

const GridRenderer = memo(({gap = 32}) => {
const {
width,
height,
transform: [x, y, scale],
} = useStoreState(s => s);

const scaledGap = gap * scale;

const xStart = x % scaledGap;
const yStart = y % scaledGap;

const lineCountX = Math.ceil(width / scaledGap) + 1;
const lineCountY = Math.ceil(height / scaledGap) + 1;

const xValues = Array.from({length: lineCountX}, (_, index) => `M${index * scaledGap + xStart} 0 V${height}`);
const yValues = Array.from({length: lineCountY}, (_, index) => `M0 ${index * scaledGap + yStart} H${width}`);

const path = [...xValues, ...yValues].join(' ');

return (
<svg width={width} height={height} style={{position: 'absolute', top: 0, left: 0}}>
<path fill="none" stroke="black" strokeWidth={.1} d={path} />
</svg>
);
});

GridRenderer.displayName = 'GridRenderer';

export default GridRenderer;

0 comments on commit 7e12648

Please sign in to comment.