Further simplify SvgExporter code.

This commit is contained in:
Jürg Lehni 2012-11-05 19:56:49 -08:00
parent 37f87a3c03
commit d3d74fb513

View file

@ -119,11 +119,11 @@ var SvgExporter = this.SvgExporter = new function() {
case 'polyline': case 'polyline':
case 'polygon': case 'polygon':
svg = createElement(type); svg = createElement(type);
var pointString = ''; var parts = [];
for(i = 0; i < segments.length; i++) { for(i = 0; i < segments.length; i++) {
pointString += segments[i]._point._x + ',' + segments[i]._point._y + ' '; parts.push(segments[i]._point._x + ',' + segments[i]._point._y);
} }
svg.setAttribute('points', pointString); svg.setAttribute('points', parts.join(' '));
break; break;
case 'text': case 'text':
svg = createElement('text'); svg = createElement('text');
@ -210,45 +210,39 @@ var SvgExporter = this.SvgExporter = new function() {
function pathSetup(path, segments) { function pathSetup(path, segments) {
var svgPath = createElement('path'); var svgPath = createElement('path');
var pointString = ''; var parts = [];
// pointstring is formatted in the way the SVG XML will be reading. // pointstring is formatted in the way the SVG XML will be reading.
// Namely, a point and the way to traverse to that point. // Namely, a point and the way to traverse to that point.
pointString += 'M' + segments[0]._point._x + ',' + segments[0]._point._y + ' '; parts.push('M' + segments[0]._point._x + ',' + segments[0]._point._y);
//Checks 2 points and the angles in between the 2 points //Checks 2 points and the angles in between the 2 points
for (i = 0; i < segments.length-1; i++) { function drawCurve(seg1, seg2) {
var x1 = segments[i]._point._x, var x1 = seg1._point._x,
y1 = segments[i]._point._y, y1 = seg1._point._y,
x2 = segments[i + 1]._point._x, x2 = seg2._point._x,
y2 = segments[i + 1]._point._y, y2 = seg2._point._y,
handleOut1 = segments[i]._handleOut, handleOut1 = seg1._handleOut,
handleIn2 = segments[i+1]._handleIn; handleIn2 = seg2._handleIn;
if (handleOut1.isZero() && handleIn2.isZero()) { if (handleOut1.isZero() && handleIn2.isZero()) {
// L is lineto, moving to a point with drawing // L is lineto, moving to a point with drawing
pointString+= 'L' + x2 + ',' + y2 + ' '; parts.push('L' + x2 + ',' + y2 + ' ');
} else { } else {
// c is curveto, relative: handleOut, handleIn - endpoint, endpoint - startpoint // c is curveto, relative: handleOut, handleIn - end, end - start
pointString += 'c' + (handleOut1._x) + ',' + (handleOut1._y) + ' '; parts.push('c' + handleOut1._x + ',' + handleOut1._y,
pointString += (x2 - x1 + handleIn2._x) + ',' + (y2 - y1 + handleIn2._y) + ' '; (x2 - x1 + handleIn2._x) + ',' + (y2 - y1 + handleIn2._y),
pointString += (x2 - x1) + ',' + (y2 - y1) + ' '; (x2 - x1) + ',' + (y2 - y1));
} }
} }
if (!segments[segments.length - 1]._handleOut.equals([0, 0]) && !segments[0]._handleIn.equals([0, 0])) { for (i = 0; i < segments.length - 1; i++)
var handleOut1 = segments[segments.length - 1]._handleOut, drawCurve(segments[i], segments[i + 1]);
handleIn2 = segments[0]._handleIn, var first = segments[0],
// Bezier curve from last point to first last = segments[segments.length - 1];
x1 = segments[segments.length - 1]._point._x, if (!first._handleOut.isZero() && !last._handleIn.isZero())
y1 = segments[segments.length - 1]._point._y, drawCurve(last, first);
x2 = segments[0]._point._x,
y2 = segments[0]._point._y;
pointString += 'c' + (handleOut1._x) + ',' + (handleOut1._y) + ' ';
pointString += (x2 - x1 + handleIn2._x) + ',' + (y2 - y1 + handleIn2._y) + ' ';
pointString += (x2 - x1) + ',' + (y2 - y1) + ' ';
}
if (path._closed) { if (path._closed) {
// z implies a closed path, connecting the first and last points // z implies a closed path, connecting the first and last points
pointString += 'z'; parts.push('z');
} }
svgPath.setAttribute('d', pointString); svgPath.setAttribute('d', parts.join(' '));
return svgPath; return svgPath;
} }