From 00bc424098d8df96f9d462cd57dffa371f9789b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:15:26 +0100 Subject: [PATCH 1/7] Add ol.coordinate.sub --- src/ol/coordinate.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ol/coordinate.js b/src/ol/coordinate.js index 81838416aa..c34247c41f 100644 --- a/src/ol/coordinate.js +++ b/src/ol/coordinate.js @@ -182,6 +182,18 @@ ol.coordinate.scale = function(coordinate, s) { }; +/** + * @param {ol.Coordinate} coordinate Coordinate. + * @param {ol.Coordinate} delta Delta. + * @return {ol.Coordinate} Coordinate. + */ +ol.coordinate.sub = function(coordinate, delta) { + coordinate[0] -= delta[0]; + coordinate[1] -= delta[1]; + return coordinate; +}; + + /** * @param {ol.Coordinate} coord1 First coordinate. * @param {ol.Coordinate} coord2 Second coordinate. From e03054f17f9239eb44ecab866bda4d2f3a05f1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:15:59 +0100 Subject: [PATCH 2/7] Add ol.View2D#rotate --- src/ol/view2d.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ol/view2d.js b/src/ol/view2d.js index b87e6dfe58..3f82ed736c 100644 --- a/src/ol/view2d.js +++ b/src/ol/view2d.js @@ -534,6 +534,18 @@ ol.View2D.prototype.isDef = function() { }; +/** + * Rotate the view around a given coordinate. + * @param {number} rotation New rotation value for the view. + * @param {ol.Coordinate} anchor The rotation center. + */ +ol.View2D.prototype.rotate = function(rotation, anchor) { + var center = this.calculateCenterRotate(rotation, anchor); + this.setRotation(rotation); + this.setCenter(center); +}; + + /** * Set the center of the current view. * @param {ol.Coordinate|undefined} center Center. From 00917b72628f9d5ed1bd7121250baa988ec5a91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:16:18 +0100 Subject: [PATCH 3/7] Export ol.View2D#rotate --- src/ol/view2d.exports | 1 + src/ol/view2d.js | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ol/view2d.exports b/src/ol/view2d.exports index 01d44dd7b5..e665804784 100644 --- a/src/ol/view2d.exports +++ b/src/ol/view2d.exports @@ -7,4 +7,5 @@ @exportProperty ol.View2D.prototype.centerOn @exportProperty ol.View2D.prototype.getView2D @exportProperty ol.View2D.prototype.getZoom +@exportProperty ol.View2D.prototype.rotate @exportProperty ol.View2D.prototype.setZoom diff --git a/src/ol/view2d.js b/src/ol/view2d.js index 3f82ed736c..e8df7039b1 100644 --- a/src/ol/view2d.js +++ b/src/ol/view2d.js @@ -537,12 +537,14 @@ ol.View2D.prototype.isDef = function() { /** * Rotate the view around a given coordinate. * @param {number} rotation New rotation value for the view. - * @param {ol.Coordinate} anchor The rotation center. + * @param {ol.Coordinate=} opt_anchor The rotation center. */ -ol.View2D.prototype.rotate = function(rotation, anchor) { - var center = this.calculateCenterRotate(rotation, anchor); +ol.View2D.prototype.rotate = function(rotation, opt_anchor) { + if (goog.isDef(opt_anchor)) { + var center = this.calculateCenterRotate(rotation, opt_anchor); + this.setCenter(center); + } this.setRotation(rotation); - this.setCenter(center); }; From 3c86dba3a81237272b117c46e58b6d0054d3b778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:17:10 +0100 Subject: [PATCH 4/7] ol.animate.rotate takes an optional anchor --- externs/olx.js | 16 +++++++++++++--- src/ol/animation.js | 13 +++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/externs/olx.js b/externs/olx.js index 1792f5eb15..4511a7d872 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -544,7 +544,8 @@ olx.animation.PanOptions.prototype.easing; /** - * @typedef {{rotation: number, + * @typedef {{rotation: (number|undefined), + * anchor: (ol.Coordinate|undefined), * start: (number|undefined), * duration: (number|undefined), * easing: (function(number):number|undefined)}} @@ -554,12 +555,21 @@ olx.animation.RotateOptions; /** - * The rotation to apply, in radians. - * @type {number} + * The rotation value (in radians) to begin rotating from, typically + * `map.getView().getRotation()`. If `undefined` then `0` is assumed. + * @type {number|undefined} */ olx.animation.RotateOptions.prototype.rotation; +/** + * The rotation center/anchor. The map rotates around the center of the view + * if unspecified. + * @type {ol.Coordinate|undefined} + */ +olx.animation.RotateOptions.prototype.anchor; + + /** * The start time of the animation. Default is immediately. * @type {number|undefined} diff --git a/src/ol/animation.js b/src/ol/animation.js index 938f838db9..eb092506da 100644 --- a/src/ol/animation.js +++ b/src/ol/animation.js @@ -4,6 +4,7 @@ goog.provide('ol.animation'); goog.require('ol.PreRenderFunction'); goog.require('ol.ViewHint'); +goog.require('ol.coordinate'); goog.require('ol.easing'); @@ -92,6 +93,8 @@ ol.animation.rotate = function(options) { var duration = goog.isDef(options.duration) ? options.duration : 1000; var easing = goog.isDef(options.easing) ? options.easing : ol.easing.inAndOut; + var anchor = goog.isDef(options.anchor) ? + options.anchor : null; return ( /** @@ -106,9 +109,15 @@ ol.animation.rotate = function(options) { } else if (frameState.time < start + duration) { var delta = 1 - easing((frameState.time - start) / duration); var deltaRotation = - sourceRotation - frameState.view2DState.rotation; + (sourceRotation - frameState.view2DState.rotation) * delta; frameState.animate = true; - frameState.view2DState.rotation += delta * deltaRotation; + frameState.view2DState.rotation += deltaRotation; + if (!goog.isNull(anchor)) { + var center = frameState.view2DState.center; + ol.coordinate.sub(center, anchor); + ol.coordinate.rotate(center, deltaRotation); + ol.coordinate.add(center, anchor); + } frameState.viewHints[ol.ViewHint.ANIMATING] += 1; return true; } else { From 59c3857afdb9f69f4e4971a9a7c5421a7a371aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:17:48 +0100 Subject: [PATCH 5/7] Add rotateAroundRome to animation example --- examples/animation.html | 1 + examples/animation.js | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/examples/animation.html b/examples/animation.html index 3eae57f489..dbe6d34ff3 100644 --- a/examples/animation.html +++ b/examples/animation.html @@ -32,6 +32,7 @@
+ diff --git a/examples/animation.js b/examples/animation.js index a14ea27954..de211bde63 100644 --- a/examples/animation.js +++ b/examples/animation.js @@ -49,6 +49,17 @@ rotateRight.addEventListener('click', function() { map.beforeRender(rotateRight); }, false); +var rotateAroundRome = document.getElementById('rotate-around-rome'); +rotateAroundRome.addEventListener('click', function() { + var currentRotation = view.getRotation(); + var rotateAroundRome = ol.animation.rotate({ + anchor: rome, + duration: 1000, + rotation: currentRotation + }); + map.beforeRender(rotateAroundRome); + view.rotate(currentRotation + (Math.PI / 2), rome); +}, false); var panToLondon = document.getElementById('pan-to-london'); panToLondon.addEventListener('click', function() { From 42adc23e8613982175c32001a678adf8e411ce54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 10:36:36 +0100 Subject: [PATCH 6/7] Use ol.View2D#rotate in Interaction --- src/ol/interaction/interaction.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ol/interaction/interaction.js b/src/ol/interaction/interaction.js index dbc57deee1..307b6e07d3 100644 --- a/src/ol/interaction/interaction.js +++ b/src/ol/interaction/interaction.js @@ -128,11 +128,7 @@ ol.interaction.Interaction.rotateWithoutConstraints = } } goog.asserts.assertInstanceof(view, ol.View2D); - if (goog.isDefAndNotNull(opt_anchor)) { - var center = view.calculateCenterRotate(rotation, opt_anchor); - view.setCenter(center); - } - view.setRotation(rotation); + view.rotate(rotation, opt_anchor); } }; From f9388749d40b0609162e06d817e6861a9b68b4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 27 Feb 2014 11:00:35 +0100 Subject: [PATCH 7/7] undefined may be passed to ol.animation.rotate --- src/ol/animation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/animation.js b/src/ol/animation.js index eb092506da..50c69921d5 100644 --- a/src/ol/animation.js +++ b/src/ol/animation.js @@ -88,7 +88,7 @@ ol.animation.pan = function(options) { * @todo stability experimental */ ol.animation.rotate = function(options) { - var sourceRotation = options.rotation; + var sourceRotation = goog.isDef(options.rotation) ? options.rotation : 0; var start = goog.isDef(options.start) ? options.start : goog.now(); var duration = goog.isDef(options.duration) ? options.duration : 1000; var easing = goog.isDef(options.easing) ?