resetOnUpdate

← All Butter documentation
Parameters
resetOnUpdate(): this

Chain .resetOnUpdate() onto a field to run setup again whenever that field changes. Use it when the field decides what setup creates, such as how many of something to make. It returns the field, so you can use it in the same line that creates it.

let count
let stars

function setup() {
  createCanvas(300, 200)
  count = createSlider(1, 50, 20).resetOnUpdate()

  // Made again whenever the slider changes. The seed keeps the first ones
  // where they were.
  randomSeed(1)
  stars = Array.from({ length: count.value() }, () => ({
    x: random(width),
    y: random(height),
  }))
}

function draw() {
  clear()
  noStroke()
  fill(255)
  for (const star of stars) {
    circle(star.x, star.y, 6)
  }
}

It does the same thing as .updatesSize(), which is the name to use when the field decides the size of your canvas. Both can go on any field, including a createMultiFieldArray. Fields made with load*, like images, videos and fonts, already run setup again when they change, so on those it changes nothing.

For more info, check out: