mirror of
https://github.com/Hopiu/fabric.js.git
synced 2026-05-21 20:01:52 +00:00
Merge pull request #1664 from asturur/Polygon-and-Polylines-code-reduction
Polygon and polylines code reduction
This commit is contained in:
commit
1ebe46be41
2 changed files with 18 additions and 49 deletions
|
|
@ -124,7 +124,7 @@
|
|||
}
|
||||
|
||||
markup.push(
|
||||
'<polygon ',
|
||||
'<', this.type, ' ',
|
||||
'points="', points.join(''),
|
||||
'" style="', this.getSvgStyles(),
|
||||
'" transform="', this.getSvgTransform(),
|
||||
|
|
@ -141,6 +141,19 @@
|
|||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
_render: function(ctx) {
|
||||
this.commonRender(ctx);
|
||||
this._renderFill(ctx);
|
||||
if (this.stroke || this.strokeDashArray) {
|
||||
ctx.closePath();
|
||||
this._renderStroke(ctx);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
commonRender: function(ctx) {
|
||||
var point;
|
||||
ctx.beginPath();
|
||||
|
||||
|
|
@ -156,11 +169,6 @@
|
|||
point = this.points[i];
|
||||
ctx.lineTo(point.x, point.y);
|
||||
}
|
||||
this._renderFill(ctx);
|
||||
if (this.stroke || this.strokeDashArray) {
|
||||
ctx.closePath();
|
||||
this._renderStroke(ctx);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
@ -168,14 +176,7 @@
|
|||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
_renderDashedStroke: function(ctx) {
|
||||
var p1, p2;
|
||||
|
||||
ctx.beginPath();
|
||||
for (var i = 0, len = this.points.length; i < len; i++) {
|
||||
p1 = this.points[i];
|
||||
p2 = this.points[i + 1] || this.points[0];
|
||||
fabric.util.drawDashedLine(ctx, p1.x, p1.y, p2.x, p2.y, this.strokeDashArray);
|
||||
}
|
||||
fabric.Polyline.prototype._renderDashedStroke.call(this, ctx);
|
||||
ctx.closePath();
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
var fabric = global.fabric || (global.fabric = { }),
|
||||
toFixed = fabric.util.toFixed;
|
||||
var fabric = global.fabric || (global.fabric = { });
|
||||
|
||||
if (fabric.Polyline) {
|
||||
fabric.warn('fabric.Polyline is already defined');
|
||||
|
|
@ -100,23 +99,7 @@
|
|||
* @return {String} svg representation of an instance
|
||||
*/
|
||||
toSVG: function(reviver) {
|
||||
var points = [],
|
||||
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), ' ');
|
||||
}
|
||||
|
||||
markup.push(
|
||||
'<polyline ',
|
||||
'points="', points.join(''),
|
||||
'" style="', this.getSvgStyles(),
|
||||
'" transform="', this.getSvgTransform(),
|
||||
' ', this.getSvgTransformMatrix(),
|
||||
'"/>\n'
|
||||
);
|
||||
|
||||
return reviver ? reviver(markup.join('')) : markup.join('');
|
||||
return fabric.Polygon.prototype.toSVG.call(this, reviver);
|
||||
},
|
||||
/* _TO_SVG_END_ */
|
||||
|
||||
|
|
@ -125,22 +108,7 @@
|
|||
* @param {CanvasRenderingContext2D} ctx Context to render on
|
||||
*/
|
||||
_render: function(ctx) {
|
||||
var point;
|
||||
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, len = this.points.length; i < len; i++) {
|
||||
point = this.points[i];
|
||||
ctx.lineTo(point.x, point.y);
|
||||
}
|
||||
|
||||
fabric.Polygon.prototype.commonRender.call(this, ctx);
|
||||
this._renderFill(ctx);
|
||||
this._renderStroke(ctx);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue