Skip to content

Commit

Permalink
refactor vertex functions
Browse files Browse the repository at this point in the history
  • Loading branch information
capGoblin committed Dec 3, 2023
1 parent 0c174c6 commit 4ebac76
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 309 deletions.
263 changes: 15 additions & 248 deletions src/core/p5.Renderer2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,266 +867,33 @@ class Renderer2D extends p5.Renderer {

endShape(
mode,
vertices,
isCurve,
isBezier,
isQuadratic,
isContour,
shapeKind
shapeKind,
allVertsObj,
currentContour,
closeShape
) {
if (vertices.length === 0) {
return this;
}
if (!this._doStroke && !this._doFill) {
return this;
}
const closeShape = mode === constants.CLOSE;
let v;
if (closeShape && !isContour) {
vertices.push(vertices[0]);
}
let i, j;
const numVerts = vertices.length;
if (isCurve && shapeKind === null) {
if (numVerts > 3) {
const b = [],
s = 1 - this._curveTightness;
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[1][0], vertices[1][1]);
for (i = 1; i + 2 < numVerts; i++) {
v = vertices[i];
b[0] = [v[0], v[1]];
b[1] = [
v[0] + (s * vertices[i + 1][0] - s * vertices[i - 1][0]) / 6,
v[1] + (s * vertices[i + 1][1] - s * vertices[i - 1][1]) / 6
];
b[2] = [
vertices[i + 1][0] +
(s * vertices[i][0] - s * vertices[i + 2][0]) / 6,
vertices[i + 1][1] +
(s * vertices[i][1] - s * vertices[i + 2][1]) / 6
];
b[3] = [vertices[i + 1][0], vertices[i + 1][1]];
this.drawingContext.bezierCurveTo(
b[1][0],
b[1][1],
b[2][0],
b[2][1],
b[3][0],
b[3][1]
);
}
if (closeShape) {
this.drawingContext.lineTo(vertices[i + 1][0], vertices[i + 1][1]);
}
this._doFillStrokeClose(closeShape);
}
} else if (
isBezier &&
shapeKind === null
) {
if (!this._clipping) this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.bezierCurveTo(
vertices[i][0],
vertices[i][1],
vertices[i][2],
vertices[i][3],
vertices[i][4],
vertices[i][5]
);
}
}
this._doFillStrokeClose(closeShape);
} else if (
isQuadratic &&
shapeKind === null
) {
if (!this._clipping) this.drawingContext.beginPath();
for (i = 0; i < numVerts; i++) {
if (vertices[i].isVert) {
if (vertices[i].moveTo) {
this.drawingContext.moveTo(vertices[i][0], vertices[i][1]);
} else {
this.drawingContext.lineTo(vertices[i][0], vertices[i][1]);
}
} else {
this.drawingContext.quadraticCurveTo(
vertices[i][0],
vertices[i][1],
vertices[i][2],
vertices[i][3]
);
}
}
this._doFillStrokeClose(closeShape);
} else {
if (shapeKind === constants.POINTS) {
for (i = 0; i < numVerts; i++) {
v = vertices[i];
if (this._doStroke) {
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._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
this._pInst.line(v[0], v[1], vertices[i + 1][0], vertices[i + 1][1]);
}
} else if (shapeKind === constants.TRIANGLES) {
for (i = 0; i + 2 < numVerts; i += 3) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);
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._doFill) {
this._pInst.fill(vertices[i + 2][5]);
this.drawingContext.fill();
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
this.drawingContext.stroke();
}
}
} else if (shapeKind === constants.TRIANGLE_STRIP) {
for (i = 0; i + 1 < numVerts; i++) {
v = vertices[i];
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._doStroke) {
this._pInst.stroke(vertices[i + 1][6]);
}
if (!this._clipping && this._doFill) {
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._doStroke) {
this._pInst.stroke(vertices[i + 2][6]);
}
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 2][5]);
}
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.TRIANGLE_FAN) {
if (numVerts > 2) {
// For performance reasons, try to batch as many of the
// fill and stroke calls as possible.
if (!this._clipping) this.drawingContext.beginPath();
for (i = 2; i < numVerts; i++) {
v = vertices[i];
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
this.drawingContext.lineTo(vertices[i - 1][0], vertices[i - 1][1]);
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.lineTo(vertices[0][0], vertices[0][1]);
// If the next colour is going to be different, stroke / fill now
if (i < numVerts - 1) {
if (
(this._doFill && v[5] !== vertices[i + 1][5]) ||
(this._doStroke && v[6] !== vertices[i + 1][6])
) {
if (!this._clipping && this._doFill) {
this._pInst.fill(v[5]);
this.drawingContext.fill();
this._pInst.fill(vertices[i + 1][5]);
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(v[6]);
this.drawingContext.stroke();
this._pInst.stroke(vertices[i + 1][6]);
}
this.drawingContext.closePath();
if (!this._clipping) this.drawingContext.beginPath(); // Begin the next one
}
}
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.QUADS) {
for (i = 0; i + 3 < numVerts; i += 4) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(v[0], v[1]);
for (j = 1; j < 4; j++) {
this.drawingContext.lineTo(vertices[i + j][0], vertices[i + j][1]);
}
this.drawingContext.lineTo(v[0], v[1]);
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
}
this._doFillStrokeClose(closeShape);
}
} else if (shapeKind === constants.QUAD_STRIP) {
if (numVerts > 3) {
for (i = 0; i + 1 < numVerts; i += 2) {
v = vertices[i];
if (!this._clipping) this.drawingContext.beginPath();
if (i + 3 < numVerts) {
this.drawingContext.moveTo(
vertices[i + 2][0], vertices[i + 2][1]);
this.drawingContext.lineTo(v[0], v[1]);
this.drawingContext.lineTo(
vertices[i + 1][0], vertices[i + 1][1]);
this.drawingContext.lineTo(
vertices[i + 3][0], vertices[i + 3][1]);
if (!this._clipping && this._doFill) {
this._pInst.fill(vertices[i + 3][5]);
}
if (!this._clipping && this._doStroke) {
this._pInst.stroke(vertices[i + 3][6]);
}
} else {
this.drawingContext.moveTo(v[0], v[1]);
this.drawingContext.lineTo(
vertices[i + 1][0], vertices[i + 1][1]);
}
this._doFillStrokeClose(closeShape);
}
}
} else {
if (!this._clipping) this.drawingContext.beginPath();
this.drawingContext.moveTo(vertices[0][0], vertices[0][1]);
for (i = 1; i < numVerts; i++) {
v = vertices[i];
if (v.isVert) {
if (v.moveTo) {
if (closeShape) this.drawingContext.closePath();
this.drawingContext.moveTo(v[0], v[1]);
} else {
this.drawingContext.lineTo(v[0], v[1]);
}
}
}
this._doFillStrokeClose(closeShape);
}
let initialVertex = allVertsObj[0];
allVertsObj.currentCoordinates = initialVertex.coordinates;
this.drawingContext.moveTo(...allVertsObj.currentCoordinates);

for (const vert of allVertsObj) {
vert.addToCanvasPath(this.drawingContext);
allVertsObj.currentCoordinates = vert.coordinates;
}
this._doFillStrokeClose(closeShape);

isCurve = false;
isBezier = false;
isQuadratic = false;
isContour = false;

if (closeShape) {
vertices.pop();
currentContour.segmentss.pop();
}

return this;
}
//////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 4ebac76

Please sign in to comment.