From 19c034fd129623abd0b32ff0f980a37733b2d7eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Thu, 17 Feb 2022 23:05:47 +0100 Subject: [PATCH 1/3] Fix setting center to undefined It only worked when the coordinate warning was shown, but fromUserCoordinate shouldn't have been called with undefined in the first place. --- src/ol/View.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ol/View.js b/src/ol/View.js index bda219cb0d..b52a7dc595 100644 --- a/src/ol/View.js +++ b/src/ol/View.js @@ -1636,7 +1636,9 @@ class View extends BaseObject { * @api */ setCenter(center) { - this.setCenterInternal(fromUserCoordinate(center, this.getProjection())); + this.setCenterInternal( + center ? fromUserCoordinate(center, this.getProjection()) : center + ); } /** From ab786cff73f64d88d8e2989bb7db3b0dd75b78ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Fri, 18 Feb 2022 00:06:31 +0100 Subject: [PATCH 2/3] Test setting resolution to undefined --- test/browser/spec/ol/View.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/browser/spec/ol/View.test.js b/test/browser/spec/ol/View.test.js index 1bb95219ca..fc75db8690 100644 --- a/test/browser/spec/ol/View.test.js +++ b/test/browser/spec/ol/View.test.js @@ -562,6 +562,18 @@ describe('ol/View', function () { }); }); + describe('#setResolution()', function () { + it('does not change center when set to undefined', function () { + const center = [1, 1]; + const view = new View({ + center: center.slice(), + resolution: 1, + }); + view.setResolution(undefined); + expect(view.getCenter()).to.eql(center); + }); + }); + describe('#setCenter()', function () { it('allows setting undefined center', function () { const view = new View({ From 5a49410d82fb22f7811d509dde9b61c67538b511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Kr=C3=B6g?= Date: Sun, 3 Oct 2021 19:36:16 +0200 Subject: [PATCH 3/3] Center should remain valid when resolution is not set --- src/ol/centerconstraint.js | 78 +++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/ol/centerconstraint.js b/src/ol/centerconstraint.js index 56f6307a36..2ccb477d47 100644 --- a/src/ol/centerconstraint.js +++ b/src/ol/centerconstraint.js @@ -18,52 +18,54 @@ export function createExtent(extent, onlyCenter, smooth) { return ( /** * @param {import("./coordinate.js").Coordinate|undefined} center Center. - * @param {number} resolution Resolution. + * @param {number|undefined} resolution Resolution. * @param {import("./size.js").Size} size Viewport size; unused if `onlyCenter` was specified. * @param {boolean} [opt_isMoving] True if an interaction or animation is in progress. * @param {Array} [opt_centerShift] Shift between map center and viewport center. * @return {import("./coordinate.js").Coordinate|undefined} Center. */ function (center, resolution, size, opt_isMoving, opt_centerShift) { - if (center) { - const viewWidth = onlyCenter ? 0 : size[0] * resolution; - const viewHeight = onlyCenter ? 0 : size[1] * resolution; - const shiftX = opt_centerShift ? opt_centerShift[0] : 0; - const shiftY = opt_centerShift ? opt_centerShift[1] : 0; - let minX = extent[0] + viewWidth / 2 + shiftX; - let maxX = extent[2] - viewWidth / 2 + shiftX; - let minY = extent[1] + viewHeight / 2 + shiftY; - let maxY = extent[3] - viewHeight / 2 + shiftY; - - // note: when zooming out of bounds, min and max values for x and y may - // end up inverted (min > max); this has to be accounted for - if (minX > maxX) { - minX = (maxX + minX) / 2; - maxX = minX; - } - if (minY > maxY) { - minY = (maxY + minY) / 2; - maxY = minY; - } - - let x = clamp(center[0], minX, maxX); - let y = clamp(center[1], minY, maxY); - const ratio = 30 * resolution; - - // during an interaction, allow some overscroll - if (opt_isMoving && smooth) { - x += - -ratio * Math.log(1 + Math.max(0, minX - center[0]) / ratio) + - ratio * Math.log(1 + Math.max(0, center[0] - maxX) / ratio); - y += - -ratio * Math.log(1 + Math.max(0, minY - center[1]) / ratio) + - ratio * Math.log(1 + Math.max(0, center[1] - maxY) / ratio); - } - - return [x, y]; - } else { + if (!center) { return undefined; } + if (!resolution && !onlyCenter) { + return center; + } + const viewWidth = onlyCenter ? 0 : size[0] * resolution; + const viewHeight = onlyCenter ? 0 : size[1] * resolution; + const shiftX = opt_centerShift ? opt_centerShift[0] : 0; + const shiftY = opt_centerShift ? opt_centerShift[1] : 0; + let minX = extent[0] + viewWidth / 2 + shiftX; + let maxX = extent[2] - viewWidth / 2 + shiftX; + let minY = extent[1] + viewHeight / 2 + shiftY; + let maxY = extent[3] - viewHeight / 2 + shiftY; + + // note: when zooming out of bounds, min and max values for x and y may + // end up inverted (min > max); this has to be accounted for + if (minX > maxX) { + minX = (maxX + minX) / 2; + maxX = minX; + } + if (minY > maxY) { + minY = (maxY + minY) / 2; + maxY = minY; + } + + let x = clamp(center[0], minX, maxX); + let y = clamp(center[1], minY, maxY); + + // during an interaction, allow some overscroll + if (opt_isMoving && smooth && resolution) { + const ratio = 30 * resolution; + x += + -ratio * Math.log(1 + Math.max(0, minX - center[0]) / ratio) + + ratio * Math.log(1 + Math.max(0, center[0] - maxX) / ratio); + y += + -ratio * Math.log(1 + Math.max(0, minY - center[1]) / ratio) + + ratio * Math.log(1 + Math.max(0, center[1] - maxY) / ratio); + } + + return [x, y]; } ); }