Skip to content

Shape updates #11

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

Merged
merged 3 commits into from
Mar 22, 2025
Merged
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
28 changes: 28 additions & 0 deletions src/shapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function addShapes(p5, fn, lifecycles) {
const oldBezierVertex = fn.bezierVertex;
const oldEndContour = fn.endContour;
const oldEndShape = fn.endShape;
const oldCurveDetail = fn.curveDetail;

lifecycles.predraw = function() {
this.splineProperty('ends', this.EXCLUDE);
Expand Down Expand Up @@ -59,6 +60,33 @@ function addShapes(p5, fn, lifecycles) {
this._renderer._currentShape.at(-1, -1).handlesClose = () => false;
oldEndShape.call(this, mode);
}

fn.curve = function(...args) {
return this.spline(...args);
}

fn.beginGeometry = function(...args) {
return this._renderer.beginGeometry(...args);
}
fn.endGeometry = function(...args) {
return this._renderer.endGeometry(...args);
}

for (const key of ['curveDetail', 'bezierDetail']) {
fn[key] = function(numPoints) {
// p5 2.0's curveDetail defined *density* while 1.x's defined *absolute number of points.*
// The only way to do a true conversion would involve updating the value dynamically based
// on the length of the curve. Since this would be complex to do as an addon, we do
// the calculation based on an approximate average curve length.
const avgLength = Math.hypot(this.width, this.height) / 3;
if (numPoints) {
const density = numPoints / avgLength;
return oldCurveDetail.call(this, density);
} else {
return oldCurveDetail.call(this) * avgLength;
}
}
}
}

if (typeof p5 !== undefined) {
Expand Down