Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix toSVG polygons and other bugs #3866

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/gradient.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
}
/* _FROM_SVG_END_ */

var clone = fabric.util.object.clone;

/**
* Gradient class
* @class fabric.Gradient
Expand Down Expand Up @@ -169,11 +171,11 @@
* @return {String} SVG representation of an gradient (linear/radial)
*/
toSVG: function(object) {
var coords = fabric.util.object.clone(this.coords),
markup, commonAttributes, colorStops = this.colorStops,
var coords = clone(this.coords, true),
markup, commonAttributes, colorStops = clone(this.colorStops, true),
needsSwap = coords.r1 > coords.r2;
// colorStops must be sorted ascending
this.colorStops.sort(function(a, b) {
colorStops.sort(function(a, b) {
return a.offset - b.offset;
});

Expand Down Expand Up @@ -221,7 +223,8 @@
if (this.type === 'radial') {
if (needsSwap) {
// svg goes from internal to external radius. if radius are inverted, swap color stops.
colorStops = colorStops.concat().reverse();
colorStops = colorStops.concat();
colorStops.reverse();
for (var i = 0; i < colorStops.length; i++) {
colorStops[i].offset = 1 - colorStops[i].offset;
}
Expand Down
20 changes: 13 additions & 7 deletions src/shapes/polyline.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
extend = fabric.util.object.extend,
min = fabric.util.array.min,
max = fabric.util.array.max,
toFixed = fabric.util.toFixed;
toFixed = fabric.util.toFixed,
NUM_FRACTION_DIGITS = fabric.Object.NUM_FRACTION_DIGITS;

if (fabric.Polyline) {
fabric.warn('fabric.Polyline is already defined');
Expand Down Expand Up @@ -125,20 +126,25 @@
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var points = [], addTransform,
var points = [], diffX, diffY,
markup = this._createBaseSVGMarkup();

for (var i = 0, len = this.points.length; i < len; i++) {
points.push(toFixed(this.points[i].x, 2), ',', toFixed(this.points[i].y, 2), ' ');
}
if (!(this.group && this.group.type === 'path-group')) {
addTransform = ' translate(' + (-this.pathOffset.x) + ', ' + (-this.pathOffset.y) + ') ';
diffX = this.pathOffset.x;
diffY = this.pathOffset.y;
}

for (var i = 0, len = this.points.length; i < len; i++) {
points.push(
toFixed(this.points[i].x - diffX, NUM_FRACTION_DIGITS), ',',
toFixed(this.points[i].y - diffY, NUM_FRACTION_DIGITS), ' '
);
}
markup.push(
'<', this.type, ' ', this.getSvgId(),
'points="', points.join(''),
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(), addTransform,
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
Expand Down