-
Notifications
You must be signed in to change notification settings - Fork 721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(demo): fix delaunay-voronoi demo #1758
base: master
Are you sure you want to change the base?
Changes from 2 commits
1462a22
0d67dcd
fb47cc7
07a06d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,9 @@ | |
const innerWidth = width - margin.left - margin.right; | ||
const innerHeight = height - margin.top - margin.bottom; | ||
|
||
if (innerWidth < 0 || innerHeight < 0) return null; | ||
|
||
const voronoiDiagram = useMemo( | ||
Check failure on line 42 in packages/visx-demo/src/sandboxes/visx-delaunay-voronoi/Example.tsx GitHub Actions / build
|
||
() => | ||
voronoi<Datum>({ | ||
data, | ||
|
@@ -49,9 +51,9 @@ | |
[innerWidth, innerHeight], | ||
); | ||
|
||
const svgRef = useRef<SVGSVGElement>(null); | ||
Check failure on line 54 in packages/visx-demo/src/sandboxes/visx-delaunay-voronoi/Example.tsx GitHub Actions / build
|
||
const [hoveredId, setHoveredId] = useState<string | null>(null); | ||
Check failure on line 55 in packages/visx-demo/src/sandboxes/visx-delaunay-voronoi/Example.tsx GitHub Actions / build
|
||
const [neighborIds, setNeighborIds] = useState<Set<string>>(new Set()); | ||
Check failure on line 56 in packages/visx-demo/src/sandboxes/visx-delaunay-voronoi/Example.tsx GitHub Actions / build
|
||
|
||
return width < 10 ? null : ( | ||
<svg width={width} height={height} ref={svgRef}> | ||
|
@@ -71,7 +73,7 @@ | |
|
||
const closest = voronoiDiagram.delaunay.find(point.x, point.y); | ||
// find neighboring polygons to hightlight | ||
if (closest && data[closest].id !== hoveredId) { | ||
if (data[closest].id !== hoveredId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the /**
* Returns the index of the input point that is closest to the specified point ⟨x, y⟩.
* The search is started at the specified point i. If i is not specified, it defaults to zero.
*/
find(x: number, y: number, i?: number): number; Furthermore, the actual find(x, y, i = 0) {
if ((x = +x, x !== x) || (y = +y, y !== y)) return -1;
const i0 = i;
let c;
while ((c = this._step(i, x, y)) >= 0 && c !== i && c !== i0) i = c;
return c;
} Additionally, this const closest = delaunayDiagram.find(point.x - margin.left, point.y - margin.top);
setHoveredId(data[closest].id); |
||
const neighbors = Array.from(voronoiDiagram.neighbors(closest)); | ||
setNeighborIds(new Set(neighbors.map((d) => data[d].id))); | ||
setHoveredId(data[closest].id); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an early return statement will break the rules of hooks, where they would then be called conditionally/a different # of times per render.
maybe a different approach (since we already return
null
ifwidth < 10
), is to add something likeconst innerWidth = Math.max(0, width - margin.left - margin.right)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely forgot about the rules of hooks. 😅
I've pushed a commit reflecting your comment here. Thank you!