Add ID to svg export (#2993)

This commit is contained in:
Andrea Bogazzi 2016-05-21 15:07:04 +02:00
parent 489230bdf5
commit eb9b7f45b9
13 changed files with 25 additions and 11 deletions

View file

@ -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}

View file

@ -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(),

View file

@ -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,

View file

@ -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(),

View file

@ -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

View file

@ -239,7 +239,7 @@
p = this.calcLinePoints();
}
markup.push(
'<line ',
'<line ', this.getSvgId(),
'x1="', p.x1,
'" y1="', p.y1,
'" x2="', p.x2,

View file

@ -511,7 +511,7 @@
}
markup.push(
//jscs:disable validateIndentation
'<path ',
'<path ', this.getSvgId(),
'd="', path,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(), addTransform,

View file

@ -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'

View file

@ -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,

View file

@ -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,

View file

@ -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 ',

View file

@ -90,7 +90,7 @@
.join(',');
markup.push(
'<polygon ',
'<polygon ', this.getSvgId(),
'points="', points,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),

View file

@ -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();