Simplify exportPath().

This commit is contained in:
Jürg Lehni 2012-11-05 21:19:53 -08:00
parent ab014c3328
commit ce5ee2dae8

View file

@ -71,18 +71,27 @@ var SvgExporter = this.SvgExporter = new function() {
function exportPath(path) { function exportPath(path) {
var segments = path._segments, var segments = path._segments,
type = determineType(path, segments); type = determineType(path, segments);
// If the object is a circle, ellipse, rectangle, or rounded rectangle,
// see if they are placed at an angle.
if (/^(circle|ellipse|rect|roundrect)$/.test(type)) {
var angle = determineIfTransformed(path, segments, type) + 90;
if (angle !== 0) {
// TODO: Need to implement exported transforms for circle, ellipse,
// and rectangles instead of making them paths
return pathSetup(path, segments);
}
}
switch (type) { switch (type) {
case 'rect': case 'rect':
var width = getDistance(segments, 0, 3), var width = getDistance(segments, 0, 3),
height = getDistance(segments, 0, 1), height = getDistance(segments, 0, 1),
point = path.getBounds().getTopLeft(); point = path.getBounds().getTopLeft();
svg = createElement('rect', { return createElement('rect', {
x: point._x, x: point._x,
y: point._y, y: point._y,
width: width, width: width,
height: height height: height
}); });
break;
case 'roundrect': case 'roundrect':
// d-variables and point are used to determine the rounded corners // d-variables and point are used to determine the rounded corners
// for the rounded rectangle // for the rounded rectangle
@ -96,7 +105,7 @@ var SvgExporter = this.SvgExporter = new function() {
point = new Point((segments[3]._point._x - dx3), (segments[2]._point._y - dy3)), point = new Point((segments[3]._point._x - dx3), (segments[2]._point._y - dy3)),
rx = segments[3]._point._x - point.x, rx = segments[3]._point._x - point.x,
ry = segments[2]._point._y - point.y; ry = segments[2]._point._y - point.y;
svg = createElement('rect', { return createElement('rect', {
x: point._x, x: point._x,
y: point._y, y: point._y,
width: width, width: width,
@ -104,37 +113,33 @@ var SvgExporter = this.SvgExporter = new function() {
rx: rx, rx: rx,
ry: ry ry: ry
}); });
break;
case'line': case'line':
var first = segments[0]._point, var first = segments[0]._point,
last = segments[segments.length - 1]._point; last = segments[segments.length - 1]._point;
svg = createElement('line', { return createElement('line', {
x1: first._x, x1: first._x,
y1: first._y, y1: first._y,
x2: last._x, x2: last._x,
y2: last._y y2: last._y
}); });
break;
case 'circle': case 'circle':
var radius = getDistance(segments, 0, 2) / 2, var radius = getDistance(segments, 0, 2) / 2,
center = path.getPosition(); center = path.getPosition();
svg = createElement('circle', { return createElement('circle', {
cx: center._x, cx: center._x,
cy: center._y, cy: center._y,
r: radius r: radius
}); });
break;
case 'ellipse': case 'ellipse':
var radiusX = getDistance(segments, 2, 0) / 2, var rx = getDistance(segments, 2, 0) / 2,
radiusY = getDistance(segments, 3, 1) / 2, ry = getDistance(segments, 3, 1) / 2,
center = path.getPosition(); center = path.getPosition();
svg = createElement('ellipse', { return createElement('ellipse', {
cx: center._x, cx: center._x,
cy: center._y, cy: center._y,
rx: radiusX, rx: rx,
ry: radiusY ry: ry
}); });
break;
case 'polyline': case 'polyline':
case 'polygon': case 'polygon':
var parts = []; var parts = [];
@ -142,25 +147,11 @@ var SvgExporter = this.SvgExporter = new function() {
var point = segments[i]._point; var point = segments[i]._point;
parts.push(point._x + ',' + point._y); parts.push(point._x + ',' + point._y);
} }
svg = createElement(type, { return createElement(type, {
points: parts.join(' ') points: parts.join(' ')
}); });
break;
default:
svg = pathSetup(path, segments);
break;
} }
// If the object is a circle, ellipse, rectangle, or rounded rectangle, return pathSetup(path, segments);
// ind the angle
if (/^(circle|ellipse|rect|roundrect)$/.test(type)) {
var angle = determineIfTransformed(path, segments, type) + 90;
if (angle !== 0) {
// TODO: Need to implement exported transforms for circle, ellipse,
// and rectangles instead of making them paths
svg = pathSetup(path, segments);
}
}
return svg;
} }
function pathSetup(path, segments) { function pathSetup(path, segments) {