22
33import { darken , lighten , readableColor } from 'polished'
44import type React from 'react'
5- import { useEffect , useMemo , useRef , useState } from 'react'
5+ import { useEffect , useLayoutEffect , useMemo , useRef , useState } from 'react'
66import type { AnalyzeData } from '@/lib/analyze-data'
77import {
88 computeTreemapLayoutFromAnalyze ,
@@ -718,13 +718,16 @@ export function TreemapVisualizer({
718718 const [ hoveredNode , setHoveredNode ] = useState < LayoutNode | null > ( null )
719719 const [ shouldDimOthers , setShouldDimOthers ] = useState ( false )
720720 const hoverTimeoutRef = useRef < NodeJS . Timeout | null > ( null )
721- const [ cssDimensions , setCssDimensions ] = useState ( {
722- width : 1200 ,
723- height : 800 ,
724- } )
725- const [ canvasDimensions , setCanvasDimensions ] = useState ( {
726- width : 1200 ,
727- height : 800 ,
721+ const [ dimensions , setDimensions ] = useState < {
722+ cssWidth : number
723+ cssHeight : number
724+ canvasWidth : number
725+ canvasHeight : number
726+ } > ( {
727+ cssWidth : 1200 ,
728+ cssHeight : 800 ,
729+ canvasWidth : 1200 ,
730+ canvasHeight : 800 ,
728731 } )
729732 const [ , _setTheme ] = useState < 'light' | 'dark' > ( 'light' )
730733
@@ -785,15 +788,22 @@ export function TreemapVisualizer({
785788 if ( ! container ) return
786789
787790 const updateSize = ( ) => {
788- const rect = container . getBoundingClientRect ( )
789791 const dpr = window . devicePixelRatio || 1
790- setCssDimensions ( {
791- width : Math . floor ( rect . width ) ,
792- height : Math . floor ( rect . height ) ,
793- } )
794- setCanvasDimensions ( {
795- width : Math . floor ( rect . width * dpr ) ,
796- height : Math . floor ( rect . height * dpr ) ,
792+ setDimensions ( ( dimensions ) => {
793+ const rect = container . getBoundingClientRect ( )
794+ if (
795+ dimensions . cssWidth === Math . floor ( rect . width ) &&
796+ dimensions . cssHeight === Math . floor ( rect . height )
797+ ) {
798+ return dimensions
799+ }
800+
801+ return {
802+ cssWidth : Math . floor ( rect . width ) ,
803+ cssHeight : Math . floor ( rect . height ) ,
804+ canvasWidth : Math . floor ( rect . width * dpr ) ,
805+ canvasHeight : Math . floor ( rect . height * dpr ) ,
806+ }
797807 } )
798808 }
799809
@@ -813,8 +823,8 @@ export function TreemapVisualizer({
813823 {
814824 x : 0 ,
815825 y : 12 * focusedAncestorChain . length ,
816- width : cssDimensions . width ,
817- height : cssDimensions . height ,
826+ width : dimensions . cssWidth ,
827+ height : dimensions . cssHeight ,
818828 } ,
819829 filterSource ,
820830 sizeMode
@@ -826,8 +836,8 @@ export function TreemapVisualizer({
826836 focusedLayout ,
827837 focusedAncestorChain ,
828838 analyzeData ,
829- cssDimensions . width ,
830- cssDimensions . height ,
839+ dimensions . cssWidth ,
840+ dimensions . cssHeight ,
831841 12
832842 )
833843 }
@@ -837,13 +847,13 @@ export function TreemapVisualizer({
837847 analyzeData ,
838848 focusedSourceIndex ,
839849 focusedAncestorChain ,
840- cssDimensions . width ,
841- cssDimensions . height ,
850+ dimensions . cssWidth ,
851+ dimensions . cssHeight ,
842852 filterSource ,
843853 sizeMode ,
844854 ] )
845855
846- useEffect ( ( ) => {
856+ useLayoutEffect ( ( ) => {
847857 const canvas = canvasRef . current
848858 if ( ! canvas ) return
849859
@@ -854,7 +864,7 @@ export function TreemapVisualizer({
854864 ctx . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 )
855865 ctx . scale ( dpr , dpr )
856866
857- ctx . clearRect ( 0 , 0 , cssDimensions . width , cssDimensions . height )
867+ ctx . clearRect ( 0 , 0 , dimensions . cssWidth , dimensions . cssHeight )
858868
859869 drawTreemap (
860870 ctx ,
@@ -871,8 +881,8 @@ export function TreemapVisualizer({
871881 layout ,
872882 hoveredAncestorChain ,
873883 selectedAncestorChain ,
874- cssDimensions . width ,
875- cssDimensions . height ,
884+ dimensions . cssWidth ,
885+ dimensions . cssHeight ,
876886 isMouseInTreemap ,
877887 focusedAncestorChain ,
878888 searchQuery ,
@@ -1005,8 +1015,8 @@ export function TreemapVisualizer({
10051015 >
10061016 < canvas
10071017 ref = { canvasRef }
1008- width = { canvasDimensions . width }
1009- height = { canvasDimensions . height }
1018+ width = { dimensions . canvasWidth }
1019+ height = { dimensions . canvasHeight }
10101020 onMouseMove = { handleMouseMove }
10111021 onMouseLeave = { handleMouseLeave }
10121022 onClick = { handleClick }
0 commit comments