From 4dd947b261499dc8f747349912b2310e03212b10 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Sat, 4 Apr 2020 18:45:14 +0200 Subject: [PATCH] fix scrollToCenter when no elements supplied (#1222) * fix scrollToCenter when no elements supplied * make getCommonBounds return default values on empty elements --- src/element/bounds.ts | 4 ++++ src/scene/scroll.ts | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/element/bounds.ts b/src/element/bounds.ts index bee744fb..ed424396 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 d065f2dd..e77c00fa 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;