2020-03-07 10:20:38 -05:00
|
|
|
import { FlooredNumber } from "../types";
|
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { getCommonBounds } from "../element";
|
|
|
|
|
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-20 16:21:37 +03:00
|
|
|
export const calculateScrollCenter = (
|
2020-03-07 10:20:38 -05:00
|
|
|
elements: readonly ExcalidrawElement[],
|
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-03-07 10:20:38 -05:00
|
|
|
const [x1, y1, x2, y2] = getCommonBounds(elements);
|
|
|
|
|
|
|
|
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
|
|
|
};
|