Skip to content

[p5.js 2.0+ Bug Report]: TRIANGLES Requires Multiples of 3 #9319

Open

Description

@lewi0622

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 browser and version

No response

Operating system

No response

Steps to reproduce this

Steps:

Creating a custom shape using the beginShape(TRIANGLES) option in v2.0+ requires the number of vertices to be exactly a multiple of 3 or it will hit a runtime error and not draw anything. The error is not a particularly friendly one: TypeError: can't access property "position", v1 is undefined

In p5 V1, extra vertices that could not be wrangled into a triangle (e.g. a shape with 4 vertices) were simply ignored.

I think the V1 method makes a lot more sense, and we should go back to ignoring extra vertices. This would also maintain some continuity for people porting V1 code to V2.

Snippet:

https://editor.p5js.org/lewi0622/sketches/lnZLG0GhN

  beginShape(TRIANGLES);
  vertex(random(width), random(height));
  vertex(random(width), random(height));
  vertex(random(width), random(height));
  //uncommment fourth vertex to see issue in v2+, not in an issue in v1
  // vertex(random(width), random(height));
  endShape();

Activity

  1. changed the title [-][p5.js 2.0+ Bug Report]:[/-] [+][p5.js 2.0+ Bug Report]: TRIANGLES Requires Multiples of 3[/+] on Oct 22, 2026
  2. MYMISERABLEMIND17 commented on Oct 22, 2026

    @MYMISERABLEMIND17

    Hey! I investigated this issue.

    In src/shape/custom_shapes.js, when vertices are added to TRIANGLES (as well as LINES and QUADS), extra vertices that do not form a complete primitive create a trailing primitive with fewer than the required vertex capacity (e.g. 1 or 2 vertices for a triangle). In PrimitiveToPath2DConverter and PrimitiveToVerticesConverter, visitTriangle destructures [v0, v1, v2] = triangle.vertices and accesses v1.position, which throws TypeError: Cannot read properties of undefined (reading 'position') when the vertex count is not a multiple of 3. A similar issue occurs in GeometryBuilder.js where the loop bound in WebGL mode indexed past the end of the vertex array.

    I have implemented guards across:

    1. PrimitiveToPath2DConverter (visitLine, visitTriangle, visitQuad): skips incomplete primitives (vertices.length < capacity).
    2. PrimitiveToVerticesConverter: guards against pushing incomplete contours.
    3. svg_export.js: guards against emitting SVG polygon/line elements for incomplete primitives.
    4. GeometryBuilder.js: updated loop bound i < geometry.vertices.length - 2 for TRIANGLES in WebGL mode to prevent out-of-bounds vertex index access.
    5. Added unit tests in test/unit/core/vertex.js covering TRIANGLES (1, 2, 4 vertices in 2D and WebGL), LINES (1, 3 vertices), and QUADS (1, 3, 5 vertices).

    All tests in test/unit/core/ (19/19 in vertex.js, 290/290 core tests), npm run lint (0 errors), npm run test:types, and npm run build pass cleanly. The branch fix/triangles-modulo-3-9319 is pushed to my fork: https://github.com/MYMISERABLEMIND17/p5.js/tree/fix/triangles-modulo-3-9319

    Could you please assign this issue to me so I can open the PR? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions