Skip to content

Commit

Permalink
refactor(frontend): crisper zoom in WebKitGTK
Browse files Browse the repository at this point in the history
Use the non-standard zoom: property whenever available,
because it gives a crisper result than transform: scale() in WebKitGTK.

See https://developer.mozilla.org/en-US/docs/Web/CSS/zoom
  • Loading branch information
kris7t committed Apr 27, 2024
1 parent 85a3ee2 commit c934d31
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions subprojects/frontend/src/graph/ZoomCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type FitZoomCallback = ((newSize?: {
}) => void) &
((newSize: boolean) => void);

const useZoom = 'zoom' in document.body.style;

export default function ZoomCanvas({
children,
fitPadding,
Expand Down Expand Up @@ -83,8 +85,12 @@ export default function ZoomCanvas({
if (newSize === undefined || typeof newSize === 'boolean') {
const elementRect = elementRef.current.getBoundingClientRect();
const currentFactor = d3.zoomTransform(canvasRef.current).k;
width = elementRect.width / currentFactor;
height = elementRect.height / currentFactor;
if (useZoom) {
({ width, height } = elementRect);
} else {
width = elementRect.width / currentFactor;
height = elementRect.height / currentFactor;
}
} else {
({ width, height } = newSize);
}
Expand Down Expand Up @@ -194,11 +200,20 @@ export default function ZoomCanvas({
position: 'absolute',
top: '50%',
left: '50%',
transform: `
translate(${zoom.x}px, ${zoom.y}px)
scale(${zoom.k})
translate(-50%, -50%)
`,
...(useZoom
? {
transform: `
translate(calc(${zoom.x / zoom.k}px - 50%), calc(${zoom.y / zoom.k}px - 50%))
`,
zoom: zoom.k,
}
: {
transform: `
translate(${zoom.x}px, ${zoom.y}px)
scale(${zoom.k})
translate(-50%, -50%)
`,
}),
transformOrigin: '0 0',
}}
ref={elementRef}
Expand Down

0 comments on commit c934d31

Please sign in to comment.