createTimeSliders
← All Butter documentationParameters
createTimeSliders(trackName: string, ranges: Array<{ start: number, end: number }>, options?: { fixedLength?: boolean }): TimeSliderstrackNameThe name of the track on the timeline.
rangesOne object for each event pill on the track, in order, each with
startandendin milliseconds from the start of the component. The pills cannot overlap. This is what the pills start as: where the user moves them is saved and wins. The length of pills that can only be moved always comes from your code, so changing it in your code applies right away; withfixedLength: falsethe lengths the user chose are kept too.optionsfixedLength: falselets the user drag the ends of every pill, as well as move them. It istrueif you leave it out, and your code decides it each time it runs.
createTimeSliders is createTimeSlider for several events that belong together, such as subtitles that come in one after another. Each one is an event pill, and they are all shown on one track of the timeline, instead of a track each.
Here, a title has two subtitles. The pill for each controls when it fades in:
let subtitles
function setup() {
createCanvas(400, 200)
subtitles = createTimeSliders('Subtitle fades', [
{ start: 800, end: 1300 },
{ start: 2000, end: 2500 },
])
}
function draw() {
clear()
fill(255)
textAlign(CENTER, CENTER)
textSize(36)
text('Big Sale', width / 2, height / 2 - 40)
const lines = ['Everything must go', 'Today only']
textSize(20)
subtitles.value().forEach(({ start, end }, i) => {
// Each subtitle goes from clear to opaque over the length of its pill
fill(255, constrain(map(millis(), start, end, 0, 255), 0, 255))
text(lines[i], width / 2, height / 2 + i * 30)
})
}
The .value() is an array with an object for each event pill, in the same order, each with start and end. Times are in milliseconds, so everything in createTimeSlider about units, .changed(), .show() and .hide() applies here too. Butter enforces that the pills are always in order and don't overlap. If the times you pass in, or set with .value(newValue), break that, an error is thrown.
Visible for a span of time
With fixedLength: false in the options, which applies to every event pill, each one can instead stand for how long something is visible, with the fade in and out worked out from its start and end. The user can then drag either end of a pill to show a subtitle for longer or shorter:
let subtitles
function setup() {
createCanvas(400, 200)
subtitles = createTimeSliders(
'Subtitles',
[
{ start: 500, end: 2500 },
{ start: 3000, end: 5000 },
],
{ fixedLength: false },
)
}
function draw() {
clear()
fill(255)
textAlign(CENTER, CENTER)
textSize(36)
text('Big Sale', width / 2, height / 2 - 40)
const lines = ['Everything must go', 'Today only']
const fadeTime = 300
textSize(20)
subtitles.value().forEach(({ start, end }, i) => {
// Fading in over the first 300 milliseconds of the pill, and out over the last
const fadeIn = map(millis(), start, start + fadeTime, 0, 255)
const fadeOut = map(millis(), end - fadeTime, end, 255, 0)
fill(255, constrain(min(fadeIn, fadeOut), 0, 255))
text(lines[i], width / 2, height / 2 + i * 30)
})
}
Changing the number of events
To add or remove event pills, call .value(newValue) with a different number of times. As in createTimeSlider, do this from a .changed() function on another field. Each new pill needs its own start and end, and it gets the same fixedLength as the rest. The pills that are still there keep their times, and pills are removed from the end.
Here, a slider sets how many pulses there are. A new pulse is put half a second after the last one, which may be where the user moved it to:
let count
let pulses
function setup() {
createCanvas(300, 300)
count = createSlider(1, 6, 2)
pulses = createTimeSliders(
'Pulses',
Array.from({ length: count.value() }, (_, i) => ({
start: i * 1000,
end: i * 1000 + 500,
})),
)
count.changed(() => {
// Keep the ones the user has placed, and add or drop from the end
const next = pulses.value().slice(0, count.value())
while (next.length < count.value()) {
const last = next[next.length - 1]
const start = last ? last.end + 500 : 0
next.push({ start, end: start + 500 })
}
pulses.value(next)
})
}
function draw() {
clear()
noFill()
stroke(255)
strokeWeight(6)
for (const { start, end } of pulses.value()) {
if (millis() < start || millis() > end) continue
// A ring that grows over the length of the pill
circle(width / 2, height / 2, map(millis(), start, end, 0, 200))
}
}
A createMultiFieldArray can set the number if you want the user to add and remove them. Here, each caption is an item, and each has an event pill for when it is visible:
let captions
let times
function setup() {
createCanvas(400, 200)
captions = createMultiFieldArray(() => ({
text: createInput('Hello'),
}), { min: 1, max: 5, default: 2 })
times = createTimeSliders(
'Captions',
captions.value().map((_, i) => ({
start: i * 1500,
end: i * 1500 + 1000,
})),
{ fixedLength: false },
)
captions.changed(() => {
// One time for each caption, with a new one placed after the last
const count = captions.value().length
const next = times.value().slice(0, count)
while (next.length < count) {
const last = next[next.length - 1]
const start = last ? last.end + 500 : 0
next.push({ start, end: start + 1000 })
}
times.value(next)
})
}
function draw() {
clear()
textAlign(CENTER, CENTER)
textSize(32)
fill(255)
captions.value().forEach((caption, i) => {
const time = times.value()[i]
// A caption that was just added has no time until its change has run
if (!time) return
if (millis() >= time.start && millis() <= time.end) {
text(caption.text.value(), width / 2, height / 2)
}
})
}
Pills that go past the end of the component are not drawn on the timeline, and the track shows a marker at its right edge saying how many are hidden. The user can drag the end of the component out to reach them, but it is better to keep new ones inside it.
Things to know
- Don't use event pills for entrances or exits that are always pinned to the start of the component, such as several elements that slide in one after another as soon as it begins. There is nothing for the user to retime, and pills they never need to move only clutter the timeline. A speed slider, a
createSliderthat sets how long the animation takes, is enough for those. UsecreateTimeSlidersfor events that happen at moments the user will want to move, like subtitles.