Skip to content

Commit

Permalink
decouple param validation from ellipse drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
outofambit committed Dec 2, 2019
1 parent 92a9695 commit 2dad489
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/core/shape/2d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,28 +244,7 @@ p5.prototype.arc = function(x, y, w, h, start, stop, mode, detail) {
p5.prototype.ellipse = function(x, y, w, h, detailX) {
p5._validateParameters('ellipse', arguments);

// if the current stroke and fill settings wouldn't result in something
// visible, exit immediately
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
}

// p5 supports negative width and heights for rects
if (w < 0) {
w = Math.abs(w);
}

if (typeof h === 'undefined') {
// Duplicate 3rd argument if only 3 given.
h = w;
} else if (h < 0) {
h = Math.abs(h);
}

const vals = canvas.modeAdjust(x, y, w, h, this._renderer._ellipseMode);
this._renderer.ellipse([vals.x, vals.y, vals.w, vals.h, detailX]);

return this;
return this._renderEllipse(...arguments);
};

/**
Expand All @@ -292,10 +271,37 @@ p5.prototype.ellipse = function(x, y, w, h, detailX) {
* white circle with black outline in mid of canvas that is 55x55.
*/
p5.prototype.circle = function() {
p5._validateParameters('circle', arguments);
const args = Array.prototype.slice.call(arguments, 0, 2);
args.push(arguments[2]);
args.push(arguments[2]);
return this.ellipse(...args);
return this._renderEllipse(...args);
};

// internal method for drawing ellipses (without parameter validation)
p5.prototype._renderEllipse = function(x, y, w, h, detailX) {
// if the current stroke and fill settings wouldn't result in something
// visible, exit immediately
if (!this._renderer._doStroke && !this._renderer._doFill) {
return this;
}

// p5 supports negative width and heights for rects
if (w < 0) {
w = Math.abs(w);
}

if (typeof h === 'undefined') {
// Duplicate 3rd argument if only 3 given.
h = w;
} else if (h < 0) {
h = Math.abs(h);
}

const vals = canvas.modeAdjust(x, y, w, h, this._renderer._ellipseMode);
this._renderer.ellipse([vals.x, vals.y, vals.w, vals.h, detailX]);

return this;
};

/**
Expand Down

0 comments on commit 2dad489

Please sign in to comment.