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({
cssHeight: number
canvasWidth: number
canvasHeight: number
devicePixelRatio: number
}>({
cssWidth: 1200,
cssHeight: 800,
canvasWidth: 1200,
canvasHeight: 800,
devicePixelRatio: 1,
})
const [, _setTheme] = useState<'light' | 'dark'>('light')
......@@ -811,21 +813,24 @@ export function TreemapVisualizer({
if (!container) return
const updateSize = () => {
const dpr = window.devicePixelRatio || 1
const devicePixelRatio = window.devicePixelRatio || 1
setDimensions((dimensions) => {
const rect = container.getBoundingClientRect()
const cssWidth = container.clientWidth
const cssHeight = container.clientHeight
if (
dimensions.cssWidth === Math.floor(rect.width) &&
dimensions.cssHeight === Math.floor(rect.height)
dimensions.cssWidth === cssWidth &&
dimensions.cssHeight === cssHeight &&
dimensions.devicePixelRatio === devicePixelRatio
) {
return dimensions
}
return {
cssWidth: Math.floor(rect.width),
cssHeight: Math.floor(rect.height),
canvasWidth: Math.floor(rect.width * dpr),
canvasHeight: Math.floor(rect.height * dpr),
cssWidth,
cssHeight,
canvasWidth: Math.round(cssWidth * devicePixelRatio),
canvasHeight: Math.round(cssHeight * devicePixelRatio),
devicePixelRatio,
}
})
}
......@@ -835,7 +840,23 @@ export function TreemapVisualizer({
const resizeObserver = new ResizeObserver(updateSize)
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(() => {
......@@ -883,9 +904,8 @@ export function TreemapVisualizer({
const ctx = canvas.getContext('2d')
if (!ctx) return
const dpr = window.devicePixelRatio || 1
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)
......@@ -908,6 +928,7 @@ export function TreemapVisualizer({
selectedAncestorChain,
dimensions.cssWidth,
dimensions.cssHeight,
dimensions.devicePixelRatio,
isMouseInTreemap,
focusedAncestorChain,
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