Skip to content

Commit

Permalink
Add ID to svg export (#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed May 21, 2016
1 parent 489230b commit eb9b7f4
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/mixins/object.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
return this.shadow ? 'filter: url(#SVGID_' + this.shadow.id + ');' : '';
},

/**
* Returns id attribute for svg output
* @return {String}
*/
getSvgId: function() {
return this.id ? 'id="' + this.id + '" ' : '';
},

/**
* Returns transform-string for svg-export
* @return {String}
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/circle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
y = this.top + this.radius;
}
markup.push(
'<circle ',
'<circle ', this.getSvgId(),
'cx="' + x + '" cy="' + y + '" ',
'r="', this.radius,
'" style="', this.getSvgStyles(),
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/ellipse.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
y = this.top + this.ry;
}
markup.push(
'<ellipse ',
'<ellipse ', this.getSvgId(),
'cx="', x, '" cy="', y, '" ',
'rx="', this.rx,
'" ry="', this.ry,
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup();
markup.push(
'<g transform="',
'<g ', this.getSvgId(), 'transform="',
/* avoiding styles intentionally */
this.getSvgTransform(),
this.getSvgTransformMatrix(),
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
}
markup.push(
'<g transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '">\n',
'<image xlink:href="', this.getSvgSrc(),
'<image ', this.getSvgId(), 'xlink:href="', this.getSvgSrc(),
'" x="', x, '" y="', y,
'" style="', this.getSvgStyles(),
// we're essentially moving origin of transformation from top/left corner to the center of the shape
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/line.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
p = this.calcLinePoints();
}
markup.push(
'<line ',
'<line ', this.getSvgId(),
'x1="', p.x1,
'" y1="', p.y1,
'" x2="', p.x2,
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/path.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@
}
markup.push(
//jscs:disable validateIndentation
'<path ',
'<path ', this.getSvgId(),
'd="', path,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(), addTransform,
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/path_group.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
translatePart = 'translate(' + p.x + ' ' + p.y + ')',
markup = this._createBaseSVGMarkup();
markup.push(
'<g ',
'<g ', this.getSvgId(),
'style="', this.getSvgStyles(), '" ',
'transform="', this.getSvgTransformMatrix(), translatePart, this.getSvgTransform(), '" ',
'>\n'
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/polygon.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
addTransform = ' translate(' + (-this.pathOffset.x) + ', ' + (-this.pathOffset.y) + ') ';
}
markup.push(
'<', this.type, ' ',
'<', this.type, ' ', this.getSvgId(),
'points="', points.join(''),
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(), addTransform,
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/rect.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
y = -this.height / 2;
}
markup.push(
'<rect ',
'<rect ', this.getSvgId(),
'x="', x, '" y="', y,
'" rx="', this.get('rx'), '" ry="', this.get('ry'),
'" width="', this.width, '" height="', this.height,
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/text.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@
style = filter === '' ? '' : ' style="' + filter + '"';

markup.push(
'\t<g transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '"',
'\t<g ', this.getSvgId(), 'transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '"',
style, '>\n',
textAndBg.textBgRects.join(''),
'\t\t<text ',
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/triangle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
.join(',');

markup.push(
'<polygon ',
'<polygon ', this.getSvgId(),
'points="', points,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
Expand Down
6 changes: 6 additions & 0 deletions test/unit/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
equal(svg, '<rect x="-50" y="-50" rx="0" ry="0" width="100" height="100" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-opacity: 0.5; fill-rule: nonzero; opacity: 1;" transform="translate(50 50)"/>\n');
});

test('toSVG with id', function() {
var rect = new fabric.Rect({id: 'myRect', width: 100, height: 100, strokeWidth: 0, fill: 'rgba(255, 0, 0, 0.5)' });
var svg = rect.toSVG();
equal(svg, '<rect id="myRect" x="-50" y="-50" rx="0" ry="0" width="100" height="100" style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(255,0,0); fill-opacity: 0.5; fill-rule: nonzero; opacity: 1;" transform="translate(50 50)"/>\n');
});

test('toSVG with alpha colors stroke', function() {
var rect = new fabric.Rect({ width: 100, height: 100, strokeWidth: 0, fill: '', stroke: 'rgba(255, 0, 0, 0.5)' });
var svg = rect.toSVG();
Expand Down

0 comments on commit eb9b7f4

Please sign in to comment.