Skip to content

Commit 0ac80e1

Browse files
authored
Merge branch 'dev-2.0' into webgpu-compute-auto-spread
2 parents efd8217 + c156aaa commit 0ac80e1

12 files changed

Lines changed: 696 additions & 156 deletions

File tree

src/core/friendly_errors/param_validator.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function validateParams(p5, fn, lifecycles) {
6464
'Boolean': z.boolean(),
6565
'Function': z.function(),
6666
'Integer': z.number().int(),
67-
'Number': z.number(),
67+
'Number': z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]),
6868
'Object': z.object({}),
6969
'String': z.string()
7070
};
@@ -411,33 +411,44 @@ function validateParams(p5, fn, lifecycles) {
411411
// of any of them. In this case, aggregate all possible types and print
412412
// a friendly error message that indicates what the expected types are at
413413
// which position (position is not 0-indexed, for accessibility reasons).
414+
414415
const processUnionError = error => {
415416
const expectedTypes = new Set();
416417
let actualType;
417418

418-
error.errors.forEach(err => {
419-
const issue = err[0];
420-
if (issue) {
421-
if (!actualType) {
422-
actualType = issue.message;
423-
}
419+
const collectIssue = issue => {
420+
if (!issue) return;
421+
if (!actualType) {
422+
actualType = issue.message;
423+
}
424424

425-
if (issue.code === 'invalid_type') {
426-
actualType = issue.message.split(', received ')[1];
427-
expectedTypes.add(issue.expected);
428-
}
429-
// The case for constants. Since we don't want to print out the actual
430-
// constant values in the error message, the error message will
431-
// direct users to the documentation.
432-
else if (issue.code === 'invalid_value') {
425+
if (issue.code === 'invalid_type') {
426+
actualType = issue.message.split(', received ')[1];
427+
expectedTypes.add(issue.expected);
428+
}
429+
// The case for constants. Since we don't want to print out the actual
430+
// constant values in the error message, the error message will
431+
// direct users to the documentation.
432+
else if (issue.code === 'invalid_value') {
433+
if (Array.isArray(issue.values) && issue.values.every(v => v === Infinity || v === -Infinity)) {
434+
expectedTypes.add('number');
435+
} else {
433436
expectedTypes.add('constant (please refer to documentation for allowed values)');
434437
actualType = args[error.path[0]];
435-
} else if (issue.code === 'custom') {
436-
const match = issue.message.match(/Input not instance of (\w+)/);
437-
if (match) expectedTypes.add(match[1]);
438-
actualType = undefined;
439438
}
439+
} else if (issue.code === 'custom') {
440+
const match = issue.message.match(/Input not instance of (\w+)/);
441+
if (match) expectedTypes.add(match[1]);
442+
actualType = undefined;
443+
} else if (issue.code === 'invalid_union') {
444+
issue.errors.forEach(nestedErr => {
445+
nestedErr.forEach(nestedIssue => collectIssue(nestedIssue));
446+
});
440447
}
448+
};
449+
450+
error.errors.forEach(err => {
451+
err.forEach(issue => collectIssue(issue));
441452
});
442453

443454
if (expectedTypes.size > 0) {
@@ -458,6 +469,7 @@ function validateParams(p5, fn, lifecycles) {
458469
return message;
459470
};
460471

472+
461473
switch (currentError.code) {
462474
case 'invalid_union': {
463475
processUnionError(currentError);

src/core/p5.Renderer2D.js

Lines changed: 19 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ class Renderer2D extends Renderer {
287287
this.clipPath.closePath();
288288
} else {
289289
if (this.states.fillColor) {
290-
this.drawingContext.fill(visitor.path);
290+
this.drawingContext.fill(visitor.fillPath || visitor.path);
291291
}
292292
if (this.states.strokeColor) {
293-
this.drawingContext.stroke(visitor.path);
293+
this.drawingContext.stroke(visitor.strokePath || visitor.path);
294294
}
295295
}
296296
}
@@ -672,106 +672,35 @@ class Renderer2D extends Renderer {
672672
* start <= stop < start + TWO_PI
673673
*/
674674
arc(x, y, w, h, start, stop, mode) {
675-
const ctx = this.drawingContext;
676-
const rx = w / 2.0;
677-
const ry = h / 2.0;
678-
const epsilon = 0.00001; // Smallest visible angle on displays up to 4K.
679-
let arcToDraw = 0;
680-
const curves = [];
681-
682-
const centerX = x + w / 2,
683-
centerY = y + h / 2,
684-
radiusX = w / 2,
685-
radiusY = h / 2;
686-
if (this._clipping) {
687-
const tempPath = new Path2D();
688-
tempPath.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
689-
const currentTransform = this.drawingContext.getTransform();
690-
const clipBaseTransform = this._clipBaseTransform.inverse();
691-
const relativeTransform = clipBaseTransform.multiply(currentTransform);
692-
this.clipPath.addPath(tempPath, relativeTransform);
693-
return this;
694-
}
695-
// Determines whether to add a line to the center, which should be done
696-
// when the mode is PIE or default; as well as when the start and end
697-
// angles do not form a full circle.
698-
const createPieSlice = ! (
699-
mode === constants.CHORD ||
700-
mode === constants.OPEN ||
701-
(stop - start) % constants.TWO_PI === 0
675+
const shape = new p5.Shape({ position: new p5.Vector(0, 0) });
676+
shape.beginShape();
677+
shape.arcPrimitive(
678+
x,
679+
y,
680+
w,
681+
h,
682+
start,
683+
stop,
684+
mode
702685
);
703-
704-
// Fill curves
705-
if (this.states.fillColor) {
706-
ctx.beginPath();
707-
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
708-
if (createPieSlice) ctx.lineTo(centerX, centerY);
709-
ctx.closePath();
710-
ctx.fill();
711-
}
712-
713-
// Stroke curves
714-
if (this.states.strokeColor) {
715-
ctx.beginPath();
716-
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, start, stop);
717-
718-
if (mode === constants.PIE && createPieSlice) {
719-
// In PIE mode, stroke is added to the center and back to path,
720-
// unless the pie forms a complete ellipse (see: createPieSlice)
721-
ctx.lineTo(centerX, centerY);
722-
}
723-
724-
if (mode === constants.PIE || mode === constants.CHORD) {
725-
// Stroke connects back to path begin for both PIE and CHORD
726-
ctx.closePath();
727-
}
728-
ctx.stroke();
729-
}
686+
shape.endShape();
687+
this.drawShape(shape);
730688

731689
return this;
732690

733691
}
734692

735693
ellipse(args) {
736-
const ctx = this.drawingContext;
737-
const doFill = !!this.states.fillColor,
738-
doStroke = this.states.strokeColor;
739694
const x = parseFloat(args[0]),
740695
y = parseFloat(args[1]),
741696
w = parseFloat(args[2]),
742697
h = parseFloat(args[3]);
743-
if (doFill && !doStroke) {
744-
if (this._getFill() === styleEmpty) {
745-
return this;
746-
}
747-
} else if (!doFill && doStroke) {
748-
if (this._getStroke() === styleEmpty) {
749-
return this;
750-
}
751-
}
752-
const centerX = x + w / 2,
753-
centerY = y + h / 2,
754-
radiusX = w / 2,
755-
radiusY = h / 2;
756-
if (this._clipping) {
757-
const tempPath = new Path2D();
758-
tempPath.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
759-
const currentTransform = this.drawingContext.getTransform();
760-
const clipBaseTransform = this._clipBaseTransform.inverse();
761-
const relativeTransform = clipBaseTransform.multiply(currentTransform);
762-
this.clipPath.addPath(tempPath, relativeTransform);
763-
return this;
764-
}
765-
ctx.beginPath();
766-
ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
767-
ctx.closePath();
768-
if (doFill) {
769-
ctx.fill();
770-
}
771-
if (doStroke) {
772-
ctx.stroke();
773-
}
774698

699+
const shape = new p5.Shape({ position: new p5.Vector(0, 0) });
700+
shape.beginShape();
701+
shape.ellipsePrimitive(x,y,w,h);
702+
shape.endShape();
703+
this.drawShape(shape);
775704
return this;
776705
}
777706

src/image/filterRenderer2D.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,10 @@ class FilterRenderer2D {
299299
'vec4 getColor': `(FilterInputs inputs, in sampler2D canvasContent) {
300300
return getTexture(canvasContent, inputs.texCoord);
301301
}`
302-
}
302+
},
303+
hookAliases: {
304+
'getColor': ['filterColor'],
305+
},
303306
}
304307
);
305308
}

src/math/p5.Vector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ class Vector {
10341034
*
10351035
* If only one value is provided, as in `v.div(2)`, then all the components
10361036
* will be divided by 2. If a value isn't provided for a component, it
1037-
* won't change. For example, `v.div(4, 5)` divides `v.x` by, `v.y` by 5,
1037+
* won't change. For example, `v.div(4, 5)` divides `v.x` by 4, `v.y` by 5,
10381038
* and `v.z` by 1. Calling `div()` with no arguments, as in `v.div()`, has
10391039
* no effect.
10401040
*

0 commit comments

Comments
 (0)