Public
Edited
Nov 24, 2022
Fork of Scrubber
Importers
Insert cell
Insert cell
Insert cell
viewof i = Scrubber(numbers)
Insert cell
numbers = Array.from({length: 256}, (_, i) => i)
Insert cell
md`The current value of *i* is ${i}.`
Insert cell
Insert cell
Insert cell
Insert cell
Scrubber(numbers, {autoplay: false})
Insert cell
Insert cell
Scrubber(numbers, {loop: false})
Insert cell
Insert cell
Scrubber(numbers, {loop: false, alternate: true})
Insert cell
Insert cell
Scrubber(["red", "green", "blue"], {delay: 1000})
Insert cell
Insert cell
Scrubber(numbers, {initial: numbers.length - 1, loopDelay: 1000})
Insert cell
Insert cell
dates = Array.from({length: 365}, (_, i) => {
const date = new Date(2019, 0, 1);
date.setDate(i + 1);
return date;
})
Insert cell
viewof date = Scrubber(dates, {
autoplay: false,
format: date => date.toLocaleString("en", {month: "long", day: "numeric"})
})
Insert cell
Insert cell
Insert cell
function Scrubber(values, {
format = value => value,
initial = 0,
delay = null,
autoplay = true,
loop = true,
loopDelay = null,
alternate = false
} = {}) {
values = Array.from(values);
const form = html`<form style="font: 12px var(--sans-serif); font-variant-numeric: tabular-nums; display: flex; height: 25px; align-items: center;">
<button name=b type=button style="margin-right: 0.4em; width: 180px;"></button>
<label style="display: flex; align-items: center;">
<input id="mapScrubber" name=i type=range min=0 max=${values.length - 1} value=${initial} step=1 style="width: 300px;">
<output id="mapScrubberText" name=o style="margin-left: 0.4em;"></output>
</label>
</form>`;
let frame = null;
let timer = null;
let interval = null;
let direction = 1;
function start() {
form.b.textContent = "Pause";
if (delay === null) frame = requestAnimationFrame(tick);
else interval = setInterval(tick, delay);
}
function stop() {
form.b.textContent = "Play 70 years of typhoons";
if (frame !== null) cancelAnimationFrame(frame), frame = null;
if (timer !== null) clearTimeout(timer), timer = null;
if (interval !== null) clearInterval(interval), interval = null;
}
function running() {
return frame !== null || timer !== null || interval !== null;
}
function tick() {
if (form.i.valueAsNumber === (direction > 0 ? values.length - 1 : direction < 0 ? 0 : NaN)) {
if (!loop) return stop();
if (alternate) direction = -direction;
if (loopDelay !== null) {
if (frame !== null) cancelAnimationFrame(frame), frame = null;
if (interval !== null) clearInterval(interval), interval = null;
timer = setTimeout(() => (step(), start()), loopDelay);
return;
}
}
if (delay === null) frame = requestAnimationFrame(tick);
step();
}
function step() {
form.i.valueAsNumber = (form.i.valueAsNumber + direction + values.length) % values.length;
form.i.dispatchEvent(new CustomEvent("input", {bubbles: true}));
}
form.i.oninput = event => {
if (event && event.isTrusted && running()) stop();
form.value = values[form.i.valueAsNumber];
form.o.value = format(form.value, form.i.valueAsNumber, values);
};
form.b.onclick = () => {
if (running()) return stop();
direction = alternate && form.i.valueAsNumber === values.length - 1 ? -1 : 1;
form.i.valueAsNumber = (form.i.valueAsNumber + direction) % values.length;
form.i.dispatchEvent(new CustomEvent("input", {bubbles: true}));
start();
};
form.i.oninput();
if (autoplay) start();
else stop();
Inputs.disposal(form).then(stop);
return form;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more