Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor vertex properties out into base renderer #7380

Closed
wants to merge 2 commits into from
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
4 changes: 2 additions & 2 deletions src/color/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ function setting(p5, fn){
* </div>
*/
fn.noFill = function() {
this._renderer.states.doFill = false;
this._renderer.noFill();
return this;
};

Expand Down Expand Up @@ -1325,7 +1325,7 @@ function setting(p5, fn){
* </div>
*/
fn.noStroke = function() {
this._renderer.states.doStroke = false;
this._renderer.states.strokeColor = false;
return this;
};

Expand Down
30 changes: 23 additions & 7 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @for p5
*/

import { Color } from '../color/p5.Color';
import * as constants from '../core/constants';
import { Image } from '../image/p5.Image';

Expand All @@ -25,9 +26,9 @@ class Renderer {

// Renderer state machine
this.states = {
doStroke: true,
strokeColor: new Color([0, 0, 0]),
strokeSet: false,
doFill: true,
fillColor: new Color([255, 255, 255]),
fillSet: false,
tint: null,
imageMode: constants.CORNER,
Expand Down Expand Up @@ -153,14 +154,29 @@ class Renderer {

}

fill() {
fill(...args) {
this.states.fillSet = true;
this.states.doFill = true;
this.states.fillColor = this._pInst.color(...args);
}

stroke() {
noFill() {
this.states.fillColor = null;
}

stroke(...args) {
this.states.strokeSet = true;
this.states.doStroke = true;
this.states.strokeColor = this._pInst.color(...args);
}

noStroke() {
this.states.strokeColor = null;
}

vertexProperties() {
return {
stroke: this.states.strokeColor,
fill: this.states.fillColor,
}
}

textSize(s) {
Expand Down Expand Up @@ -254,7 +270,7 @@ class Renderer {
// fix for #5785 (top of bounding box)
let finalMinHeight = y;

if (!(this.states.doFill || this.states.doStroke)) {
if (!(this.states.fillColor || this.states.strokeColor)) {
return;
}

Expand Down
74 changes: 37 additions & 37 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Renderer2D extends Renderer {

fill(...args) {
super.fill(...args);
const color = this._pInst.color(...args);
const color = this.states.fillColor;
this._setFill(color.toString());

//accessible Outputs
Expand All @@ -212,7 +212,7 @@ class Renderer2D extends Renderer {

stroke(...args) {
super.stroke(...args);
const color = this._pInst.color(...args);
const color = this.states.strokeColor;
this._setStroke(color.toString());

//accessible Outputs
Expand Down Expand Up @@ -672,7 +672,7 @@ class Renderer2D extends Renderer {
}

// Fill curves
if (this.states.doFill) {
if (this.states.fillColor) {
if (!this._clipping) ctx.beginPath();
curves.forEach((curve, index) => {
if (index === 0) {
Expand All @@ -692,7 +692,7 @@ class Renderer2D extends Renderer {
}

// Stroke curves
if (this.states.doStroke) {
if (this.states.strokeColor) {
if (!this._clipping) ctx.beginPath();
curves.forEach((curve, index) => {
if (index === 0) {
Expand All @@ -717,8 +717,8 @@ class Renderer2D extends Renderer {

ellipse(args) {
const ctx = this.drawingContext;
const doFill = this.states.doFill,
doStroke = this.states.doStroke;
const doFill = !!this.states.fillColor,
doStroke = this.states.strokeColor;
const x = parseFloat(args[0]),
y = parseFloat(args[1]),
w = parseFloat(args[2]),
Expand Down Expand Up @@ -750,7 +750,7 @@ class Renderer2D extends Renderer {

line(x1, y1, x2, y2) {
const ctx = this.drawingContext;
if (!this.states.doStroke) {
if (!this.states.strokeColor) {
return this;
} else if (this._getStroke() === styleEmpty) {
return this;
Expand All @@ -764,7 +764,7 @@ class Renderer2D extends Renderer {

point(x, y) {
const ctx = this.drawingContext;
if (!this.states.doStroke) {
if (!this.states.strokeColor) {
return this;
} else if (this._getStroke() === styleEmpty) {
return this;
Expand All @@ -785,8 +785,8 @@ class Renderer2D extends Renderer {

quad(x1, y1, x2, y2, x3, y3, x4, y4) {
const ctx = this.drawingContext;
const doFill = this.states.doFill,
doStroke = this.states.doStroke;
const doFill = !!this.states.fillColor,
doStroke = this.states.strokeColor;
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
Expand Down Expand Up @@ -821,8 +821,8 @@ class Renderer2D extends Renderer {
let br = args[6];
let bl = args[7];
const ctx = this.drawingContext;
const doFill = this.states.doFill,
doStroke = this.states.doStroke;
const doFill = !!this.states.fillColor,
doStroke = this.states.strokeColor;
if (doFill && !doStroke) {
if (this._getFill() === styleEmpty) {
return this;
Expand Down Expand Up @@ -891,10 +891,10 @@ class Renderer2D extends Renderer {
ctx.arcTo(x, y, x + w, y, tl);
ctx.closePath();
}
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
ctx.fill();
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
ctx.stroke();
}
return this;
Expand All @@ -903,8 +903,8 @@ class Renderer2D extends Renderer {

triangle(args) {
const ctx = this.drawingContext;
const doFill = this.states.doFill,
doStroke = this.states.doStroke;
const doFill = !!this.states.fillColor,
doStroke = this.states.strokeColor;
const x1 = args[0],
y1 = args[1];
const x2 = args[2],
Expand Down Expand Up @@ -945,7 +945,7 @@ class Renderer2D extends Renderer {
if (vertices.length === 0) {
return this;
}
if (!this.states.doStroke && !this.states.doFill) {
if (!this.states.strokeColor && !this.states.fillColor) {
return this;
}
const closeShape = mode === constants.CLOSE;
Expand Down Expand Up @@ -1039,15 +1039,15 @@ class Renderer2D extends Renderer {
if (shapeKind === constants.POINTS) {
for (i = 0; i < numVerts; i++) {
v = vertices[i];
if (this.states.doStroke) {
if (this.states.strokeColor) {
this._pInst.stroke(v[6]);
}
this._pInst.point(v[0], v[1]);
}
} else if (shapeKind === constants.LINES) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
if (this.states.doStroke) {
if (this.states.strokeColor) {
this._pInst.stroke(vertices[i + 1][6]);
}
this._pInst.line(v[0], v[1], vertices[i + 1][0], vertices[i + 1][1]);
Expand All @@ -1060,11 +1060,11 @@ class Renderer2D extends Renderer {
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.closePath();
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(vertices[i + 2][5]);
this.drawingContext.fill();
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(vertices[i + 2][6]);
this.drawingContext.stroke();
}
Expand All @@ -1075,18 +1075,18 @@ class Renderer2D extends Renderer {
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(vertices[i + 1][6]);
}
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(vertices[i + 1][5]);
}
if (i + 2 < numVerts) {
this.drawingContext.lineTo(vertices[i + 2][0], vertices[i + 2][1]);
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(vertices[i + 2][6]);
}
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(vertices[i + 2][5]);
}
}
Expand All @@ -1106,15 +1106,15 @@ class Renderer2D extends Renderer {
// If the next colour is going to be different, stroke / fill now
if (i < numVerts - 1) {
if (
(this.states.doFill && v[5] !== vertices[i + 1][5]) ||
(this.states.doStroke && v[6] !== vertices[i + 1][6])
(this.states.fillColor && v[5] !== vertices[i + 1][5]) ||
(this.states.strokeColor && v[6] !== vertices[i + 1][6])
) {
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(v[5]);
this.drawingContext.fill();
this._pInst.fill(vertices[i + 1][5]);
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(v[6]);
this.drawingContext.stroke();
this._pInst.stroke(vertices[i + 1][6]);
Expand All @@ -1135,10 +1135,10 @@ class Renderer2D extends Renderer {
this.drawingContext.lineTo(vertices[i + j][0], vertices[i + j][1]);
}
this.drawingContext.lineTo(v[0], v[1]);
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(vertices[i + 3][5]);
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(vertices[i + 3][6]);
}
this._doFillStrokeClose(closeShape);
Expand All @@ -1156,10 +1156,10 @@ class Renderer2D extends Renderer {
vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(
vertices[i + 3][0], vertices[i + 3][1]);
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this._pInst.fill(vertices[i + 3][5]);
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this._pInst.stroke(vertices[i + 3][6]);
}
} else {
Expand Down Expand Up @@ -1290,10 +1290,10 @@ class Renderer2D extends Renderer {
if (closeShape) {
this.drawingContext.closePath();
}
if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
this.drawingContext.fill();
}
if (!this._clipping && this.states.doStroke) {
if (!this._clipping && this.states.strokeColor) {
this.drawingContext.stroke();
}
}
Expand Down Expand Up @@ -1352,11 +1352,11 @@ class Renderer2D extends Renderer {
// a system/browser font

// no stroke unless specified by user
if (this.states.doStroke && this.states.strokeSet) {
if (this.states.strokeColor && this.states.strokeSet) {
this.drawingContext.strokeText(line, x, y);
}

if (!this._clipping && this.states.doFill) {
if (!this._clipping && this.states.fillColor) {
// if fill hasn't been set by user, use default text fill
if (!this.states.fillSet) {
this._setFill(constants._DEFAULT_TEXT_FILL);
Expand Down
14 changes: 7 additions & 7 deletions src/shape/2d_primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ function primitives(p5, fn){

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

Expand Down Expand Up @@ -540,7 +540,7 @@ function primitives(p5, fn){
fn._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.states.doStroke && !this._renderer.states.doFill) {
if (!this._renderer.states.strokeColor && !this._renderer.states.fillColor) {
return this;
}

Expand Down Expand Up @@ -712,7 +712,7 @@ function primitives(p5, fn){
fn.line = function(...args) {
p5._validateParameters('line', args);

if (this._renderer.states.doStroke) {
if (this._renderer.states.strokeColor) {
this._renderer.line(...args);
}

Expand Down Expand Up @@ -896,7 +896,7 @@ function primitives(p5, fn){
fn.point = function(...args) {
p5._validateParameters('point', args);

if (this._renderer.states.doStroke) {
if (this._renderer.states.strokeColor) {
if (args.length === 1 && args[0] instanceof p5.Vector) {
this._renderer.point.call(
this._renderer,
Expand Down Expand Up @@ -1057,7 +1057,7 @@ function primitives(p5, fn){
fn.quad = function(...args) {
p5._validateParameters('quad', args);

if (this._renderer.states.doStroke || this._renderer.states.doFill) {
if (this._renderer.states.strokeColor || this._renderer.states.fillColor) {
if (this._renderer.isP3D && args.length < 12) {
// if 3D and we weren't passed 12 args, assume Z is 0
this._renderer.quad.call(
Expand Down Expand Up @@ -1334,7 +1334,7 @@ function primitives(p5, fn){

// internal method to have renderer draw a rectangle
fn._renderRect = function() {
if (this._renderer.states.doStroke || this._renderer.states.doFill) {
if (this._renderer.states.strokeColor || this._renderer.states.fillColor) {
// duplicate width for height in case only 3 arguments is provided
if (arguments.length === 3) {
arguments[3] = arguments[2];
Expand Down Expand Up @@ -1433,7 +1433,7 @@ function primitives(p5, fn){
fn.triangle = function(...args) {
p5._validateParameters('triangle', args);

if (this._renderer.states.doStroke || this._renderer.states.doFill) {
if (this._renderer.states.strokeColor || this._renderer.states.fillColor) {
this._renderer.triangle(args);
}

Expand Down
Loading
Loading