Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[p5.js 2.0+ Bug Report]: textAlign() with an invalid horizontal value draws nothing in 2D but falls back to LEFT in WEBGL

未关闭 适合新手
#9,356 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
javascript

调研方向

阅读 src/core/p5.Renderer2D.js(第942-956行)中的水平对齐 switch,并添加 default: adjustedX = x; 以匹配 Renderer3D 的 fallback。在 typography 测试套件中为 textAlign 使用一个无效的水平值添加单元测试。当 2D 画布在 LEFT 绘制文本且新测试通过时完成。

由索引模型根据 Issue 内容生成。

描述

Area:Typography Area:WebGL p5.js 2.0+
Most appropriate sub-area of p5.js?
  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)
p5.js version

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:

Image Image

textAlign(TOP), 2D then WEBGL:

Image Image

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():

Image Image

Sketch:

// p5.disableFriendlyErrors = true;          // uncomment for the FES-off run
let font;
async function setup() {
  createCanvas(200, 100);                    // or createCanvas(200, 100, WEBGL)
  font = await loadFont('Arial.ttf');
  // translate(-width / 2, -height / 2);     // WEBGL only
  background(220);
  textFont(font);
  textSize(20);
  textAlign(TOP);                            // LEFT for the control
  text('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.

I'd like to fix this myself if approved.

主要语言
JavaScript
星标
24.3k
派生
3.9k
平均合并
3 天 19 小时
30 天内合并 PR
48

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

processing/p5.js 的其他 Issue

查看 processing/p5.js 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。