2013-01-28 21:03:27 -05:00
|
|
|
/*
|
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2012-11-06 16:07:18 -05:00
|
|
|
* http://paperjs.org/
|
|
|
|
*
|
2015-12-27 12:09:25 -05:00
|
|
|
* Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
|
2014-01-03 19:47:16 -05:00
|
|
|
* http://scratchdisk.com/ & http://jonathanpuckey.com/
|
2012-11-06 16:07:18 -05:00
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2016-02-01 06:50:22 -05:00
|
|
|
QUnit.module('SvgExport');
|
2012-09-30 17:51:50 -04:00
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG line', function() {
|
|
|
|
var attrs = {
|
|
|
|
x1: 5,
|
|
|
|
x2: 45,
|
|
|
|
y1: 5,
|
|
|
|
y2: 45
|
|
|
|
};
|
|
|
|
var path = new Path.Line([attrs.x1, attrs.y1], [attrs.x2, attrs.y2]);
|
2015-08-17 08:18:22 -04:00
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('line', attrs));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG rect', function() {
|
|
|
|
var attrs = {
|
|
|
|
x: 25,
|
|
|
|
y: 25,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
};
|
|
|
|
var path = new Path.Rectangle(attrs);
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('rect', attrs));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG round rect', function() {
|
|
|
|
var attrs = {
|
|
|
|
x: 25,
|
|
|
|
y: 25,
|
|
|
|
rx: 50,
|
|
|
|
ry: 50,
|
|
|
|
width: 100,
|
|
|
|
height: 100
|
|
|
|
};
|
|
|
|
var path = new Path.Rectangle(new Rectangle(attrs),
|
|
|
|
new Size(attrs.rx, attrs.ry));
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('rect', attrs));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG ellipse', function() {
|
|
|
|
var attrs = {
|
|
|
|
cx: 300,
|
|
|
|
cy: 80,
|
|
|
|
rx: 100,
|
|
|
|
ry: 50
|
2016-02-11 05:06:09 -05:00
|
|
|
};
|
2015-07-27 05:42:41 -04:00
|
|
|
var path = new Path.Ellipse({
|
|
|
|
center: new Point(attrs.cx, attrs.cy),
|
|
|
|
radius: new Point(attrs.rx, attrs.ry)
|
|
|
|
});
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('ellipse', attrs));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG circle', function() {
|
|
|
|
var attrs = {
|
|
|
|
cx: 100,
|
|
|
|
cy: 80,
|
|
|
|
r: 50
|
2016-02-11 05:06:09 -05:00
|
|
|
};
|
2015-07-27 05:42:41 -04:00
|
|
|
var path = new Path.Circle({
|
|
|
|
center: new Point(attrs.cx, attrs.cy),
|
|
|
|
radius: attrs.r
|
|
|
|
});
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('circle', attrs));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
function createPolyPath(str) {
|
|
|
|
var points = str.split(' ').map(function(point) {
|
|
|
|
return point.split(',').map(parseFloat);
|
|
|
|
});
|
|
|
|
var path = new Path();
|
|
|
|
path.moveTo(points[0]);
|
|
|
|
for (var i = 1; i < points.length; i++)
|
|
|
|
path.lineTo(points[i]);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
test('Export SVG polygon', function() {
|
|
|
|
var points = '100,10 40,180 190,60 10,60 160,180';
|
|
|
|
var path = createPolyPath(points);
|
|
|
|
path.closePath();
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('polygon', {
|
|
|
|
points: points
|
|
|
|
}));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
|
|
|
|
2015-07-27 05:42:41 -04:00
|
|
|
test('Export SVG polyline', function() {
|
|
|
|
var points = '5,5 45,45 5,45 45,5';
|
|
|
|
var path = createPolyPath(points);
|
|
|
|
equals(path.exportSVG({ matchShapes: true }), createSVG('polyline', {
|
|
|
|
points: points
|
|
|
|
}));
|
2012-09-30 17:51:50 -04:00
|
|
|
});
|
2015-07-27 05:42:41 -04:00
|
|
|
|
2015-10-27 18:33:09 -04:00
|
|
|
test('Export SVG path defaults to precision 5', function() {
|
|
|
|
var path = new Path('M0.123456789,1.9l0.8,1.1');
|
|
|
|
equals(path.exportSVG({}).getAttribute('d'), 'M0.12346,1.9l0.8,1.1');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Export SVG path at precision 0', function() {
|
|
|
|
var path = new Path('M0.123456789,1.9l0.8,1.1');
|
|
|
|
equals(path.exportSVG({ precision: 0 }).getAttribute('d'), 'M0,2l1,1');
|
|
|
|
});
|