Merge pull request #1975 from fredj/rotation-anchor
Rotate around arbitrary coordinate
This commit is contained in:
@@ -4,6 +4,7 @@ goog.provide('ol.animation');
|
||||
|
||||
goog.require('ol.PreRenderFunction');
|
||||
goog.require('ol.ViewHint');
|
||||
goog.require('ol.coordinate');
|
||||
goog.require('ol.easing');
|
||||
|
||||
|
||||
@@ -87,11 +88,13 @@ 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) ?
|
||||
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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -534,6 +534,20 @@ ol.View2D.prototype.isDef = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Rotate the view around a given coordinate.
|
||||
* @param {number} rotation New rotation value for the view.
|
||||
* @param {ol.Coordinate=} opt_anchor The rotation center.
|
||||
*/
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Set the center of the current view.
|
||||
* @param {ol.Coordinate|undefined} center Center.
|
||||
|
||||
Reference in New Issue
Block a user