Fix polygon/polyline tests and minor cleanup

This commit is contained in:
kangax 2013-07-07 22:25:01 +02:00
parent 3076a7b45f
commit 21f87bb63a
7 changed files with 77 additions and 42 deletions

56
dist/all.js vendored
View file

@ -2434,6 +2434,32 @@ fabric.Collection = {
];
}
/**
* Takes an array of points and returns a bounding rectangle around them
* @static
* @memberOf fabric.util
* @param {Array.<Object>} points An array of objects with x and y properties.
* @return {Object} An object with x1, y1, x2, and y2 properties
* corresponding to the points of the rect, and
* width and height properties.
*/
function getBoundingRect(points) {
var utilMin = fabric.util.array.min,
utilMax = fabric.util.array.max;
var rect = {
x1: utilMin(points, 'x'),
y1: utilMin(points, 'y'),
x2: utilMax(points, 'x'),
y2: utilMax(points, 'y')
};
rect.width = rect.x2 - rect.x1;
rect.height = rect.y2 - rect.y1;
return rect;
}
function getFunctionBody(fn) {
return (String(fn).match(/function[^{]*\{([\s\S]*)\}/) || {})[1];
}
@ -2567,6 +2593,7 @@ fabric.Collection = {
fabric.util.createAccessors = createAccessors;
fabric.util.clipContext = clipContext;
fabric.util.multiplyTransformMatrices = multiplyTransformMatrices;
fabric.util.getBoundingRect = getBoundingRect;
fabric.util.getFunctionBody = getFunctionBody;
fabric.util.drawArc = drawArc;
@ -13440,8 +13467,7 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
"use strict";
var fabric = global.fabric || (global.fabric = { }),
toFixed = fabric.util.toFixed,
min = fabric.util.array.min;
toFixed = fabric.util.toFixed;
if (fabric.Polyline) {
fabric.warn('fabric.Polyline is already defined');
@ -13589,16 +13615,15 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES),
minX = min(points, 'x'),
minY = min(points, 'y');
boundingRect = fabric.util.getBoundingRect(points);
minX = minX < 0 ? minX : 0;
minY = minX < 0 ? minY : 0;
options.top = boundingRect.y1 + boundingRect.height / 2;
options.left = boundingRect.x1 + boundingRect.width / 2;
for (var i = 0, len = points.length; i < len; i++) {
// normalize coordinates, according to containing box (dimensions of which are passed via `options`)
points[i].x -= (options.width / 2 + minX) || 0;
points[i].y -= (options.height / 2 + minY) || 0;
// normalize coordinates, according to containing box (dimensions of which are calculated above)
points[i].x -= options.left;
points[i].y -= options.top;
}
return new fabric.Polyline(points, fabric.util.object.extend(parsedAttributes, options), true);
@ -13802,16 +13827,15 @@ fabric.util.object.extend(fabric.Object.prototype, /** @lends fabric.Object.prot
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES),
minX = min(points, 'x'),
minY = min(points, 'y');
boundingRect = fabric.util.getBoundingRect(points);
minX = minX < 0 ? minX : 0;
minY = minX < 0 ? minY : 0;
options.top = boundingRect.y1 + boundingRect.height / 2;
options.left = boundingRect.x1 + boundingRect.width / 2;
for (var i = 0, len = points.length; i < len; i++) {
// normalize coordinates, according to containing box (dimensions of which are passed via `options`)
points[i].x -= (options.width / 2 + minX) || 0;
points[i].y -= (options.height / 2 + minY) || 0;
// normalize coordinates, according to containing box (dimensions of which are calculated above)
points[i].x -= options.left;
points[i].y -= options.top;
}
return new fabric.Polygon(points, extend(parsedAttributes, options), true);

12
dist/all.min.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/all.min.js.gz vendored

Binary file not shown.

View file

@ -180,13 +180,11 @@
options || (options = { });
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES);
parsedAttributes = fabric.parseAttributes(element, fabric.Polygon.ATTRIBUTE_NAMES),
boundingRect = fabric.util.getBoundingRect(points);
var boundingRect = fabric.util.getBoundingRect(points);
var width = boundingRect.width;
var height = boundingRect.height;
options.top = boundingRect.y1 + height / 2;
options.left = boundingRect.x1 + width / 2;
options.top = boundingRect.y1 + boundingRect.height / 2;
options.left = boundingRect.x1 + boundingRect.width / 2;
for (var i = 0, len = points.length; i < len; i++) {
// normalize coordinates, according to containing box (dimensions of which are calculated above)

View file

@ -150,13 +150,11 @@
options || (options = { });
var points = fabric.parsePointsAttribute(element.getAttribute('points')),
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES);
parsedAttributes = fabric.parseAttributes(element, fabric.Polyline.ATTRIBUTE_NAMES),
boundingRect = fabric.util.getBoundingRect(points);
var boundingRect = fabric.util.getBoundingRect(points);
var width = boundingRect.width;
var height = boundingRect.height;
options.top = boundingRect.y1 + height / 2;
options.left = boundingRect.x1 + width / 2;
options.top = boundingRect.y1 + boundingRect.height / 2;
options.left = boundingRect.x1 + boundingRect.width / 2;
for (var i = 0, len = points.length; i < len; i++) {
// normalize coordinates, according to containing box (dimensions of which are calculated above)

View file

@ -91,7 +91,9 @@
var expected = fabric.util.object.extend(
fabric.util.object.clone(REFERENCE_OBJECT), {
points: [ { x: 10, y: 12 }, { x: 20, y: 22 } ]
points: [ { x: -5, y: -5 }, { x: 5, y: 5 } ],
left: 15,
top: 17
});
deepEqual(polygon.toObject(), expected);
@ -110,10 +112,10 @@
var polygonWithAttrs = fabric.Polygon.fromElement(elPolygonWithAttrs);
var expectedPoints = [
{ x: -10, y: -10 },
{ x: 0, y: 0 },
{ x: 10, y: 10 },
{ x: 20, y: 20 },
{ x: 30, y: 30 },
{ x: 10, y: 10 }
{ x: -10, y: -10 }
];
deepEqual(polygonWithAttrs.toObject(), fabric.util.object.extend(REFERENCE_OBJECT, {
@ -127,7 +129,9 @@
'strokeLineJoin': 'bevil',
'strokeMiterLimit': 5,
'opacity': 0.34,
'points': expectedPoints
'points': expectedPoints,
'left': 20,
'top': 20
}));
deepEqual(polygonWithAttrs.get('transformMatrix'), [ 2, 0, 0, 2, -10, -20 ]);

View file

@ -87,7 +87,11 @@
var polyline = fabric.Polyline.fromElement(elPolyline);
ok(polyline instanceof fabric.Polyline);
deepEqual(polyline.toObject(), REFERENCE_OBJECT);
deepEqual(polyline.toObject(), fabric.util.object.extend(REFERENCE_OBJECT, {
points: [ { x: -5, y: -5 }, { x: 5, y: 5 } ],
left: 15,
top: 17
}));
var elPolylineWithAttrs = fabric.document.createElement('polyline');
elPolylineWithAttrs.setAttribute('points', '10,10 20,20 30,30 10,10');
@ -103,7 +107,12 @@
var polylineWithAttrs = fabric.Polyline.fromElement(elPolylineWithAttrs);
var expectedPoints = [{x: 10, y: 10}, {x: 20, y: 20}, {x: 30, y: 30}, {x: 10, y: 10}];
var expectedPoints = [
{ x: -10, y: -10 },
{ x: 0, y: 0 },
{ x: 10, y: 10 },
{ x: -10, y: -10 }
];
deepEqual(polylineWithAttrs.toObject(), fabric.util.object.extend(REFERENCE_OBJECT, {
'width': 20,
@ -116,7 +125,9 @@
'strokeLineJoin': 'bevil',
'strokeMiterLimit': 5,
'opacity': 0.34,
'points': expectedPoints
'points': expectedPoints,
'left': 20,
'top': 20
}));
deepEqual(polylineWithAttrs.get('transformMatrix'), [ 2, 0, 0, 2, -10, -20 ]);