Unverified Commit 36e51187 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Fix blurry bundle analyzer treemap on low-DPI displays (#99197)

## Summary

Size the treemap canvas from its content box so the backing store is not
scaled to fit inside the bordered container. Recreate the backing store
when the device pixel ratio changes.

## Verification

- Manually verified treemap rendering and backing dimensions on
standard- and high-DPI displays

<!-- NEXT_JS_LLM -->
parent a0ed5fa7
...@@ -746,11 +746,13 @@ export function TreemapVisualizer({ ...@@ -746,11 +746,13 @@ export function TreemapVisualizer({
cssHeight: number cssHeight: number
canvasWidth: number canvasWidth: number
canvasHeight: number canvasHeight: number
devicePixelRatio: number
}>({ }>({
cssWidth: 1200, cssWidth: 1200,
cssHeight: 800, cssHeight: 800,
canvasWidth: 1200, canvasWidth: 1200,
canvasHeight: 800, canvasHeight: 800,
devicePixelRatio: 1,
}) })
const [, _setTheme] = useState<'light' | 'dark'>('light') const [, _setTheme] = useState<'light' | 'dark'>('light')
...@@ -811,21 +813,24 @@ export function TreemapVisualizer({ ...@@ -811,21 +813,24 @@ export function TreemapVisualizer({
if (!container) return if (!container) return
const updateSize = () => { const updateSize = () => {
const dpr = window.devicePixelRatio || 1 const devicePixelRatio = window.devicePixelRatio || 1
setDimensions((dimensions) => { setDimensions((dimensions) => {
const rect = container.getBoundingClientRect() const cssWidth = container.clientWidth
const cssHeight = container.clientHeight
if ( if (
dimensions.cssWidth === Math.floor(rect.width) && dimensions.cssWidth === cssWidth &&
dimensions.cssHeight === Math.floor(rect.height) dimensions.cssHeight === cssHeight &&
dimensions.devicePixelRatio === devicePixelRatio
) { ) {
return dimensions return dimensions
} }
return { return {
cssWidth: Math.floor(rect.width), cssWidth,
cssHeight: Math.floor(rect.height), cssHeight,
canvasWidth: Math.floor(rect.width * dpr), canvasWidth: Math.round(cssWidth * devicePixelRatio),
canvasHeight: Math.floor(rect.height * dpr), canvasHeight: Math.round(cssHeight * devicePixelRatio),
devicePixelRatio,
} }
}) })
} }
...@@ -835,7 +840,23 @@ export function TreemapVisualizer({ ...@@ -835,7 +840,23 @@ export function TreemapVisualizer({
const resizeObserver = new ResizeObserver(updateSize) const resizeObserver = new ResizeObserver(updateSize)
resizeObserver.observe(container) resizeObserver.observe(container)
return () => resizeObserver.disconnect() let resolutionQuery = window.matchMedia(
`(resolution: ${window.devicePixelRatio || 1}dppx)`
)
const handleResolutionChange = () => {
resolutionQuery.removeEventListener('change', handleResolutionChange)
updateSize()
resolutionQuery = window.matchMedia(
`(resolution: ${window.devicePixelRatio || 1}dppx)`
)
resolutionQuery.addEventListener('change', handleResolutionChange)
}
resolutionQuery.addEventListener('change', handleResolutionChange)
return () => {
resizeObserver.disconnect()
resolutionQuery.removeEventListener('change', handleResolutionChange)
}
}, []) }, [])
const layout = useMemo(() => { const layout = useMemo(() => {
...@@ -883,9 +904,8 @@ export function TreemapVisualizer({ ...@@ -883,9 +904,8 @@ export function TreemapVisualizer({
const ctx = canvas.getContext('2d') const ctx = canvas.getContext('2d')
if (!ctx) return if (!ctx) return
const dpr = window.devicePixelRatio || 1
ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.setTransform(1, 0, 0, 1, 0, 0)
ctx.scale(dpr, dpr) ctx.scale(dimensions.devicePixelRatio, dimensions.devicePixelRatio)
ctx.clearRect(0, 0, dimensions.cssWidth, dimensions.cssHeight) ctx.clearRect(0, 0, dimensions.cssWidth, dimensions.cssHeight)
...@@ -908,6 +928,7 @@ export function TreemapVisualizer({ ...@@ -908,6 +928,7 @@ export function TreemapVisualizer({
selectedAncestorChain, selectedAncestorChain,
dimensions.cssWidth, dimensions.cssWidth,
dimensions.cssHeight, dimensions.cssHeight,
dimensions.devicePixelRatio,
isMouseInTreemap, isMouseInTreemap,
focusedAncestorChain, focusedAncestorChain,
searchQuery, searchQuery,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment