Handle event coordinate in the user projection

This commit is contained in:
Tim Schaub
2019-09-26 15:21:26 +02:00
parent 1e27846d6d
commit 5d4e77151c
6 changed files with 110 additions and 6 deletions

View File

@@ -1237,6 +1237,17 @@ class View extends BaseObject {
* @api
*/
adjustResolution(ratio, opt_anchor) {
const anchor = opt_anchor && fromUserCoordinate(opt_anchor, this.getProjection());
this.adjustResolutionInternal(ratio, anchor);
}
/**
* Multiply the view resolution by a ratio, optionally using an anchor. Any resolution
* constraint will apply.
* @param {number} ratio The ratio to apply on the view resolution.
* @param {import("./coordinate.js").Coordinate=} opt_anchor The origin of the transformation.
*/
adjustResolutionInternal(ratio, opt_anchor) {
const isMoving = this.getAnimating() || this.getInteracting();
const size = this.getSizeFromViewport_(this.getRotation());
const newResolution = this.constraints_.resolution(this.targetResolution_ * ratio, 0, size, isMoving);
@@ -1448,6 +1459,18 @@ class View extends BaseObject {
* @api
*/
endInteraction(opt_duration, opt_resolutionDirection, opt_anchor) {
const anchor = opt_anchor && fromUserCoordinate(opt_anchor, this.getProjection());
this.endInteractionInternal(opt_duration, opt_resolutionDirection, anchor);
}
/**
* Notify the View that an interaction has ended. The view state will be resolved
* to a stable one if needed (depending on its constraints).
* @param {number=} opt_duration Animation duration in ms.
* @param {number=} opt_resolutionDirection Which direction to zoom.
* @param {import("./coordinate.js").Coordinate=} opt_anchor The origin of the transformation.
*/
endInteractionInternal(opt_duration, opt_resolutionDirection, opt_anchor) {
this.setHint(ViewHint.INTERACTING, -1);
this.resolveConstraints(opt_duration, opt_resolutionDirection, opt_anchor);

View File

@@ -91,7 +91,7 @@ class DragRotateAndZoom extends PointerInteraction {
}
this.lastAngle_ = theta;
if (this.lastMagnitude_ !== undefined) {
view.adjustResolution(this.lastMagnitude_ / magnitude);
view.adjustResolutionInternal(this.lastMagnitude_ / magnitude);
}
if (this.lastMagnitude_ !== undefined) {
this.lastScaleDelta_ = this.lastMagnitude_ / magnitude;

View File

@@ -122,7 +122,7 @@ export function pan(view, delta, opt_duration) {
/**
* @param {import("../View.js").default} view View.
* @param {number} delta Delta from previous zoom level.
* @param {import("../coordinate.js").Coordinate=} opt_anchor Anchor coordinate.
* @param {import("../coordinate.js").Coordinate=} opt_anchor Anchor coordinate in the user projection.
* @param {number=} opt_duration Duration.
*/
export function zoomByDelta(view, delta, opt_anchor, opt_duration) {
@@ -138,7 +138,7 @@ export function zoomByDelta(view, delta, opt_anchor, opt_duration) {
if (view.getAnimating()) {
view.cancelAnimations();
}
view.animateInternal({
view.animate({
resolution: newResolution,
anchor: opt_anchor,
duration: opt_duration !== undefined ? opt_duration : 250,

View File

@@ -95,7 +95,7 @@ class PinchZoom extends PointerInteraction {
// scale, bypass the resolution constraint
map.render();
view.adjustResolution(scaleDelta, this.anchor_);
view.adjustResolutionInternal(scaleDelta, this.anchor_);
}
/**

View File

@@ -1,7 +1,8 @@
import Map from '../../../../src/ol/Map.js';
import {Map, View} from '../../../../src/ol/index.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Interaction, {zoomByDelta} from '../../../../src/ol/interaction/Interaction.js';
import {FALSE} from '../../../../src/ol/functions.js';
import {useGeographic, clearUserProjection} from '../../../../src/ol/proj.js';
describe('ol.interaction.Interaction', function() {
@@ -87,3 +88,25 @@ describe('ol.interaction.Interaction', function() {
});
});
describe('zoomByDelta - useGeographic', () => {
beforeEach(useGeographic);
afterEach(clearUserProjection);
it('works with a user projection set', done => {
const view = new View({
center: [0, 0],
zoom: 0
});
const anchor = [90, 45];
const duration = 10;
zoomByDelta(view, 1, anchor, duration);
setTimeout(() => {
const center = view.getCenter();
expect(center[0]).to.be(45);
expect(center[1]).to.roughlyEqual(24.4698, 1e-4);
done();
}, 2 * duration);
});
});

View File

@@ -8,6 +8,7 @@ import {createEmpty} from '../../../src/ol/extent.js';
import Circle from '../../../src/ol/geom/Circle.js';
import LineString from '../../../src/ol/geom/LineString.js';
import Point from '../../../src/ol/geom/Point.js';
import {useGeographic, clearUserProjection} from '../../../src/ol/proj.js';
describe('ol.View', function() {
@@ -1890,6 +1891,63 @@ describe('ol.View', function() {
expect(view.getCenter()).to.eql([0, 100 - halfSize]);
});
});
describe('#adjustZoom() - useGeographic', () => {
beforeEach(useGeographic);
afterEach(clearUserProjection);
it('changes view resolution', () => {
const view = new View({
resolution: 1,
resolutions: [4, 2, 1, 0.5, 0.25]
});
view.adjustZoom(1);
expect(view.getResolution()).to.be(0.5);
view.adjustZoom(-1);
expect(view.getResolution()).to.be(1);
view.adjustZoom(2);
expect(view.getResolution()).to.be(0.25);
view.adjustZoom(-2);
expect(view.getResolution()).to.be(1);
});
it('changes view resolution and center relative to the anchor', function() {
const view = new View({
center: [0, 0],
zoom: 0
});
let center;
view.adjustZoom(1, [90, 45]);
center = view.getCenter();
expect(center[0]).to.be(45);
expect(center[1]).to.roughlyEqual(24.4698, 1e-4);
view.adjustZoom(-1, [90, 45]);
center = view.getCenter();
expect(center[0]).to.roughlyEqual(0, 1e-10);
expect(center[1]).to.roughlyEqual(0, 1e-10);
view.adjustZoom(2, [-90, -45]);
center = view.getCenter();
expect(center[0]).to.be(-67.5);
expect(center[1]).to.roughlyEqual(-35.3836, 1e-4);
view.adjustZoom(-2, [-90, -45]);
center = view.getCenter();
expect(center[0]).to.roughlyEqual(0, 1e-10);
expect(center[1]).to.roughlyEqual(0, 1e-10);
});
});
});
describe('does not start unexpected animations during interaction', function() {