Skip to content

Commit 4a59bfe

Browse files
authored
Merge branch 'dev-2.0' into fix-unmatched-push-popwarning
2 parents 41913cd + 8f23e69 commit 4a59bfe

File tree

15 files changed

+164
-17
lines changed

15 files changed

+164
-17
lines changed

src/core/friendly_errors/fes_core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ function fesCore(p5, fn){
593593

594594
// get the function just above the topmost frame in the friendlyStack.
595595
// i.e the name of the library function called from user's code
596-
const func = stacktrace[friendlyStack[0].frameIndex - 2].functionName
596+
const func = stacktrace[friendlyStack[0].frameIndex - 1].functionName
597597
.split('.')
598598
.slice(-1)[0];
599599

src/dom/p5.MediaElement.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,13 @@ class MediaElement extends Element {
935935

936936
/*** CONNECT TO WEB AUDIO API / p5.sound.js ***/
937937

938+
_getAudioContext() {
939+
return undefined;
940+
}
941+
_getSoundOut() {
942+
return undefined;
943+
}
944+
938945
/**
939946
* Sends the element's audio to an output.
940947
*
@@ -954,9 +961,9 @@ class MediaElement extends Element {
954961
let audioContext, mainOutput;
955962

956963
// if p5.sound exists, same audio context
957-
if (typeof fn.getAudioContext === 'function') {
958-
audioContext = fn.getAudioContext();
959-
mainOutput = p5.soundOut.input;
964+
if (this._getAudioContext() && this._getSoundOut()) {
965+
audioContext = this._getAudioContext();
966+
mainOutput = this._getSoundOut().input;
960967
} else {
961968
try {
962969
audioContext = obj.context;
@@ -1792,6 +1799,19 @@ function media(p5, fn){
17921799
*/
17931800
p5.MediaElement = MediaElement;
17941801

1802+
// Patch MediaElement to give it access to fn, which p5.sound may attach things to
1803+
// if present in a sketch
1804+
MediaElement.prototype._getSoundOut = function() {
1805+
return p5.soundOut;
1806+
}
1807+
MediaElement.prototype._getAudioContext = function() {
1808+
if (typeof fn.getAudioContext === 'function') {
1809+
return fn.getAudioContext();
1810+
} else {
1811+
return undefined;
1812+
}
1813+
}
1814+
17951815
/**
17961816
* Path to the media element's source as a string.
17971817
*

src/type/p5.Font.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ export class Font {
234234
* path and are more precise.
235235
*
236236
* `simplifyThreshold` removes collinear points if it's set to a number other
237-
* than 0. The value represents the threshold angle to use when determining
237+
* than 0. The value represents the threshold angle in radians to use when determining
238238
* whether two edges are collinear.
239239
*
240240
* @param {String} str string of text.
241241
* @param {Number} x x-coordinate of the text.
242242
* @param {Number} y y-coordinate of the text.
243243
* @param {Object} [options] Configuration:
244244
* @param {Number} [options.sampleFactor=0.1] The ratio of the text's path length to the number of samples.
245-
* @param {Number} [options.simplifyThreshold=0] A minmum angle between two segments. Segments with a shallower angle will be merged.
245+
* @param {Number} [options.simplifyThreshold=0] A minmum angle in radian sbetween two segments. Segments with a shallower angle will be merged.
246246
* @return {Array<Object>} array of point objects, each with `x`, `y`, and `alpha` (path angle) properties.
247247
*
248248
* @example
@@ -306,15 +306,15 @@ export class Font {
306306
* path and are more precise.
307307
*
308308
* `simplifyThreshold` removes collinear points if it's set to a number other
309-
* than 0. The value represents the threshold angle to use when determining
309+
* than 0. The value represents the threshold angle in radians to use when determining
310310
* whether two edges are collinear.
311311
*
312312
* @param {String} str string of text.
313313
* @param {Number} x x-coordinate of the text.
314314
* @param {Number} y y-coordinate of the text.
315315
* @param {Object} [options] Configuration options:
316316
* @param {Number} [options.sampleFactor=0.1] The ratio of the text's path length to the number of samples.
317-
* @param {Number} [options.simplifyThreshold=0] A minmum angle between two segments. Segments with a shallower angle will be merged.
317+
* @param {Number} [options.simplifyThreshold=0] A minmum angle in radians between two segments. Segments with a shallower angle will be merged.
318318
* @return {Array<Array<Object>>} array of point objects, each with `x`, `y`, and `alpha` (path angle) properties.
319319
*
320320
* @example
@@ -1092,6 +1092,35 @@ function pathToPoints(cmds, options, font) {
10921092
return num;
10931093
};
10941094

1095+
const collinear = (a, b, c, thresholdAngle) => {
1096+
if (!thresholdAngle) {
1097+
return areaTriangle(a, b, c) === 0;
1098+
}
1099+
1100+
if (typeof collinear.tmpPoint1 === 'undefined') {
1101+
collinear.tmpPoint1 = [];
1102+
collinear.tmpPoint2 = [];
1103+
}
1104+
1105+
const ab = collinear.tmpPoint1,
1106+
bc = collinear.tmpPoint2;
1107+
ab.x = b.x - a.x;
1108+
ab.y = b.y - a.y;
1109+
bc.x = c.x - b.x;
1110+
bc.y = c.y - b.y;
1111+
1112+
const dot = ab.x * bc.x + ab.y * bc.y,
1113+
magA = Math.sqrt(ab.x * ab.x + ab.y * ab.y),
1114+
magB = Math.sqrt(bc.x * bc.x + bc.y * bc.y),
1115+
angle = Math.acos(dot / (magA * magB));
1116+
1117+
return angle < thresholdAngle;
1118+
};
1119+
1120+
const areaTriangle = (a, b, c) => {
1121+
return (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
1122+
};
1123+
10951124
const path = createFromCommands(arrayCommandsToObjects(cmds));
10961125
let opts = parseOpts(options, {
10971126
sampleFactor: 0.1,

src/webgl/3d_primitives.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ function primitives3D(p5, fn){
17261726

17271727
this.states.setValue('uModelMatrix', mult);
17281728

1729-
this._drawGeometry(this.geometryBufferCache.getGeometryByID(gid));
1729+
this.model(this.geometryBufferCache.getGeometryByID(gid));
17301730
} finally {
17311731
this.states.setValue('uModelMatrix', uModelMatrix);
17321732
}
@@ -1858,7 +1858,7 @@ function primitives3D(p5, fn){
18581858
this.states.uModelMatrix.translate([x, y, 0]);
18591859
this.states.uModelMatrix.scale(width, height, 1);
18601860

1861-
this._drawGeometry(this.geometryBufferCache.getGeometryByID(gid));
1861+
this.model(this.geometryBufferCache.getGeometryByID(gid));
18621862
} finally {
18631863
this.states.setValue('uModelMatrix', uModelMatrix);
18641864
}
@@ -1919,7 +1919,7 @@ function primitives3D(p5, fn){
19191919
this.states.uModelMatrix.translate([x, y, 0]);
19201920
this.states.uModelMatrix.scale(width, height, 1);
19211921

1922-
this._drawGeometry(this.geometryBufferCache.getGeometryByID(gid));
1922+
this.model(this.geometryBufferCache.getGeometryByID(gid));
19231923
} finally {
19241924
this.states.setValue('uModelMatrix', uModelMatrix);
19251925
}
@@ -1967,7 +1967,7 @@ function primitives3D(p5, fn){
19671967
const prevOrder = this.bezierOrder();
19681968
this.bezierOrder(2);
19691969
this.beginShape();
1970-
const addUVs = (x, y) => [x, y, (x - x1)/width, (y - y1)/height];
1970+
const addUVs = (x, y) => [x, y, 0, (x - x1)/width, (y - y1)/height];
19711971
if (tr !== 0) {
19721972
this.vertex(...addUVs(x2 - tr, y1));
19731973
this.bezierVertex(...addUVs(x2, y1));
@@ -2069,7 +2069,7 @@ function primitives3D(p5, fn){
20692069
quadGeom.gid = gid;
20702070
this.geometryBufferCache.ensureCached(quadGeom);
20712071
}
2072-
this._drawGeometry(this.geometryBufferCache.getGeometryByID(gid));
2072+
this.model(this.geometryBufferCache.getGeometryByID(gid));
20732073
return this;
20742074
};
20752075

src/webgl/GeometryBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class GeometryBuilder {
137137
for (let i = 2; i < geometry.vertices.length; i++) {
138138
faces.push([0, i - 1, i]);
139139
}
140-
} else {
140+
} else if (shapeMode === constants.TRIANGLES) {
141141
for (let i = 0; i < geometry.vertices.length; i += 3) {
142142
if (
143143
!validateFaces ||

src/webgl/ShapeBuilder.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ export class ShapeBuilder {
280280
* @private
281281
*/
282282
_tesselateShape() {
283-
// TODO: handle non-PATH shape modes that have contours
284-
this.shapeMode = constants.TRIANGLES;
285283
// const contours = [[]];
286284
const contours = [];
287285
for (let i = 0; i < this.geometry.vertices.length; i++) {
@@ -316,6 +314,15 @@ export class ShapeBuilder {
316314
}
317315

318316
const polyTriangles = this._triangulate(contours);
317+
318+
// If there were no valid faces, we still want to use the original vertices
319+
// for strokes, so we'll stop here.
320+
if (polyTriangles.length === 0) {
321+
return;
322+
}
323+
324+
// TODO: handle non-PATH shape modes that have contours
325+
this.shapeMode = constants.TRIANGLES;
319326
const originalVertices = this.geometry.vertices;
320327
this.geometry.vertices = [];
321328
this.geometry.vertexNormals = [];

src/webgl/p5.RendererGL.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,11 @@ class RendererGL extends Renderer {
511511
);
512512
}
513513
const geometry = this.geometryBuilder.finish();
514-
this.fill(this.geometryBuilder.prevFillColor);
514+
if (this.geometryBuilder.prevFillColor) {
515+
this.fill(this.geometryBuilder.prevFillColor);
516+
} else {
517+
this.noFill();
518+
}
515519
this.geometryBuilder = undefined;
516520
return geometry;
517521
}

test/unit/type/p5.Font.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ suite('p5.Font', function () {
6060
expect(pt.y).not.toBeNaN();
6161
}
6262
});
63+
64+
test('simplifies collinear points', async () => {
65+
const font = await myp5.loadFont(fontFile);
66+
myp5.textSize(50);
67+
const pts = font.textToPoints('T', 0, 0);
68+
const simplifiedPts = font.textToPoints('T', 0, 0, { simplifyThreshold: Math.PI * 0.01 });
69+
expect(pts.length).toBeGreaterThan(simplifiedPts.length);
70+
});
6371
});
6472
});
6573

test/unit/visual/cases/webgl.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,24 @@ visualSuite('WebGL', function() {
673673
screenshot();
674674
});
675675
});
676+
677+
visualSuite('texture()', () => {
678+
visualTest('on a rect', async (p5, screenshot) => {
679+
p5.createCanvas(50, 50, p5.WEBGL);
680+
const tex = await p5.loadImage('/unit/assets/cat.jpg');
681+
p5.texture(tex);
682+
p5.texture(tex);
683+
p5.rect(-20, -20, 40, 40);
684+
screenshot();
685+
});
686+
687+
visualTest('on a rect with rounded corners', async (p5, screenshot) => {
688+
p5.createCanvas(50, 50, p5.WEBGL);
689+
const tex = await p5.loadImage('/unit/assets/cat.jpg');
690+
p5.texture(tex);
691+
p5.texture(tex);
692+
p5.rect(-20, -20, 40, 40, 10);
693+
screenshot();
694+
});
695+
});
676696
});
5.13 KB
Loading

0 commit comments

Comments
 (0)