@@ -4,6 +4,9 @@ import useMouseMatrixTransform from "use-mouse-matrix-transform"
44import { compose , scale , translate } from "transformation-matrix"
55import useResizeObserver from "@react-hook/resize-observer"
66import { 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
811interface 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 ( {
0 commit comments