Skip to content

Commit a9caf7a

Browse files
authored
Merge branch 'dev-2.0' into nb-remove-requires-jsdoc
2 parents a16e552 + 938cb8b commit a9caf7a

26 files changed

Lines changed: 475 additions & 165 deletions

File tree

src/core/friendly_errors/param_validator.js

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

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

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

442453
if (expectedTypes.size > 0) {
@@ -457,6 +468,7 @@ function validateParams(p5, fn, lifecycles) {
457468
return message;
458469
};
459470

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

src/core/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ export default p5;
672672
*
673673
* @method setup
674674
* @for p5
675+
* @return {void|Promise<void>}
675676
*
676677
* @example
677678
* function setup() {

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: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import filterBaseVert from '../webgl/shaders/filters/base.vert';
1717
import webgl2CompatibilityShader from '../webgl/shaders/webgl2Compatibility.glsl';
1818
import { glslBackend } from '../webgl/strands_glslBackend';
1919
import { getShaderHookTypes } from '../webgl/shaderHookUtils';
20-
import noiseGLSL from '../webgl/shaders/functions/noise3DGLSL.glsl';
2120
import { makeFilterShader } from '../core/filterShaders';
2221

2322
class FilterRenderer2D {
@@ -309,9 +308,6 @@ class FilterRenderer2D {
309308
return this._baseFilterShader;
310309
}
311310

312-
getNoiseShaderSnippet() {
313-
return noiseGLSL;
314-
}
315311

316312
/**
317313
* Set the current filter operation and parameter. If a customShader is provided,

0 commit comments

Comments
 (0)