From 747a6ae0fedaebf82ca820034238531e2b59bfec Mon Sep 17 00:00:00 2001 From: Aleksey Kuznietsov Date: Fri, 29 Nov 2024 06:49:19 -0500 Subject: [PATCH] fix: check _map is assigned to avoid errors on often redraw from scratch --- src/canvas-overlay.ts | 46 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/canvas-overlay.ts b/src/canvas-overlay.ts index f4e1a09..ed7078a 100644 --- a/src/canvas-overlay.ts +++ b/src/canvas-overlay.ts @@ -165,29 +165,31 @@ export class CanvasOverlay extends Layer { _redraw(): void { const { _map, canvas } = this; - const size = _map.getSize(); - const bounds = _map.getBounds(); - const zoomScale = - (size.x * 180) / (20037508.34 * (bounds.getEast() - bounds.getWest())); // resolution = 1/zoomScale - const zoom = _map.getZoom(); - const topLeft = new LatLng(bounds.getNorth(), bounds.getWest()); - const offset = this._unclampedProject(topLeft, 0); - if (canvas) { - this._userDrawFunc({ - bounds, - canvas, - offset, - scale: Math.pow(2, zoom), - size, - zoomScale, - zoom, - }); - } + if (_map) { + const size = _map.getSize(); + const bounds = _map.getBounds(); + const zoomScale = + (size.x * 180) / (20037508.34 * (bounds.getEast() - bounds.getWest())); // resolution = 1/zoomScale + const zoom = _map.getZoom(); + const topLeft = new LatLng(bounds.getNorth(), bounds.getWest()); + const offset = this._unclampedProject(topLeft, 0); + if (canvas) { + this._userDrawFunc({ + bounds, + canvas, + offset, + scale: Math.pow(2, zoom), + size, + zoomScale, + zoom, + }); + } - while (this._redrawCallbacks.length > 0) { - const callback = this._redrawCallbacks.shift(); - if (callback) { - callback(this); + while (this._redrawCallbacks.length > 0) { + const callback = this._redrawCallbacks.shift(); + if (callback) { + callback(this); + } } }