You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[p5.js 2.0+ Bug Report]: textAlign() with an invalid horizontal value draws nothing in 2D but falls back to LEFT in WEBGL #9356
2.3.4 (Web Editor). Source checked on main at 51eefe3d0, and the relevant code is the same.
Web browser and version
156.0.8078.48 (Official Build) (arm64)
Operating system
MacOS GoldenGate
Steps to reproduce this
Steps to reproduce this
textAlign() saves any string as the horizontal alignment. When the value isn't LEFT, CENTER or RIGHT, the 2D and WEBGL renderers handle it differently. An easy way to hit this is textAlign(TOP), which passes a vertical constant into the horizontal slot.
2D
WEBGL
textAlign(LEFT) (control)
text drawn at left
text drawn at left
textAlign(TOP)
nothing drawn
text drawn at left
Controls (textAlign(LEFT)), 2D then WEBGL:
textAlign(TOP), 2D then WEBGL:
With FES on, both renderers show the same warning:
🌸 p5.js says: Expected constant (please refer to documentation for allowed values) at the first parameter, but received top in textAlign().
So the user is warned, but what happens next depends on the renderer.
With p5.disableFriendlyErrors = true, the 2D sketch draws nothing and the p5 console is empty. The only message is the browser's CanvasTextAlign warning in DevTools, which doesn't point to textAlign():
Sketch:
// p5.disableFriendlyErrors = true; // uncomment for the FES-off runletfont;asyncfunctionsetup(){createCanvas(200,100);// or createCanvas(200, 100, WEBGL)font=awaitloadFont('Arial.ttf');// translate(-width / 2, -height / 2); // WEBGL onlybackground(220);textFont(font);textSize(20);textAlign(TOP);// LEFT for the controltext('hello',20,50);}
Why they differ (from reading the source). Both renderers work out the x position with a switch (textAlign):
src/core/p5.Renderer3D.js:1891–1909 has default: adjustedX = x, so an unknown value falls back to LEFT.
src/core/p5.Renderer2D.js:942–956 has no default:. That leaves adjustedX as undefined, and it gets passed to fillText() as the x coordinate, so nothing is drawn.
The shared helper _xAlignOffset (src/type/textCore.js:2103) also has default: return 0, so the 2D renderer is the only one without a fallback.
Possible fix: add default: adjustedX = x; to the 2D switch so both renderers fall back to LEFT, plus a unit test.
Activity