2012-09-13 20:45:27 -04:00
|
|
|
/**
|
|
|
|
* Imports svg into items with groups
|
|
|
|
* Stetson Alpha - Paper.js
|
2012-09-15 19:58:44 -04:00
|
|
|
*
|
2012-09-13 20:45:27 -04:00
|
|
|
*/
|
|
|
|
|
2012-09-15 22:25:19 -04:00
|
|
|
var ImportSVG = this.ImportSVG = Base.extend({
|
2012-09-15 23:58:39 -04:00
|
|
|
/**
|
|
|
|
* Takes the svg dom obj and parses the data
|
|
|
|
* to create a layer with groups (if needed) with
|
|
|
|
* items inside. Should support nested groups.
|
|
|
|
*
|
|
|
|
* takes in a svg object (xml dom)
|
|
|
|
* returns Paper.js Layer
|
|
|
|
*/
|
|
|
|
importSVG: function(svg)
|
|
|
|
{
|
2012-09-16 00:10:14 -04:00
|
|
|
var layer = new Layer();
|
|
|
|
groups = this.importGroup(svg);
|
|
|
|
layer.addChild(groups);
|
|
|
|
|
|
|
|
return layer;
|
2012-09-15 23:58:39 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Paper.js Group by parsing
|
|
|
|
* a specific svg g node
|
|
|
|
*
|
|
|
|
* takes in a svg object (xml dom)
|
|
|
|
* returns Paper.js Group
|
|
|
|
*/
|
|
|
|
importGroup: function(svg)
|
|
|
|
{
|
2012-09-16 00:10:14 -04:00
|
|
|
var group = new Group();
|
|
|
|
var child;
|
|
|
|
for (var i in svg.childNodes) {
|
|
|
|
child = svg.childNodes[i];
|
|
|
|
if (child.nodeType != 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
item = this.importPath(child);
|
|
|
|
group.addChild(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return group;
|
2012-09-15 23:58:39 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Paper.js Path by parsing
|
|
|
|
* a specific svg node (rect, path, circle, polygon, etc)
|
|
|
|
* and creating the right path object based on the svg type.
|
|
|
|
*
|
|
|
|
* takes in a svg object (xml dom)
|
2012-09-16 00:10:14 -04:00
|
|
|
* returns Paper.js Item
|
2012-09-15 23:58:39 -04:00
|
|
|
*/
|
|
|
|
importPath: function(svg)
|
|
|
|
{
|
2012-09-16 00:10:14 -04:00
|
|
|
switch (svg.nodeName.toLowerCase()) {
|
|
|
|
case 'line':
|
|
|
|
item = this.importLine(svg);
|
|
|
|
break;
|
|
|
|
case 'rect':
|
|
|
|
item = this.importRectangle(svg);
|
|
|
|
break;
|
|
|
|
case 'ellipse':
|
|
|
|
item = this.importOval(svg);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
item = this.importGroup(svg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
2012-09-15 23:58:39 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Path.Circle Paper.js item
|
|
|
|
*
|
|
|
|
* takes a svg circle node (xml dom)
|
|
|
|
* returns Paper.js Path.Circle item
|
|
|
|
*/
|
2012-09-16 00:09:39 -04:00
|
|
|
importCircle: function(svgCircle)
|
2012-09-15 23:58:39 -04:00
|
|
|
{
|
|
|
|
var cx = svgCircle.cx.baseVal.value || 0;
|
|
|
|
var cy = svgCircle.cy.baseVal.value || 0;
|
|
|
|
var r = svgCircle.r.baseVal.value || 0;
|
|
|
|
var center = new Point(cx, cy);
|
|
|
|
var circle = new Path.Circle(center, r);
|
|
|
|
|
|
|
|
return circle;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Path.Oval Paper.js item
|
|
|
|
*
|
|
|
|
* takes a svg ellipse node (xml dom)
|
|
|
|
* returns Paper.js Path.Oval item
|
|
|
|
*/
|
2012-09-16 00:09:39 -04:00
|
|
|
importOval: function(svgOval)
|
2012-09-15 23:58:39 -04:00
|
|
|
{
|
|
|
|
var cx = svgOval.cx.baseVal.value || 0;
|
|
|
|
var cy = svgOval.cy.baseVal.value || 0;
|
|
|
|
var rx = svgOval.rx.baseVal.value || 0;
|
|
|
|
var ry = svgOval.ry.baseVal.value || 0;
|
|
|
|
|
|
|
|
var center = new Point(cx, cy);
|
|
|
|
var offset = new Point(rx, ry);
|
|
|
|
var topLeft = center.subtract(offset);
|
|
|
|
var bottomRight = center.add(offset);
|
|
|
|
|
|
|
|
var rect = new Rectangle(topLeft, bottomRight);
|
|
|
|
var oval = new Path.Oval(rect);
|
|
|
|
|
|
|
|
return oval;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a "rectangle" Paper.js item
|
|
|
|
*
|
|
|
|
* takes a svg rect node (xml dom)
|
|
|
|
* returns either a
|
|
|
|
* - Path.Rectangle item
|
|
|
|
* - Path.RoundRectangle item (if the rectangle has rounded corners)
|
|
|
|
*/
|
2012-09-16 00:09:39 -04:00
|
|
|
importRectangle: function(svgRectangle)
|
2012-09-15 23:58:39 -04:00
|
|
|
{
|
|
|
|
var x = svgRectangle.x.baseVal.value || 0;
|
|
|
|
var y = svgRectangle.y.baseVal.value || 0;
|
|
|
|
var rx = svgRectangle.rx.baseVal.value || 0;
|
|
|
|
var ry = svgRectangle.ry.baseVal.value || 0;
|
|
|
|
var width = svgRectangle.width.baseVal.value || 0;
|
|
|
|
var height = svgRectangle.height.baseVal.value || 0;
|
|
|
|
|
|
|
|
var topLeft = new Point(x, y);
|
|
|
|
var size = new Size(width, height);
|
|
|
|
var rectangle = new Rectangle(topLeft, size);
|
|
|
|
|
|
|
|
if (rx > 0 || ry > 0) {
|
|
|
|
var cornerSize = new Size(rx, ry);
|
|
|
|
rectangle = new Path.RoundRectangle(rectangle, cornerSize);
|
|
|
|
} else {
|
|
|
|
rectangle = new Path.Rectangle(rectangle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rectangle;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Path.Line Paper.js item
|
|
|
|
*
|
|
|
|
* takes a svg line node (xml dom)
|
|
|
|
* returns a Path.Line item
|
|
|
|
*/
|
2012-09-16 00:09:39 -04:00
|
|
|
importLine: function(svgLine)
|
2012-09-15 23:58:39 -04:00
|
|
|
{
|
|
|
|
var x1 = svgLine.x1.baseVal.value || 0;
|
|
|
|
var y1 = svgLine.y1.baseVal.value || 0;
|
|
|
|
var x2 = svgLine.x2.baseVal.value || 0;
|
|
|
|
var y2 = svgLine.y2.baseVal.value || 0;
|
|
|
|
|
|
|
|
var from = new Point(x1, y1);
|
|
|
|
var to = new Point(x2, y2);
|
|
|
|
var line = new Path.Line(from, to);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
2012-09-15 22:25:19 -04:00
|
|
|
});
|