Skip to content

Commit

Permalink
Merge pull request #2627 from asturur/clean-polygon
Browse files Browse the repository at this point in the history
Clean polygon class
  • Loading branch information
kangax committed Dec 27, 2015
2 parents 8ffa6a1 + 1c9932f commit 7235f36
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions src/shapes/polygon.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
if (!('left' in options)) {
this.left = this.minX;
}
this.pathOffset = {
x: this.minX + this.width / 2,
y: this.minY + this.height / 2
};
},

/**
Expand All @@ -86,18 +90,6 @@
this.minY = minY || 0;
},

/**
* @private
*/
_applyPointOffset: function() {
// change points to offset polygon into a bounding box
// executed one time
this.points.forEach(function(p) {
p.x -= (this.minX + this.width / 2);
p.y -= (this.minY + this.height / 2);
}, this);
},

/**
* Returns object representation of an instance
* @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output
Expand All @@ -116,18 +108,20 @@
* @return {String} svg representation of an instance
*/
toSVG: function(reviver) {
var points = [],
var points = [], addTransform,
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) + ') ';
}
markup.push(
'<', this.type, ' ',
'points="', points.join(''),
'" style="', this.getSvgStyles(),
'" transform="', this.getSvgTransform(),
'" transform="', this.getSvgTransform(), addTransform,
' ', this.getSvgTransformMatrix(),
'"/>\n'
);
Expand All @@ -140,8 +134,8 @@
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_render: function(ctx) {
if (!this.commonRender(ctx)) {
_render: function(ctx, noTransform) {
if (!this.commonRender(ctx, noTransform)) {
return;
}
this._renderFill(ctx);
Expand All @@ -155,7 +149,7 @@
* @private
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
commonRender: function(ctx) {
commonRender: function(ctx, noTransform) {
var point, len = this.points.length;

if (!len || isNaN(this.points[len - 1].y)) {
Expand All @@ -164,15 +158,8 @@
return false;
}

noTransform || ctx.translate(-this.pathOffset.x, -this.pathOffset.y);
ctx.beginPath();

if (this._applyPointOffset) {
if (!(this.group && this.group.type === 'path-group')) {
this._applyPointOffset();
}
this._applyPointOffset = null;
}

ctx.moveTo(this.points[0].x, this.points[0].y);
for (var i = 0; i < len; i++) {
point = this.points[i];
Expand Down

0 comments on commit 7235f36

Please sign in to comment.