Skip to content

Commit

Permalink
Paint order svg for CIRCLE, IMAGE and TEXT (fabricjs#4320)
Browse files Browse the repository at this point in the history
* added were missing
* added for image too
  • Loading branch information
asturur authored Sep 17, 2017
1 parent 0fc7134 commit ad6521a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/mixins/itext.svg_export.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
(this.fontStyle ? 'font-style="' + this.fontStyle + '" ' : ''),
(this.fontWeight ? 'font-weight="' + this.fontWeight + '" ' : ''),
(this.textDecoration ? 'text-decoration="' + this.textDecoration + '" ' : ''),
'style="', this.getSvgStyles(noShadow), '" >\n',
'style="', this.getSvgStyles(noShadow), '"', this.addPaintOrder(), ' >\n',
textAndBg.textSpans.join(''),
'\t\t</text>\n',
'\t</g>\n'
Expand Down
8 changes: 5 additions & 3 deletions src/shapes/circle.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@
'r="', this.radius,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
'"/>\n'
' ', this.getSvgTransformMatrix(), '"',
this.addPaintOrder(),
'/>\n'
);
}
else {
Expand All @@ -123,7 +124,8 @@
' 0 ', +largeFlag + ' 1', ' ' + endX + ' ' + endY,
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
' ', this.getSvgTransformMatrix(),
' ', this.getSvgTransformMatrix(), '"',
this.addPaintOrder(),
'"/>\n'
);
}
Expand Down
28 changes: 16 additions & 12 deletions src/shapes/image.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,34 @@
*/
toSVG: function(reviver) {
var markup = this._createBaseSVGMarkup(), x = -this.width / 2, y = -this.height / 2;
markup.push(
'<g transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '">\n',
'\t<image ', this.getSvgId(), 'xlink:href="', this.getSvgSrc(true),
markup.push('<g transform="', this.getSvgTransform(), this.getSvgTransformMatrix(), '">\n');
var imageMarkup = ['\t<image ', this.getSvgId(), 'xlink:href="', this.getSvgSrc(true),
'" x="', x, '" y="', y,
'" style="', this.getSvgStyles(),
// we're essentially moving origin of transformation from top/left corner to the center of the shape
// by wrapping it in container <g> element with actual transformation, then offsetting object to the top/left
// so that object's center aligns with container's left/top
'" width="', this.width,
'" height="', this.height,
'"></image>\n'
);

'"></image>\n'];
if (this.paintFirst === 'fill') {
Array.prototype.push.apply(markup, imageMarkup);
}
if (this.stroke || this.strokeDashArray) {
var origFill = this.fill;
this.fill = null;
markup.push(
'<rect ',
'\t<rect ',
'x="', x, '" y="', y,
'" width="', this.width, '" height="', this.height,
'" style="', this.getSvgStyles(),
'"/>\n'
);
this.fill = origFill;
}

if (this.paintFirst !== 'fill') {
Array.prototype.push.apply(markup, imageMarkup);
}
markup.push('</g>\n');

return reviver ? reviver(markup.join('')) : markup.join('');
Expand Down Expand Up @@ -451,19 +453,21 @@
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
var x = -this.width / 2, y = -this.height / 2, elementToDraw;

if (this.isMoving === false && this.resizeFilter && this._needsResize()) {
this._lastScaleX = this.scaleX;
this._lastScaleY = this.scaleY;
this.applyResizeFilters();
}
this._stroke(ctx);
this._renderPaintInOrder(ctx);
},

_renderFill: function(ctx) {
var x = -this.width / 2, y = -this.height / 2, elementToDraw;
elementToDraw = this._element;
elementToDraw && ctx.drawImage(elementToDraw,
this.cropX, this.cropY, this.width, this.height,
x, y, this.width, this.height);
this._stroke(ctx);
this._renderStroke(ctx);
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/object.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
* @type Array
*/
cacheProperties: (
'fill stroke strokeWidth strokeDashArray width height' +
'fill stroke strokeWidth strokeDashArray width height paintFirst' +
' strokeLineCap strokeLineJoin strokeMiterLimit backgroundColor'
).split(' '),

Expand Down

0 comments on commit ad6521a

Please sign in to comment.