Skip to content

Commit 88e5a1d

Browse files
committed
ui-spacetimechart: add support for HiDPI
Multiply the canvas buffer size by the number of physical pixels per logical pixels. Use CSS to set the size of the canvas element in logical pixels. Signed-off-by: Simon Ser <[email protected]> References: #757
1 parent 54e521d commit 88e5a1d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ui-spacetimechart/src/hooks/useCanvas.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,21 @@ export function useCanvas(
210210

211211
// Handle resizing:
212212
useEffect(() => {
213+
const scale = window.devicePixelRatio || 1;
214+
213215
for (const id in canvasesRef.current) {
214216
const canvas = canvasesRef.current[id];
217+
const ctx = contextsRef.current[id];
215218

216219
if (canvas) {
217220
canvas.style.width = size.width + 'px';
218221
canvas.style.height = size.height + 'px';
219-
canvas.setAttribute('width', size.width + 'px');
220-
canvas.setAttribute('height', size.height + 'px');
222+
canvas.setAttribute('width', size.width * scale + 'px');
223+
canvas.setAttribute('height', size.height * scale + 'px');
224+
225+
// Reset the transform to identity, then apply the new scale
226+
ctx.setTransform(1, 0, 0, 1, 0, 0);
227+
ctx.scale(scale, scale);
221228
}
222229
}
223230

0 commit comments

Comments
 (0)