2020-05-30 17:32:32 +05:30
|
|
|
import { AppState, FlooredNumber } from "../types";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-05-30 17:32:32 +05:30
|
|
|
import { getCommonBounds, getClosestElementBounds } from "../element";
|
|
|
|
|
|
|
|
import {
|
|
|
|
sceneCoordsToViewportCoords,
|
|
|
|
viewportCoordsToSceneCoords,
|
|
|
|
} from "../utils";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const normalizeScroll = (pos: number) =>
|
|
|
|
Math.floor(pos) as FlooredNumber;
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-30 17:32:32 +05:30
|
|
|
function isOutsideViewPort(
|
|
|
|
appState: AppState,
|
|
|
|
canvas: HTMLCanvasElement | null,
|
|
|
|
cords: Array<number>,
|
|
|
|
) {
|
|
|
|
const [x1, y1, x2, y2] = cords;
|
|
|
|
const { x: viewportX1, y: viewportY1 } = sceneCoordsToViewportCoords(
|
|
|
|
{ sceneX: x1, sceneY: y1 },
|
|
|
|
appState,
|
|
|
|
canvas,
|
|
|
|
window.devicePixelRatio,
|
|
|
|
);
|
|
|
|
const { x: viewportX2, y: viewportY2 } = sceneCoordsToViewportCoords(
|
|
|
|
{ sceneX: x2, sceneY: y2 },
|
|
|
|
appState,
|
|
|
|
canvas,
|
|
|
|
window.devicePixelRatio,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
viewportX2 - viewportX1 > window.innerWidth ||
|
|
|
|
viewportY2 - viewportY1 > window.innerHeight
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const calculateScrollCenter = (
|
2020-03-07 10:20:38 -05:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-05-30 17:32:32 +05:30
|
|
|
appState: AppState,
|
|
|
|
canvas: HTMLCanvasElement | null,
|
2020-05-20 16:21:37 +03:00
|
|
|
): { scrollX: FlooredNumber; scrollY: FlooredNumber } => {
|
2020-04-04 18:45:14 +02:00
|
|
|
if (!elements.length) {
|
|
|
|
return {
|
|
|
|
scrollX: normalizeScroll(0),
|
|
|
|
scrollY: normalizeScroll(0),
|
|
|
|
};
|
|
|
|
}
|
2020-05-30 17:32:32 +05:30
|
|
|
const scale = window.devicePixelRatio;
|
|
|
|
let [x1, y1, x2, y2] = getCommonBounds(elements);
|
|
|
|
if (isOutsideViewPort(appState, canvas, [x1, y1, x2, y2])) {
|
|
|
|
[x1, y1, x2, y2] = getClosestElementBounds(
|
|
|
|
elements,
|
|
|
|
viewportCoordsToSceneCoords(
|
|
|
|
{ clientX: appState.scrollX, clientY: appState.scrollY },
|
|
|
|
appState,
|
|
|
|
canvas,
|
|
|
|
scale,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
const centerX = (x1 + x2) / 2;
|
|
|
|
const centerY = (y1 + y2) / 2;
|
|
|
|
|
|
|
|
return {
|
|
|
|
scrollX: normalizeScroll(window.innerWidth / 2 - centerX),
|
|
|
|
scrollY: normalizeScroll(window.innerHeight / 2 - centerY),
|
|
|
|
};
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|