add a bit of offset
This commit is contained in:
parent
7fe8047f23
commit
8e6abb95db
@ -29,25 +29,14 @@ function snapToGrid(p: Point, gap: number): Point {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const ORIGIN: Point = { x: 0, y: 0 };
|
|
||||||
|
|
||||||
function useWindowSizeCustom() {
|
|
||||||
const sizeRaw = useWindowSize();
|
|
||||||
const size = useMemo(() => {
|
|
||||||
if (sizeRaw.width === null || sizeRaw.height === null) return null;
|
|
||||||
return { width: sizeRaw.width, height: sizeRaw.height };
|
|
||||||
}, [sizeRaw]);
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
function useRange(offset: Point): Range {
|
function useRange(offset: Point): Range {
|
||||||
const windowSize = useWindowSizeCustom();
|
const windowSize = useWindowSize();
|
||||||
const range = useMemo(
|
const range = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
x0: offset.x,
|
x0: offset.x,
|
||||||
y0: offset.y,
|
y0: offset.y,
|
||||||
x1: offset.x + (windowSize?.width || 0),
|
x1: offset.x + (windowSize.width || 0),
|
||||||
y1: offset.y + (windowSize?.height || 0),
|
y1: offset.y + (windowSize.height || 0),
|
||||||
}),
|
}),
|
||||||
[windowSize, offset]
|
[windowSize, offset]
|
||||||
);
|
);
|
||||||
@ -60,18 +49,21 @@ function useMouseInRange(range: Range): Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Grid: FC = () => {
|
const Grid: FC = () => {
|
||||||
const [offset, setOffset] = useState<Point>(ORIGIN);
|
const GAP = 20;
|
||||||
|
|
||||||
|
// const [offset, setOffset] = useState<Point>({ x: GAP / 2, y: GAP / 2 });
|
||||||
|
const [offset, setOffset] = useState<Point>({ x: 10, y: 10 });
|
||||||
const range = useRange(offset);
|
const range = useRange(offset);
|
||||||
const mousePoint = useMouseInRange(range);
|
const mousePoint = useMouseInRange(range);
|
||||||
|
|
||||||
const GAP = 20;
|
|
||||||
|
|
||||||
const gridLines = useMemo(() => {
|
const gridLines = useMemo(() => {
|
||||||
const lines = [];
|
const lines = [];
|
||||||
for (let x = range.x0; x <= range.x1; x += GAP) {
|
const start = snapToGrid({ x: range.x0, y: range.y0 }, GAP);
|
||||||
|
const end = snapToGrid({ x: range.x1, y: range.y1 }, GAP);
|
||||||
|
for (let x = start.x; x <= end.x; x += GAP) {
|
||||||
lines.push({ ...range, x0: x, x1: x });
|
lines.push({ ...range, x0: x, x1: x });
|
||||||
}
|
}
|
||||||
for (let y = range.y0; y <= range.y1; y += GAP) {
|
for (let y = start.y; y <= end.y; y += GAP) {
|
||||||
lines.push({ ...range, y0: y, y1: y });
|
lines.push({ ...range, y0: y, y1: y });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,9 +132,7 @@ const Grid: FC = () => {
|
|||||||
|
|
||||||
const onGridClick = useCallback(
|
const onGridClick = useCallback(
|
||||||
(event: MouseEvent<SVGSVGElement>) => {
|
(event: MouseEvent<SVGSVGElement>) => {
|
||||||
console.log('grid click', event);
|
const point = snapToGrid(mousePoint, GAP);
|
||||||
const pointRaw = { x: event.clientX, y: event.clientY };
|
|
||||||
const point = snapToGrid(pointRaw, GAP);
|
|
||||||
|
|
||||||
if (lodash.isEqual(userStartPoint, point)) return;
|
if (lodash.isEqual(userStartPoint, point)) return;
|
||||||
if (lodash.isNull(userStartPoint)) {
|
if (lodash.isNull(userStartPoint)) {
|
||||||
@ -159,14 +149,16 @@ const Grid: FC = () => {
|
|||||||
setUserStartPoint(point);
|
setUserStartPoint(point);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[userStartPoint, setUserStartPoint, setUserLines, GAP]
|
[userStartPoint, setUserStartPoint, setUserLines, GAP, mousePoint]
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<svg
|
<svg
|
||||||
className="grid"
|
className="grid"
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
viewBox={`${range.x0} ${range.y0} ${range.x1} ${range.y1}`}
|
viewBox={`${range.x0} ${range.y0} ${range.x1 - range.x0} ${
|
||||||
|
range.y1 - range.y0
|
||||||
|
}`}
|
||||||
onClick={onGridClick}
|
onClick={onGridClick}
|
||||||
>
|
>
|
||||||
{gridLineElements}
|
{gridLineElements}
|
||||||
|
Loading…
Reference in New Issue
Block a user