Skip to content

Commit 6de8717

Browse files
authored
remove useMemos from interactivegraphicscanvas to avoid call stack overflow (#48)
1 parent 8802139 commit 6de8717

File tree

4 files changed

+80
-56
lines changed

4 files changed

+80
-56
lines changed

site/components/InteractiveGraphicsCanvas.tsx

Lines changed: 10 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import useMouseMatrixTransform from "use-mouse-matrix-transform"
44
import { compose, scale, translate } from "transformation-matrix"
55
import useResizeObserver from "@react-hook/resize-observer"
66
import { DimensionOverlay } from "./DimensionOverlay"
7+
import { getMaxStep } from "site/utils/getMaxStep"
8+
import { getGraphicsFilteredByStep } from "site/utils/getGraphicsFilteredByStep"
9+
import { getGraphicsBoundsWithPadding } from "site/utils/getGraphicsBoundsWithPadding"
710

811
interface InteractiveGraphicsCanvasProps {
912
graphics: GraphicsObject
@@ -28,66 +31,17 @@ export function InteractiveGraphicsCanvas({
2831
const [showLastStep, setShowLastStep] = useState(true)
2932

3033
// Calculate the maximum step value from all graphics objects
31-
const maxStep = useMemo(() => {
32-
const maxPointStep = Math.max(
33-
0,
34-
...(graphics.points?.map((p) =>
35-
Number.isNaN(p.step) ? 0 : p.step || 0,
36-
) ?? []),
37-
)
38-
const maxLineStep = Math.max(
39-
0,
40-
...(graphics.lines?.map((l) =>
41-
Number.isNaN(l.step) ? 0 : l.step || 0,
42-
) ?? []),
43-
)
44-
const maxRectStep = Math.max(
45-
0,
46-
...(graphics.rects?.map((r) =>
47-
Number.isNaN(r.step) ? 0 : r.step || 0,
48-
) ?? []),
49-
)
50-
const maxCircleStep = Math.max(
51-
0,
52-
...(graphics.circles?.map((c) =>
53-
Number.isNaN(c.step) ? 0 : c.step || 0,
54-
) ?? []),
55-
)
56-
return Math.max(maxPointStep, maxLineStep, maxRectStep, maxCircleStep)
57-
}, [graphics])
34+
const maxStep = getMaxStep(graphics)
5835

5936
// Filter graphics objects based on step
60-
const filteredGraphics = useMemo(() => {
61-
const selectedStep = showLastStep ? maxStep : activeStep
62-
63-
if (selectedStep === null) {
64-
return graphics
65-
}
66-
67-
const filterByStep = (objStep?: number) =>
68-
objStep === undefined || objStep === selectedStep
69-
70-
return {
71-
...graphics,
72-
points: graphics.points?.filter((p) => filterByStep(p.step)),
73-
lines: graphics.lines?.filter((l) => filterByStep(l.step)),
74-
rects: graphics.rects?.filter((r) => filterByStep(r.step)),
75-
circles: graphics.circles?.filter((c) => filterByStep(c.step)),
76-
}
77-
}, [graphics, activeStep, showLastStep, maxStep])
37+
const filteredGraphics = getGraphicsFilteredByStep(graphics, {
38+
activeStep,
39+
showLastStep,
40+
maxStep,
41+
})
7842

7943
// Get bounds of the graphics with padding
80-
const graphicsBoundsWithPadding = useMemo(() => {
81-
const bounds = getBounds(graphics)
82-
const width = bounds.maxX - bounds.minX
83-
const height = bounds.maxY - bounds.minY
84-
return {
85-
minX: bounds.minX - width / 10,
86-
minY: bounds.minY - height / 10,
87-
maxX: bounds.maxX + width / 10,
88-
maxY: bounds.maxY + height / 10,
89-
}
90-
}, [graphics])
44+
const graphicsBoundsWithPadding = getGraphicsBoundsWithPadding(graphics)
9145

9246
// Use mouse transform hook for panning/zooming
9347
const { transform, ref: mouseTransformRef } = useMouseMatrixTransform({
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getBounds } from "lib/drawGraphicsToCanvas"
2+
import { GraphicsObject } from "lib/types"
3+
4+
export function getGraphicsBoundsWithPadding(graphics: GraphicsObject) {
5+
const bounds = getBounds(graphics)
6+
const width = bounds.maxX - bounds.minX
7+
const height = bounds.maxY - bounds.minY
8+
return {
9+
minX: bounds.minX - width / 10,
10+
minY: bounds.minY - height / 10,
11+
maxX: bounds.maxX + width / 10,
12+
maxY: bounds.maxY + height / 10,
13+
}
14+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { GraphicsObject } from "lib/types"
2+
3+
export function getGraphicsFilteredByStep(
4+
graphics: GraphicsObject,
5+
{
6+
showLastStep,
7+
activeStep,
8+
maxStep,
9+
}: {
10+
showLastStep?: boolean
11+
activeStep?: number | null
12+
maxStep?: number | null
13+
},
14+
) {
15+
const selectedStep = showLastStep ? maxStep : activeStep
16+
17+
if (selectedStep === null) {
18+
return graphics
19+
}
20+
21+
const filteredGraphics = {
22+
...graphics,
23+
points: graphics.points?.filter((p) => p.step === selectedStep),
24+
lines: graphics.lines?.filter((l) => l.step === selectedStep),
25+
rects: graphics.rects?.filter((r) => r.step === selectedStep),
26+
circles: graphics.circles?.filter((c) => c.step === selectedStep),
27+
}
28+
29+
return filteredGraphics
30+
}

site/utils/getMaxStep.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { GraphicsObject } from "lib/types"
2+
3+
export function getMaxStep(graphics: GraphicsObject) {
4+
const maxPointStep = Math.max(
5+
0,
6+
...(graphics.points?.map((p) => (Number.isNaN(p.step) ? 0 : p.step || 0)) ??
7+
[]),
8+
)
9+
const maxLineStep = Math.max(
10+
0,
11+
...(graphics.lines?.map((l) => (Number.isNaN(l.step) ? 0 : l.step || 0)) ??
12+
[]),
13+
)
14+
const maxRectStep = Math.max(
15+
0,
16+
...(graphics.rects?.map((r) => (Number.isNaN(r.step) ? 0 : r.step || 0)) ??
17+
[]),
18+
)
19+
const maxCircleStep = Math.max(
20+
0,
21+
...(graphics.circles?.map((c) =>
22+
Number.isNaN(c.step) ? 0 : c.step || 0,
23+
) ?? []),
24+
)
25+
return Math.max(maxPointStep, maxLineStep, maxRectStep, maxCircleStep)
26+
}

0 commit comments

Comments
 (0)