2012-08-10 15:14:56 +00:00
|
|
|
(function(){
|
|
|
|
|
|
|
|
|
|
var REFERENCE_PATH_GROUP_OBJECT = {
|
2013-02-04 19:47:02 +00:00
|
|
|
'type': 'path-group',
|
|
|
|
|
'originX': 'center',
|
|
|
|
|
'originY': 'center',
|
|
|
|
|
'left': 0,
|
|
|
|
|
'top': 0,
|
|
|
|
|
'width': 0,
|
|
|
|
|
'height': 0,
|
|
|
|
|
'fill': '',
|
|
|
|
|
'overlayFill': null,
|
|
|
|
|
'stroke': null,
|
|
|
|
|
'strokeWidth': 1,
|
|
|
|
|
'strokeDashArray': null,
|
2013-05-18 11:01:34 +00:00
|
|
|
'strokeLineCap': 'butt',
|
|
|
|
|
'strokeLineJoin': 'miter',
|
|
|
|
|
'strokeMiterLimit': 10,
|
2013-02-04 19:47:02 +00:00
|
|
|
'scaleX': 1,
|
|
|
|
|
'scaleY': 1,
|
|
|
|
|
'angle': 0,
|
|
|
|
|
'flipX': false,
|
|
|
|
|
'flipY': false,
|
|
|
|
|
'opacity': 1,
|
|
|
|
|
'selectable': true,
|
|
|
|
|
'hasControls': true,
|
|
|
|
|
'hasBorders': true,
|
|
|
|
|
'hasRotatingPoint': true,
|
2012-10-14 00:53:25 +00:00
|
|
|
'transparentCorners': true,
|
|
|
|
|
'perPixelTargetFind': false,
|
2013-02-04 19:47:02 +00:00
|
|
|
'shadow': null,
|
2013-03-06 17:45:18 +00:00
|
|
|
'visible': true,
|
2013-06-02 21:04:54 +00:00
|
|
|
'clipTo': null,
|
2013-02-04 19:47:02 +00:00
|
|
|
'paths': getPathObjects()
|
2012-08-10 15:14:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getPathElement(path) {
|
|
|
|
|
var el = fabric.document.createElement('path');
|
|
|
|
|
el.setAttribute('d', path);
|
|
|
|
|
el.setAttribute('fill', 'rgb(255,0,0)');
|
|
|
|
|
el.setAttribute('stroke', 'blue');
|
Parse SVG stroke-opacity and fill-opacity
- SVG attribute opacity is now used for object's opacity
- fill-opacity and stroke-opacity are added to stroke and fill color value
- Add hsl/hsla support (e.g. hsl(270, 80%, 10%), hsla(320, 10%, 66%, 0.5))
- Add support for rgb/rgba values with whitespaces around values (e.g. rgba( 255 , 100 , 50 , 0.1 )) and percentage values (e.g. rgb(100%, 67%, 15%, 0.8))
- Delete stroke and strokeWidth from fabric.Text (defined in fabric.Object)
- New unit test for parse stroke-opacity and fill-opacity
- Update unit tests (new tests for hsl/hsla and rgb/rgba (whitespaces and percentage values))
- Change equal and deepEqual parameter order (e.g. equal(actualValue, expectedValue, message))
- Doc additions
2013-05-25 09:03:09 +00:00
|
|
|
el.setAttribute('stroke-width', 3);
|
2012-08-10 15:14:56 +00:00
|
|
|
return el;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
function getPathObject(path, callback) {
|
|
|
|
|
fabric.Path.fromElement(getPathElement(path), callback);
|
2012-08-10 15:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
function getPathObjects(callback) {
|
|
|
|
|
function onLoaded() {
|
|
|
|
|
if (++numLoadedObjects === numTotalObjects) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(objects);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var objects = [ ],
|
|
|
|
|
paths = ["M 100 100 L 300 100 L 200 300 z", "M 200 200 L 100 200 L 400 50 z"],
|
|
|
|
|
numLoadedObjects = 0,
|
|
|
|
|
numTotalObjects = paths.length;
|
|
|
|
|
|
|
|
|
|
paths.forEach(function (o, index) {
|
|
|
|
|
getPathObject(o, function(o) {
|
|
|
|
|
objects[index] = o;
|
|
|
|
|
onLoaded();
|
|
|
|
|
});
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
function getPathGroupObject(callback) {
|
|
|
|
|
getPathObjects(function(objects) {
|
|
|
|
|
callback(new fabric.PathGroup(objects));
|
|
|
|
|
})
|
2012-08-10 15:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QUnit.module('fabric.PathGroup');
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('constructor', function() {
|
2012-08-10 15:14:56 +00:00
|
|
|
ok(fabric.PathGroup);
|
2013-07-18 20:21:19 +00:00
|
|
|
getPathGroupObject(function(pathGroup) {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
ok(pathGroup instanceof fabric.PathGroup);
|
|
|
|
|
ok(pathGroup instanceof fabric.Object);
|
|
|
|
|
//this.assertHasMixin(Enumerable, pathGroup);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
equal(pathGroup.get('type'), 'path-group');
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('getObjects', function() {
|
|
|
|
|
getPathObjects(function(paths) {
|
|
|
|
|
var pathGroup = new fabric.PathGroup(paths);
|
|
|
|
|
ok(typeof pathGroup.getObjects == 'function');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
// nulling group to avoid circular reference (qUnit goes into inifinite loop)
|
|
|
|
|
paths[0].group = null;
|
|
|
|
|
paths[1].group = null;
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
deepEqual(pathGroup.getObjects(), paths);
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('toObject', function() {
|
|
|
|
|
getPathGroupObject(function(pathGroup) {
|
|
|
|
|
ok(typeof pathGroup.toObject == 'function');
|
|
|
|
|
var object = pathGroup.toObject();
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('complexity', function() {
|
2012-08-10 15:14:56 +00:00
|
|
|
function sum(objects) {
|
|
|
|
|
var i = objects.length, total = 0;
|
|
|
|
|
while (i--) {
|
|
|
|
|
total += objects[i];
|
|
|
|
|
}
|
|
|
|
|
return total;
|
|
|
|
|
}
|
2013-07-18 20:21:19 +00:00
|
|
|
getPathGroupObject(function(pathGroup) {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
ok(typeof pathGroup.complexity == 'function');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
var objectsTotalComplexity = pathGroup.getObjects().reduce(function(total, current) {
|
|
|
|
|
total += current.complexity();
|
|
|
|
|
return total;
|
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
|
|
equal(pathGroup.complexity(), objectsTotalComplexity);
|
|
|
|
|
start();
|
|
|
|
|
});
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('toDatalessObject', function() {
|
|
|
|
|
getPathGroupObject(function(pathGroup) {
|
|
|
|
|
ok(typeof pathGroup.toDatalessObject == 'function');
|
|
|
|
|
|
|
|
|
|
pathGroup.setSourcePath('http://example.com/');
|
|
|
|
|
var expectedObject = fabric.util.object.extend(fabric.util.object.clone(REFERENCE_PATH_GROUP_OBJECT), {
|
|
|
|
|
'paths': 'http://example.com/',
|
|
|
|
|
'sourcePath': 'http://example.com/'
|
|
|
|
|
});
|
|
|
|
|
deepEqual(pathGroup.toDatalessObject(), expectedObject);
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('fromObject', function() {
|
|
|
|
|
getPathGroupObject(function(pathGroup) {
|
|
|
|
|
|
|
|
|
|
ok(typeof fabric.PathGroup.fromObject == 'function');
|
|
|
|
|
var pathGroupObject = pathGroup.toObject();
|
|
|
|
|
|
|
|
|
|
fabric.PathGroup.fromObject(pathGroupObject, function(newPathGroupFromObject) {
|
|
|
|
|
|
|
|
|
|
var objectFromOldPathGroup = pathGroup.toObject();
|
|
|
|
|
var objectFromNewPathGroup = newPathGroupFromObject.toObject();
|
|
|
|
|
|
|
|
|
|
ok(newPathGroupFromObject instanceof fabric.PathGroup);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
deepEqual(objectFromNewPathGroup, objectFromOldPathGroup);
|
|
|
|
|
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('toString', function() {
|
|
|
|
|
getPathGroupObject(function(pathGroup) {
|
|
|
|
|
ok(typeof pathGroup.toString == 'function');
|
|
|
|
|
equal(pathGroup.toString(), '#<fabric.PathGroup (8): { top: 0, left: 0 }>');
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('isSameColor', function() {
|
|
|
|
|
getPathGroupObject(function(pathGroup) {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
ok(typeof pathGroup.isSameColor == 'function');
|
|
|
|
|
equal(pathGroup.isSameColor(), true);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.getObjects()[0].set('fill', 'black');
|
|
|
|
|
equal(pathGroup.isSameColor(), false);
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('set', function() {
|
2012-08-10 15:14:56 +00:00
|
|
|
var fillValue = 'rgb(100,200,100)';
|
2013-07-18 20:21:19 +00:00
|
|
|
getPathGroupObject(function(pathGroup) {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.getObjects()[0].group = null;
|
|
|
|
|
pathGroup.getObjects()[1].group = null;
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
ok(typeof pathGroup.set == 'function');
|
|
|
|
|
equal(pathGroup.set('fill', fillValue), pathGroup, 'should be chainable');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.getObjects().forEach(function(path) {
|
|
|
|
|
equal(path.get('fill'), fillValue);
|
|
|
|
|
}, this);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
equal(pathGroup.get('fill'), fillValue);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
// set different color to one of the paths
|
|
|
|
|
pathGroup.getObjects()[1].set('fill', 'black');
|
|
|
|
|
pathGroup.set('fill', 'rgb(255,255,255)');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
equal(pathGroup.getObjects()[0].get('fill'), 'rgb(100,200,100)',
|
|
|
|
|
'when paths are of different fill, setting fill of a group should not change them');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.getObjects()[1].set('fill', 'red');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.set('left', 1234);
|
|
|
|
|
ok(pathGroup.getObjects()[0].get('left') !== 1234);
|
|
|
|
|
equal(pathGroup.get('left'), 1234);
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
|
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
asyncTest('grayscale', function() {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
getPathGroupObject(function(pathGroup) {
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.getObjects()[0].group = null;
|
|
|
|
|
pathGroup.getObjects()[1].group = null;
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
ok(typeof pathGroup.toGrayscale == 'function');
|
|
|
|
|
equal(pathGroup.toGrayscale(), pathGroup, 'should be chainable');
|
|
|
|
|
var firstObject = pathGroup.getObjects()[0],
|
|
|
|
|
secondObject = pathGroup.getObjects()[1];
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
firstObject.set('overlayFill', null);
|
|
|
|
|
secondObject.set('overlayFill', null);
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
firstObject.set('fill', 'rgb(200,0,0)');
|
|
|
|
|
secondObject.set('fill', '0000FF');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
pathGroup.toGrayscale();
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
equal(firstObject.get('overlayFill'), 'rgb(60,60,60)');
|
|
|
|
|
equal(secondObject.get('overlayFill'), 'rgb(28,28,28)');
|
2012-08-10 15:14:56 +00:00
|
|
|
|
2013-07-18 20:21:19 +00:00
|
|
|
equal(firstObject.get('fill'), 'rgb(200,0,0)', 'toGrayscale should not change original fill value');
|
|
|
|
|
equal(new fabric.Color(secondObject.get('fill')).toRgb(), 'rgb(0,0,255)', 'toGrayscale should not change original fill value');
|
|
|
|
|
start();
|
|
|
|
|
});
|
2012-08-10 15:14:56 +00:00
|
|
|
});
|
2013-06-02 21:04:54 +00:00
|
|
|
})();
|