Skip to content

[p5.js 2.0+ Bug Report]: strands breaking on arrow functions with parameters #9180

Description

@davepagurek

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.2

Web browser and version

All

Operating system

All

Steps to reproduce this

If you are using instance mode, you have to pass in a second parameter to p5.strands containing whatever outside context you need in the shader. This gets passed back into the function you pass in, so you add an argument to your function to collect the data from the outside world. However, this currently throws an error, I assume because a regex doesn't handle param => { ... } correctly.

new p5(p => {
  let gradientShader

  p.setup = function() {
    p.createCanvas(400, 400, p.WEBGL)
    gradientShader = p.buildMaterialShader((param) => {
      let { p } = param
      let pos = p.sharedVec4()
  
      p.worldInputs.begin();
      pos = [p.worldInputs.position/200, 1];
      p.worldInputs.end();
      
      p.pixelInputs.begin();
      p.pixelInputs.color = pos;
      p.pixelInputs.end();
    }, { p })
  }
  
  p.draw = function() {
    p.clear()
    p.shader(gradientShader)
    p.noStroke()
    p.plane(p.width, p.height)
  }
})

Live: https://editor.p5js.org/davepagurek/sketches/k64e4h1LA

Notably, this works if you:

  • Use function(param) { ... }
  • Use an arrow function with no args (e.g. in global mode)
  • Destructure parameters directly in the parentheses instead of in the function body, e.g. ({ p }) => { ... } in the example above.

I believe we have tests that do some of the above, but I guess not this particular case.

Activity

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

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions