createTimeSlider

← All Butter documentation
Parameters
createTimeSlider(trackName: string, range: { start: number, end: number, fixedLength?: boolean }): TimeSlider
  • trackName

    The name of the track on the timeline.

  • range

    start and end are in milliseconds from the start of the component. By default (fixedLength: true) the user can only move the pill, keeping its length; with fixedLength: false they can drag either end too. Your code decides fixedLength each time it runs. start and end are what the pill starts as: where the user moves it is saved and wins. The length of a pill that can only be moved always comes from your code, so changing it in your code applies right away; with fixedLength: false the length the user chose is kept too.

Many things in a component happen at a certain time: a subtitle fades in partway through, a button gets clicked. Instead of writing that time into your code, createTimeSlider puts it on the timeline as a pill the user can drag.

Here is a title with a subtitle that fades in below it. The pill on the timeline controls when the fade happens:

let fade

function setup() {
  createCanvas(400, 200)
  fade = createTimeSlider('Subtitle fade', { start: 800, end: 1400 })
}

function draw() {
  clear()
  fill(255)
  textAlign(CENTER, CENTER)
  textSize(40)
  text('Big Sale', width / 2, height / 2 - 20)

  // The subtitle goes from clear to opaque over the length of the pill
  const { start, end } = fade.value()
  const opacity = constrain(map(millis(), start, end, 0, 255), 0, 255)
  textSize(20)
  fill(255, opacity)
  text('Everything must go', width / 2, height / 2 + 25)
}

start and end are milliseconds from the start of the component. They are not frames, so they mean the same thing whatever frame rate the sketch runs at. The simplest way to use them is to compare them with millis(), as above. If you need a frame number instead, convert it with ms * frameRate() / 1000.

A time slider does not show up in the sidebar: it is only a track on the component's timeline. That also makes it the right choice when something should line up with other things on the timeline, like another component: let the user line the pill up with it instead of hard-coding the time.

Moving and resizing

By default the pill keeps its length and the user can only move it (fixedLength is true if you leave it out). Use that for when you want to give control of when something happens without compromising the speed of the animation at all. Pass fixedLength: false to let the user drag either end of the pill as well, letting them control the speed too. For the fade above, they can then choose how long it lasts, as well as when it starts:

let fade

function setup() {
  createCanvas(400, 200)
  fade = createTimeSlider('Subtitle fade', {
    start: 800,
    end: 1400,
    fixedLength: false,
  })
}

function draw() {
  clear()
  fill(255)
  textAlign(CENTER, CENTER)
  textSize(40)
  text('Big Sale', width / 2, height / 2 - 20)

  // The fade now lasts as long as the pill, which the user can resize
  const { start, end } = fade.value()
  const opacity = constrain(map(millis(), start, end, 0, 255), 0, 255)
  textSize(20)
  fill(255, opacity)
  text('Everything must go', width / 2, height / 2 + 25)
}

Reading and changing the value

.value() returns an object with start and end. Read it in draw each time you need it, since it changes as the user drags the pill. Call .changed() with a function to run it when the user moves the pill.

To change the value from your code, call .value(newValue) from a .changed() function on another field. That is where it belongs: the new value is saved along with the change that caused it. To change just one part, start from what .value() returns, like fade.value({ ...fade.value(), end: 3000 }). Whether the pill can be resized is not part of the value: it is set in the call to createTimeSlider, so changing it in your code applies right away, even if the user has already moved the pill.

Here, a select picks a fast or a slow fade. The pill can still only be moved, but its length follows the select:

let speed
let fade

function setup() {
  createCanvas(400, 200)
  speed = createSelect()
  speed.option('Fast', 'fast')
  speed.option('Slow', 'slow')

  const length = () => (speed.value() === 'fast' ? 300 : 1200)
  fade = createTimeSlider('Subtitle fade', { start: 800, end: 800 + length() })

  speed.changed(() => {
    const { start } = fade.value()
    fade.value({ start, end: start + length() })
  })
}

function draw() {
  clear()
  fill(255)
  textAlign(CENTER, CENTER)
  textSize(40)
  text('Big Sale', width / 2, height / 2 - 20)

  const { start, end } = fade.value()
  const opacity = constrain(map(millis(), start, end, 0, 255), 0, 255)
  textSize(20)
  fill(255, opacity)
  text('Everything must go', width / 2, height / 2 + 25)
}

Use .value() to change the pill. If you worked out the end in draw instead, the pill on the timeline would keep showing the old length. The new length is saved in the same step as the select, so undoing one undoes the other. When a value is being restored (the component loads, or the user undoes or redoes), .changed() functions still run but cannot set values: the new length was saved along with the select, so there is nothing to set again. Using the select's value for the pill's starting length too, as above, keeps it matching when the select starts out on "Slow".

Showing and hiding the track

Call .hide() or .show() to take the track off the timeline or put it back. This lets one component do two things, where only one needs a time. Here, a bouncing ball is either always jumping, with no track, or jumps once at a time the user sets:

let mode
let bounceTime

function setup() {
  createCanvas(300, 300)
  mode = createSelect()
  mode.option('Continuous', 'continuous')
  mode.option('Emphasis', 'emphasis')
  bounceTime = createTimeSlider('Bounce', { start: 1000, end: 1600 })

  // Only emphasis needs a time on the timeline
  mode.changed(() => {
    if (mode.value() === 'emphasis') {
      bounceTime.show()
    } else {
      bounceTime.hide()
    }
  })
}

function draw() {
  clear()
  const { start, end } = bounceTime.value()

  // How far through a bounce it is, from 0 to 1
  let progress
  if (mode.value() === 'continuous') {
    // Always jumping, once every 600 milliseconds
    progress = (millis() % 600) / 600
  } else {
    // Jumping once, over the length of the pill
    progress = constrain(map(millis(), start, end, 0, 1), 0, 1)
  }

  // Up and back down, resting at the start and the end
  const lift = sin(progress * PI) * 80
  noStroke()
  fill('#ff6ec4')
  circle(width / 2, height - 60 - lift, 60)
}

Things to know

  • Don't use a time slider for an entrance or exit that is always pinned to the start of the component, such as a title that slides in as soon as the component begins. There is nothing for the user to retime, and a pill they never need to move only clutters the timeline. A speed slider, a createSlider that sets how long the animation takes, is enough for those. Save time sliders for things that happen at a moment that really varies, like a subtitle that appears partway through, or a click that has to line up with something else.
  • For several times on one track, such as subtitles that come in one after another, use createTimeSliders.
  • Time sliders are not supported inside a createMultiFieldArray item. Use createTimeSliders when you need a variable number of them.

For a longer example, see the Cursor Click example.