Skip to content

Commit 29bcf5a

Browse files
committed
Add approximate curveDetail implementation
1 parent ff9e11d commit 29bcf5a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/shapes.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function addShapes(p5, fn, lifecycles) {
22
const oldBezierVertex = fn.bezierVertex;
33
const oldEndContour = fn.endContour;
44
const oldEndShape = fn.endShape;
5+
const oldCurveDetail = fn.curveDetail;
56

67
lifecycles.predraw = function() {
78
this.splineProperty('ends', this.EXCLUDE);
@@ -70,6 +71,22 @@ function addShapes(p5, fn, lifecycles) {
7071
fn.endGeometry = function(...args) {
7172
return this._renderer.endGeometry(...args);
7273
}
74+
75+
for (const key of ['curveDetail', 'bezierDetail']) {
76+
fn[key] = function(numPoints) {
77+
// p5 2.0's curveDetail defined *density* while 1.x's defined *absolute number of points.*
78+
// The only way to do a true conversion would involve updating the value dynamically based
79+
// on the length of the curve. Since this would be complex to do as an addon, we do
80+
// the calculation based on an approximate average curve length.
81+
const avgLength = Math.hypot(this.width, this.height) / 3;
82+
if (numPoints) {
83+
const density = numPoints / avgLength;
84+
return oldCurveDetail.call(this, density);
85+
} else {
86+
return oldCurveDetail.call(this) * avgLength;
87+
}
88+
}
89+
}
7390
}
7491

7592
if (typeof p5 !== undefined) {

0 commit comments

Comments
 (0)