Skip to content

Add parameter validation to WebGL light functions using Friendly Error System #9007

Description

@NAME-ASHWANIYADAV

Increasing access

Better parameter validation makes p5.js more beginner-friendly. When a new user calls ambientLight(), directionalLight(), pointLight(), spotLight(), lightFalloff(), specularColor(), or noLights() with incorrect arguments, they currently get cryptic runtime errors or silent failures. Adding friendly error messages through p5._friendlyError()
helps users understand what went wrong and how to fix it - directly supporting p5.js's mission of making creative coding accessible to everyone, including those new to programming.

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

Feature enhancement details

Seven WebGL light functions in src/webgl/light.js have commented-out parameter validation (// p5._validateParameters(...)) and TODO comments requesting parameter checks (//@TODO: check parameters number).

Affected functions:

  1. ambientLight (line 176) - commented-out validation
  2. specularColor (line 407) - commented-out validation
  3. directionalLight (line 587) - commented-out validation + @todo
  4. pointLight (line 813) - commented-out validation + @todo
  5. lightFalloff (line 1069) - commented-out validation
  6. spotLight (line 1289) - commented-out validation
  7. noLights (line 1352) - commented-out validation

Additionally, the renderer-level implementations in Renderer3D.prototype.lightFalloffand Renderer3D.prototype.spotLight use console.warn() instead of p5._friendlyError(), which is inconsistent with the Friendly Error System.

Proposed changes:

  • Replace commented-out p5._validateParameters() with argument count/type checks using p5._friendlyError()
  • Convert renderer-level console.warn() calls to p5._friendlyError()
  • Add unit tests for all validation cases
  • Preserve chainability (return this) on validation errors

I have an implementation ready with 31 new tests (all passing) and would be happy
to submit a PR

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions