2020-11-04 17:49:15 +00:00
|
|
|
import { NormalizedZoomValue, PointerCoords, Zoom } from "../types";
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-11-04 17:49:15 +00:00
|
|
|
export const getNewZoom = (
|
|
|
|
newZoomValue: NormalizedZoomValue,
|
|
|
|
prevZoom: Zoom,
|
2020-12-14 14:14:56 +00:00
|
|
|
canvasOffset: { left: number; top: number },
|
2020-11-04 17:49:15 +00:00
|
|
|
zoomOnViewportPoint: PointerCoords = { x: 0, y: 0 },
|
|
|
|
): Zoom => {
|
2020-02-15 21:03:32 +01:00
|
|
|
return {
|
2020-11-04 17:49:15 +00:00
|
|
|
value: newZoomValue,
|
|
|
|
translation: {
|
|
|
|
x:
|
|
|
|
zoomOnViewportPoint.x -
|
2020-12-14 14:14:56 +00:00
|
|
|
canvasOffset.left -
|
|
|
|
(zoomOnViewportPoint.x - canvasOffset.left - prevZoom.translation.x) *
|
2020-11-04 17:49:15 +00:00
|
|
|
(newZoomValue / prevZoom.value),
|
|
|
|
y:
|
|
|
|
zoomOnViewportPoint.y -
|
2020-12-14 14:14:56 +00:00
|
|
|
canvasOffset.top -
|
|
|
|
(zoomOnViewportPoint.y - canvasOffset.top - prevZoom.translation.y) *
|
2020-11-04 17:49:15 +00:00
|
|
|
(newZoomValue / prevZoom.value),
|
|
|
|
},
|
2020-02-15 21:03:32 +01:00
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-02-15 21:03:32 +01:00
|
|
|
|
2020-11-29 18:32:51 +02:00
|
|
|
export const getNormalizedZoom = (zoom: number): NormalizedZoomValue => {
|
2020-02-16 14:38:53 +01:00
|
|
|
const normalizedZoom = parseFloat(zoom.toFixed(2));
|
2021-01-30 18:03:23 +01:00
|
|
|
const clampedZoom = Math.max(0.1, Math.min(normalizedZoom, 10));
|
2020-11-04 17:49:15 +00:00
|
|
|
return clampedZoom as NormalizedZoomValue;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|