Skip to content

Commit 220a1c3

Browse files
authored
Merge pull request #11 from processing/shape-updates
Shape updates
2 parents 0ca288b + 29bcf5a commit 220a1c3

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/shapes.js

Lines changed: 28 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);
@@ -59,6 +60,33 @@ function addShapes(p5, fn, lifecycles) {
5960
this._renderer._currentShape.at(-1, -1).handlesClose = () => false;
6061
oldEndShape.call(this, mode);
6162
}
63+
64+
fn.curve = function(...args) {
65+
return this.spline(...args);
66+
}
67+
68+
fn.beginGeometry = function(...args) {
69+
return this._renderer.beginGeometry(...args);
70+
}
71+
fn.endGeometry = function(...args) {
72+
return this._renderer.endGeometry(...args);
73+
}
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+
}
6290
}
6391

6492
if (typeof p5 !== undefined) {

0 commit comments

Comments
 (0)