[Web] Canvas backing store ignores CSS transforms, scaled canvases render blurry
Description
On web, WebGLRenderer.onResize sizes the canvas backing store from canvas.clientWidth * devicePixelRatio. clientWidth is the layout size and ignores CSS transforms on ancestors, so a <Canvas> inside a transform: scale() gets a surface rasterised for its unscaled box. The browser then resamples that bitmap onto the painted area, and text and thin lines come out soft (scale below 1) or blurry (scale above 1).
We hit this in Expensify's chat charts: a 680px wide chart fitted into a 516px column at DPR 2 has canvas.width 1360 while it is painted across 1032 device pixels.
Expected: the backing store matches the painted size, so text is as sharp as the DOM text around it.
React Native Skia Version
2.11.2, and the same code is on main (8f4b85c)
React Native Version
0.86.0 (react-native-web 0.21.2)
Using New Architecture
- Enabled
Steps to Reproduce
- Render a
<Canvas>with some text inside a view that hastransform: [{scale: 0.75}], on web at devicePixelRatio 2. - In devtools, compare
canvas.widthwithcanvas.getBoundingClientRect().width * devicePixelRatio. - They differ (1360 vs 1020 for a 680px canvas), and the text is visibly softer than the same text drawn without the transform.
Snack, Code Example, Screenshot, or Link to Repository
<View style={{transform: [{scale: 0.75}], transformOrigin: 'top left'}}>
<Canvas style={{width: 680, height: 430}}>
<Text
x={32}
y={40}
text="Monthly spend"
font={font}
/>
</Canvas>
</View>const c = document.querySelector('canvas');
[c.width, Math.round(c.getBoundingClientRect().width * devicePixelRatio)]; // [1360, 1020]Downstream report with before/after screenshots: https://github.com/Expensify/App/issues/95221
Source: Shopify/react-native-skia