Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix scrollToCenter when no elements supplied (#1222)

David Luzar4dd947b26149
diff --git a/src/element/bounds.ts b/src/element/bounds.ts
index bee744fb4c..ed42439601 100644
--- a/src/element/bounds.ts
+++ b/src/element/bounds.ts
@@ -187,6 +187,10 @@ export function getArrowPoints(
}
export function getCommonBounds(elements: readonly ExcalidrawElement[]) {
+ if (!elements.length) {
+ return [0, 0, 0, 0];
+ }
+
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
diff --git a/src/scene/scroll.ts b/src/scene/scroll.ts
index d065f2ddc9..e77c00faf5 100644
--- a/src/scene/scroll.ts
+++ b/src/scene/scroll.ts
@@ -9,6 +9,13 @@ export function normalizeScroll(pos: number) {
export function calculateScrollCenter(
elements: readonly ExcalidrawElement[],
): { scrollX: FlooredNumber; scrollY: FlooredNumber } {
+ if (!elements.length) {
+ return {
+ scrollX: normalizeScroll(0),
+ scrollY: normalizeScroll(0),
+ };
+ }
+
const [x1, y1, x2, y2] = getCommonBounds(elements);
const centerX = (x1 + x2) / 2;