Remove Rect options contructor.

This commit is contained in:
Antoine Vandecreme 2015-11-30 19:11:41 -05:00
parent 94186826af
commit 2e26ae5ff1
2 changed files with 69 additions and 182 deletions

View File

@ -44,55 +44,39 @@
* degrees increases clockwise with 0 being the horizontal
*
* @memberof OpenSeadragon
* @param {Object} options
* @param {Number} options.x X coordinate of the top left corner of the rectangle.
* @param {Number} options.y Y coordinate of the top left corner of the rectangle.
* @param {Number} options.width Width of the rectangle.
* @param {Number} options.height Height of the rectangle.
* @param {Number} options.degrees Rotation of the rectangle around (x,y) in degrees.
* @param {Number} [x] Deprecated: The vector component 'x'.
* @param {Number} [y] Deprecated: The vector component 'y'.
* @param {Number} [width] Deprecated: The vector component 'width'.
* @param {Number} [height] Deprecated: The vector component 'height'.
* @param {Number} [x=0] The vector component 'x'.
* @param {Number} [y=0] The vector component 'y'.
* @param {Number} [width=0] The vector component 'width'.
* @param {Number} [height=0] The vector component 'height'.
* @param {Number} [degrees=0] Rotation of the rectangle around (x,y) in degrees.
*/
$.Rect = function(x, y, width, height) {
var options = x;
if (!$.isPlainObject(options)) {
options = {
x: x,
y: y,
width: width,
height: height
};
}
$.Rect = function(x, y, width, height, degrees) {
/**
* The vector component 'x'.
* @member {Number} x
* @memberof OpenSeadragon.Rect#
*/
this.x = typeof(options.x) === "number" ? options.x : 0;
this.x = typeof(x) === "number" ? x : 0;
/**
* The vector component 'y'.
* @member {Number} y
* @memberof OpenSeadragon.Rect#
*/
this.y = typeof(options.y) === "number" ? options.y : 0;
this.y = typeof(y) === "number" ? y : 0;
/**
* The vector component 'width'.
* @member {Number} width
* @memberof OpenSeadragon.Rect#
*/
this.width = typeof(options.width) === "number" ? options.width : 0;
this.width = typeof(width) === "number" ? width : 0;
/**
* The vector component 'height'.
* @member {Number} height
* @memberof OpenSeadragon.Rect#
*/
this.height = typeof(options.height) === "number" ? options.height : 0;
this.height = typeof(height) === "number" ? height : 0;
this.degrees = typeof(options.degrees) === "number" ? options.degrees : 0;
this.degrees = typeof(degrees) === "number" ? degrees : 0;
};
$.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
@ -101,13 +85,12 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
* @returns {OpenSeadragon.Rect} a duplicate of this Rect
*/
clone: function() {
return new $.Rect({
x: this.x,
y: this.y,
width: this.width,
height: this.height,
degrees: this.degrees
});
return new $.Rect(
this.x,
this.y,
this.width,
this.height,
this.degrees);
},
/**
@ -216,13 +199,12 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
* of the vector components by the factor
*/
times: function(factor) {
return new $.Rect({
x: this.x * factor,
y: this.y * factor,
width: this.width * factor,
height: this.height * factor,
degrees: this.degrees
});
return new $.Rect(
this.x * factor,
this.y * factor,
this.width * factor,
this.height * factor,
this.degrees);
},
/**
@ -232,13 +214,12 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
* @returns {OpenSeadragon.Rect} A new rect with altered position
*/
translate: function(delta) {
return new $.Rect({
x: this.x + delta.x,
y: this.y + delta.y,
width: this.width,
height: this.height,
degrees: this.degrees
});
return new $.Rect(
this.x + delta.x,
this.y + delta.y,
this.width,
this.height,
this.degrees);
},
/**
@ -256,12 +237,11 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
var right = Math.max(this.x + this.width, rect.x + rect.width);
var bottom = Math.max(this.y + this.height, rect.y + rect.height);
return new $.Rect({
left: left,
top: top,
width: right - left,
height: bottom - top
});
return new $.Rect(
left,
top,
right - left,
bottom - top);
},
/**
@ -289,13 +269,12 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
} else if (diff.y < 0) {
radians += 2 * Math.PI;
}
return new $.Rect({
x: newTopLeft.x,
y: newTopLeft.y,
width: this.width,
height: this.height,
degrees: radians / Math.PI * 180
});
return new $.Rect(
newTopLeft.x,
newTopLeft.y,
this.width,
this.height,
radians / Math.PI * 180);
},
/**
@ -315,12 +294,11 @@ $.Rect.prototype = /** @lends OpenSeadragon.Rect.prototype */{
var maxX = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
var minY = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
var maxY = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
return new $.Rect({
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY
});
return new $.Rect(
minX,
minY,
maxX - minX,
maxY - minY);
},
/**

View File

@ -22,30 +22,15 @@
message + " degrees: ");
}
test('Legacy constructor', function() {
var rect = new OpenSeadragon.Rect(1, 2, 3, 4);
strictEqual(rect.x, 1, 'rect.x should be 1');
strictEqual(rect.y, 2, 'rect.y should be 2');
strictEqual(rect.width, 3, 'rect.width should be 3');
strictEqual(rect.height, 4, 'rect.height should be 4');
strictEqual(rect.degrees, 0, 'rect.degrees should be 0');
});
test('Constructor', function() {
var rect = new OpenSeadragon.Rect({
x: 1,
y: 2,
width: 3,
height: 4,
degrees: 5
});
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5);
strictEqual(rect.x, 1, 'rect.x should be 1');
strictEqual(rect.y, 2, 'rect.y should be 2');
strictEqual(rect.width, 3, 'rect.width should be 3');
strictEqual(rect.height, 4, 'rect.height should be 4');
strictEqual(rect.degrees, 5, 'rect.degrees should be 5');
rect = new OpenSeadragon.Rect({});
rect = new OpenSeadragon.Rect();
strictEqual(rect.x, 0, 'rect.x should be 0');
strictEqual(rect.y, 0, 'rect.y should be 0');
strictEqual(rect.width, 0, 'rect.width should be 0');
@ -54,24 +39,13 @@
});
test('getTopLeft', function() {
var rect = new OpenSeadragon.Rect({
x: 1,
y: 2,
width: 3,
height: 4,
degrees: 5
});
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5);
var expected = new OpenSeadragon.Point(1, 2);
ok(expected.equals(rect.getTopLeft()), "Incorrect top left point.");
});
test('getTopRight', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 1,
height: 3
});
var rect = new OpenSeadragon.Rect(0, 0, 1, 3);
var expected = new OpenSeadragon.Point(1, 0);
ok(expected.equals(rect.getTopRight()), "Incorrect top right point.");
@ -82,12 +56,7 @@
});
test('getBottomLeft', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 3,
height: 1
});
var rect = new OpenSeadragon.Rect(0, 0, 3, 1);
var expected = new OpenSeadragon.Point(0, 1);
ok(expected.equals(rect.getBottomLeft()), "Incorrect bottom left point.");
@ -98,12 +67,7 @@
});
test('getBottomRight', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 1,
height: 1
});
var rect = new OpenSeadragon.Rect(0, 0, 1, 1);
var expected = new OpenSeadragon.Point(1, 1);
ok(expected.equals(rect.getBottomRight()), "Incorrect bottom right point.");
@ -124,12 +88,7 @@
});
test('getCenter', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 1,
height: 1
});
var rect = new OpenSeadragon.Rect(0, 0, 1, 1);
var expected = new OpenSeadragon.Point(0.5, 0.5);
ok(expected.equals(rect.getCenter()), "Incorrect center point.");
@ -150,109 +109,59 @@
});
test('rotate', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 1
});
var rect = new OpenSeadragon.Rect(0, 0, 2, 1);
// Rotate 45deg around center.
var expected = new OpenSeadragon.Rect({
x: 1 - 1 / (2 * Math.sqrt(2)),
y: 0.5 - 3 / (2 * Math.sqrt(2)),
width: 2,
height: 1,
degrees: 45
});
var expected = new OpenSeadragon.Rect(
1 - 1 / (2 * Math.sqrt(2)),
0.5 - 3 / (2 * Math.sqrt(2)),
2,
1,
45);
var actual = rect.rotate(45);
assertRectangleEquals(expected, actual,
"Incorrect rectangle after rotation of 45deg around center.");
expected = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 1,
degrees: 33
});
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 33);
actual = rect.rotate(33, rect.getTopLeft());
assertRectangleEquals(expected, actual,
"Incorrect rectangle after rotation of 33deg around topLeft.");
expected = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 1,
degrees: 101
});
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 101);
actual = rect.rotate(101, rect.getTopLeft());
assertRectangleEquals(expected, actual,
"Incorrect rectangle after rotation of 187deg around topLeft.");
expected = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 1,
degrees: 187
});
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 187);
actual = rect.rotate(187, rect.getTopLeft());
assertRectangleEquals(expected, actual,
"Incorrect rectangle after rotation of 187deg around topLeft.");
expected = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 1,
degrees: 300
});
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 300);
actual = rect.rotate(300, rect.getTopLeft());
assertRectangleEquals(expected, actual,
"Incorrect rectangle after rotation of 300deg around topLeft.");
});
test('getBoundingBox', function() {
var rect = new OpenSeadragon.Rect({
x: 0,
y: 0,
width: 2,
height: 3
});
var rect = new OpenSeadragon.Rect(0, 0, 2, 3);
var bb = rect.getBoundingBox();
ok(rect.equals(bb), "Bounding box of horizontal rectangle should be " +
"identical to rectangle.");
rect.degrees = 90;
var expected = new OpenSeadragon.Rect({
x: -3,
y: 0,
width: 3,
height: 2
});
var expected = new OpenSeadragon.Rect(-3, 0, 3, 2);
assertRectangleEquals(expected, rect.getBoundingBox(),
"Bounding box of rect rotated 90deg.");
rect.degrees = 180;
var expected = new OpenSeadragon.Rect({
x: -2,
y: -3,
width: 2,
height: 3
});
var expected = new OpenSeadragon.Rect(-2, -3, 2, 3);
assertRectangleEquals(expected, rect.getBoundingBox(),
"Bounding box of rect rotated 180deg.");
rect.degrees = 270;
var expected = new OpenSeadragon.Rect({
x: 0,
y: -2,
width: 3,
height: 2
});
var expected = new OpenSeadragon.Rect(0, -2, 3, 2);
assertRectangleEquals(expected, rect.getBoundingBox(),
"Bounding box of rect rotated 270deg.");
});