excalidraw/src/scene/zoom.ts

31 lines
961 B
TypeScript
Raw Normal View History

2020-11-04 17:49:15 +00:00
import { NormalizedZoomValue, PointerCoords, Zoom } from "../types";
2020-11-04 17:49:15 +00:00
export const getNewZoom = (
newZoomValue: NormalizedZoomValue,
prevZoom: Zoom,
canvasOffset: { left: number; top: number },
2020-11-04 17:49:15 +00:00
zoomOnViewportPoint: PointerCoords = { x: 0, y: 0 },
): Zoom => {
return {
2020-11-04 17:49:15 +00:00
value: newZoomValue,
translation: {
x:
zoomOnViewportPoint.x -
canvasOffset.left -
(zoomOnViewportPoint.x - canvasOffset.left - prevZoom.translation.x) *
2020-11-04 17:49:15 +00:00
(newZoomValue / prevZoom.value),
y:
zoomOnViewportPoint.y -
canvasOffset.top -
(zoomOnViewportPoint.y - canvasOffset.top - prevZoom.translation.y) *
2020-11-04 17:49:15 +00:00
(newZoomValue / prevZoom.value),
},
};
};
export const getNormalizedZoom = (zoom: number): NormalizedZoomValue => {
const normalizedZoom = parseFloat(zoom.toFixed(2));
const clampedZoom = Math.max(0.1, Math.min(normalizedZoom, 2));
2020-11-04 17:49:15 +00:00
return clampedZoom as NormalizedZoomValue;
};