Skip to content

Commit

Permalink
ui-spacetimechart: add support for HiDPI
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emersion committed Feb 5, 2025
1 parent 54e521d commit 88e5a1d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions ui-spacetimechart/src/hooks/useCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,21 @@ export function useCanvas(

// Handle resizing:
useEffect(() => {
const scale = window.devicePixelRatio || 1;

for (const id in canvasesRef.current) {
const canvas = canvasesRef.current[id];
const ctx = contextsRef.current[id];

if (canvas) {
canvas.style.width = size.width + 'px';
canvas.style.height = size.height + 'px';
canvas.setAttribute('width', size.width + 'px');
canvas.setAttribute('height', size.height + 'px');
canvas.setAttribute('width', size.width * scale + 'px');
canvas.setAttribute('height', size.height * scale + 'px');

// Reset the transform to identity, then apply the new scale
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(scale, scale);
}
}

Expand Down

0 comments on commit 88e5a1d

Please sign in to comment.