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

View File

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