Experiments >

Code animation: Discrete tween

Experiment #1745th May, 2021by Joshua Nussbaum

This experiment builds on top of svelte’s tweened() store, to provide a store can animate between discrete values.

Svelte’s builtin tweened() works on continuous values. ie. if the store’s value is zero, and later it’s set to 1, it will produce values like 0, 0.1, 0.5, 0.6, 0.71.

But sometimes you have values that are discrete. For example, if you want to toggle between several states ['red', 'green', 'blue']. In this case, we want to animate the index of the array, and return the value at that index.

The API looks like this:

import { tweened } from './discrete-tweened-store'

const store = tweened({values: ['red', 'green', 'blue'], duration: 1000})

// toggle between each value
// total duration is 1000ms, therefore each state lasts 333ms

// we can play it in reverse

// or reset back to index zero





  • Maybe an abstraction can be created on top of this, like “an array of tweens”, so you can run a set of operations on many tweens at once.
  • Another way to accomplish this, may be by making a custom interpolator function.
view all experiments

Stay tuned in

Learn how to add more experimentation to your workflow