Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiled image rotation #1006

Merged
merged 17 commits into from Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/drawer.js
Expand Up @@ -328,8 +328,7 @@ $.Drawer.prototype = {
// the viewport get rotated later on, we will need to resize it.
if (this.viewport.getRotation() === 0) {
var self = this;
this.viewer.addHandler('rotate', function resizeSketchCanvas() {
self.viewer.removeHandler('rotate', resizeSketchCanvas);
this.viewer.addOnceHandler('rotate', function resizeSketchCanvas() {
var sketchCanvasSize = self._calculateSketchCanvasSize();
self.sketchCanvas.width = sketchCanvasSize.x;
self.sketchCanvas.height = sketchCanvasSize.y;
Expand Down Expand Up @@ -464,7 +463,7 @@ $.Drawer.prototype = {
},

// private
drawDebugInfo: function( tile, count, i ){
drawDebugInfo: function(tile, count, i, tiledImage) {
if ( !this.useCanvas ) {
return;
}
Expand All @@ -477,7 +476,14 @@ $.Drawer.prototype = {
context.fillStyle = this.debugGridColor;

if ( this.viewport.degrees !== 0 ) {
this._offsetForRotation(this.viewport.degrees);
this._offsetForRotation({degrees: this.viewport.degrees});
}
if (tiledImage.getRotation() !== 0) {
this._offsetForRotation({
degrees: tiledImage.getRotation(),
point: tiledImage.viewport.pixelFromPointNoRotate(
tiledImage._getRotationPoint(true), true)
});
}

context.strokeRect(
Expand Down Expand Up @@ -541,6 +547,9 @@ $.Drawer.prototype = {
if ( this.viewport.degrees !== 0 ) {
this._restoreRotationChanges();
}
if (tiledImage.getRotation() !== 0) {
this._restoreRotationChanges();
}
context.restore();
},

Expand Down Expand Up @@ -574,17 +583,22 @@ $.Drawer.prototype = {
return new $.Point(canvas.width, canvas.height);
},

getCanvasCenter: function() {
return new $.Point(this.canvas.width / 2, this.canvas.height / 2);
},

// private
_offsetForRotation: function(degrees, useSketch) {
var cx = this.canvas.width / 2;
var cy = this.canvas.height / 2;
_offsetForRotation: function(options) {
var point = options.point ?
options.point.times($.pixelDensityRatio) :
this.getCanvasCenter();

var context = this._getContext(useSketch);
var context = this._getContext(options.useSketch);
context.save();

context.translate(cx, cy);
context.rotate(Math.PI / 180 * degrees);
context.translate(-cx, -cy);
context.translate(point.x, point.y);
context.rotate(Math.PI / 180 * options.degrees);
context.translate(-point.x, -point.y);
},

// private
Expand Down
7 changes: 5 additions & 2 deletions src/navigator.js
Expand Up @@ -341,9 +341,12 @@ $.extend( $.Navigator.prototype, $.EventSource.prototype, $.Viewer.prototype, /*
myItem._originalForNavigator = original;
_this._matchBounds(myItem, original, true);

original.addHandler('bounds-change', function() {
function matchBounds() {
_this._matchBounds(myItem, original);
});
}

original.addHandler('bounds-change', matchBounds);
original.addHandler('clip-change', matchBounds);
}
});

Expand Down
15 changes: 15 additions & 0 deletions src/openseadragon.js
Expand Up @@ -1375,6 +1375,21 @@ function OpenSeadragon( options ){
return string.charAt(0).toUpperCase() + string.slice(1);
},

/**
* Compute the modulo of a number but makes sure to always return
* a positive value.
* @param {Number} number the number to computes the modulo of
* @param {Number} modulo the modulo
* @returns {Number} the result of the modulo of number
*/
positiveModulo: function(number, modulo) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

var result = number % modulo;
if (result < 0) {
result += modulo;
}
return result;
},

/**
* Determines if a point is within the bounding rectangle of the given element (hit-test).
* @function
Expand Down
5 changes: 1 addition & 4 deletions src/point.js
Expand Up @@ -190,10 +190,7 @@ $.Point.prototype = {
var sin;
// Avoid float computations when possible
if (degrees % 90 === 0) {
var d = degrees % 360;
if (d < 0) {
d += 360;
}
var d = $.positiveModulo(degrees, 360);
switch (d) {
case 0:
cos = 1;
Expand Down
15 changes: 7 additions & 8 deletions src/rectangle.js
Expand Up @@ -81,10 +81,7 @@ $.Rect = function(x, y, width, height, degrees) {
this.degrees = typeof(degrees) === "number" ? degrees : 0;

// Normalizes the rectangle.
this.degrees = this.degrees % 360;
if (this.degrees < 0) {
this.degrees += 360;
}
this.degrees = $.positiveModulo(this.degrees, 360);
var newTopLeft, newWidth;
if (this.degrees >= 270) {
newTopLeft = this.getTopRight();
Expand Down Expand Up @@ -442,19 +439,21 @@ $.Rect.prototype = {
* @return {OpenSeadragon.Rect}
*/
rotate: function(degrees, pivot) {
degrees = degrees % 360;
degrees = $.positiveModulo(degrees, 360);
if (degrees === 0) {
return this.clone();
}
if (degrees < 0) {
degrees += 360;
}

pivot = pivot || this.getCenter();
var newTopLeft = this.getTopLeft().rotate(degrees, pivot);
var newTopRight = this.getTopRight().rotate(degrees, pivot);

var diff = newTopRight.minus(newTopLeft);
// Handle floating point error
diff = diff.apply(function(x) {
var EPSILON = 1e-15;
return Math.abs(x) < EPSILON ? 0 : x;
});
var radians = Math.atan(diff.y / diff.x);
if (diff.x < 0) {
radians += Math.PI;
Expand Down