Skip to content
Closed
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
17 changes: 17 additions & 0 deletions src/core/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,21 @@ p5.prototype.strokeWeight = function(w) {
return this;
};

/**
* Sets the fill rule used when filling paths, either NONZERO or EVENODD.
* The default fill rule is NONZERO.
*
* @method fillRule
* @param {Number/Constant} fillRule either NONZERO, EVENODD
* @return {p5} the p5 object
*
*/
p5.prototype.fillRule = function(fillRule) {
if (fillRule === constants.NONZERO ||
fillRule === constants.EVENODD) {
this._renderer.fillRule(fillRule);
}
return this;
};

module.exports = p5;
2 changes: 2 additions & 0 deletions src/core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ module.exports = {
ROUND: 'round',
BEVEL: 'bevel',
MITER: 'miter',
NONZERO: 'nonzero',
EVENODD: 'evenodd',

// COLOR
RGB: 'rgb',
Expand Down
1 change: 1 addition & 0 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ p5.Renderer = function(elt, pInst, isMainCanvas) {
this._ellipseMode = constants.CENTER;
this._curveTightness = 0;
this._imageMode = constants.CORNER;
this._fillRule = constants.NONZERO;

this._tint = null;
this._doStroke = true;
Expand Down
24 changes: 16 additions & 8 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ p5.Renderer2D.prototype.arc =
ctx.lineTo(vals.x, vals.y);
}
ctx.closePath();
ctx.fill();
ctx.fill(this._fillRule);
}

// Stroke curves
Expand Down Expand Up @@ -475,7 +475,7 @@ p5.Renderer2D.prototype.ellipse = function(args) {
ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
ctx.closePath();
if (doFill) {
ctx.fill();
ctx.fill(this._fillRule);
}
if (doStroke) {
ctx.stroke();
Expand Down Expand Up @@ -525,7 +525,7 @@ p5.Renderer2D.prototype.point = function(x, y) {
constants.TWO_PI,
false
);
ctx.fill();
ctx.fill(this._fillRule);
} else {
ctx.fillRect(x, y, 1, 1);
}
Expand All @@ -552,7 +552,7 @@ p5.Renderer2D.prototype.quad =
ctx.lineTo(x4, y4);
ctx.closePath();
if (doFill) {
ctx.fill();
ctx.fill(this._fillRule);
}
if (doStroke) {
ctx.stroke();
Expand Down Expand Up @@ -619,7 +619,7 @@ p5.Renderer2D.prototype.rect = function(args) {
ctx.closePath();
}
if (this._doFill) {
ctx.fill();
ctx.fill(this._fillRule);
}
if (this._doStroke) {
ctx.stroke();
Expand Down Expand Up @@ -651,7 +651,7 @@ p5.Renderer2D.prototype.triangle = function(args) {
ctx.lineTo(x3, y3);
ctx.closePath();
if (doFill) {
ctx.fill();
ctx.fill(this._fillRule);
}
if (doStroke) {
ctx.stroke();
Expand Down Expand Up @@ -764,7 +764,7 @@ function (mode, vertices, isCurve, isBezier,
this.drawingContext.lineTo(v[0], v[1]);
if (this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
this.drawingContext.fill();
this.drawingContext.fill(this._fillRule);
}
if (this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
Expand Down Expand Up @@ -952,6 +952,14 @@ p5.Renderer2D.prototype.strokeWeight = function(w) {
return this;
};

p5.Renderer2D.prototype.fillRule = function(fillRule) {
if (fillRule === constants.NONZERO ||
fillRule === constants.EVENODD) {
this._fillRule = fillRule;
}
return this;
};

p5.Renderer2D.prototype._getFill = function(){
return this.drawingContext.fillStyle;
};
Expand Down Expand Up @@ -987,7 +995,7 @@ p5.Renderer2D.prototype.curve = function (x1, y1, x2, y2, x3, y3, x4, y4) {

p5.Renderer2D.prototype._doFillStrokeClose = function () {
if (this._doFill) {
this.drawingContext.fill();
this.drawingContext.fill(this._fillRule);
}
if (this._doStroke) {
this.drawingContext.stroke();
Expand Down