excalidraw/src/scene/zoom.ts
Christopher Chedeau 2937efacde
Remove last get/setTransform (#964)
My original hack to put the scale when we create the canvas element doesn't make much sense. It should be done when we are rendering the scene. I moved it there in this PR.

The rest was all about forwarding the scale to where it's needed.
2020-03-15 12:25:18 -07:00

24 lines
639 B
TypeScript

export function getZoomOrigin(canvas: HTMLCanvasElement | null, scale: number) {
if (canvas === null) {
return { x: 0, y: 0 };
}
const context = canvas.getContext("2d");
if (context === null) {
return { x: 0, y: 0 };
}
const normalizedCanvasWidth = canvas.width / scale;
const normalizedCanvasHeight = canvas.height / scale;
return {
x: normalizedCanvasWidth / 2,
y: normalizedCanvasHeight / 2,
};
}
export function getNormalizedZoom(zoom: number): number {
const normalizedZoom = parseFloat(zoom.toFixed(2));
const clampedZoom = Math.max(0.1, Math.min(normalizedZoom, 2));
return clampedZoom;
}