Select a color

Change Color

This example builds a production-grade color picker on top of ease360° — using the fade and loading flag built into changeFrames(), with a loader and responsive breakpoints.

In this example you will learn the following:

Frame sets per variant

Each color variant lives in its own Cloudinary folder. A single makeFrames() helper builds the URL array for any color at any width — swap the color name, get a different variant.

Important: when you provide sourceWidth and sourceHeight, they must match the delivered image dimensions — what Cloudinary actually sends to the browser — not the master. If you request w_540 in the URL, set sourceWidth: 540. If you omit both, ease360° probes the first frame automatically and reads the correct dimensions — but see Ex 1 — sourceWidth & sourceHeight for the speed and CORS trade-offs before deciding.

const cdn = {
    cloud: 'YOURCLOUDID'
    // No version string — omitting it tells Cloudinary to always serve the
    // latest uploaded asset. Re-upload any frame and it's live immediately.
};

// Frames named teapot_{color}_1.jpg → teapot_{color}_36.jpg — one-based, no padding.
const makeFrames = (color, width) =>
    Array.from({ length: 36 }, (_, i) =>
        `https://res.cloudinary.com/${cdn.cloud}/image/upload/w_${width},q_auto,f_webp/teapot_${color}_${i + 1}.jpg`
    );

const variantFrames = {
    green:  makeFrames('green',  540),
    blue:   makeFrames('blue',   540),
    orange: makeFrames('orange', 540)
};

const myEase360 = ease360('#myEase360', {
    frames:         variantFrames.green,
    sourceWidth:    540,   // optional — pixel width Cloudinary delivers at w_540 (not the master)
    sourceHeight:   540,   // optional — omit to auto-detect; see Ex 1 — sourceWidth & sourceHeight for speed/CORS trade-offs
    backgroundSize: 'cover-center',
    preloadSmart:   true,
    progressUpdate: () => onProgress()
});

changeFrames() — the swap, with a fade

changeFrames() (and changeFramesResponsive(), covered below) waits for the physics engine to stop before swapping — it won't cut off a spin in progress. Pass an options object as the last argument and ease360° does the rest: it keeps the current picture on screen as a snapshot, loads the new set behind it, and fades the snapshot out only once the new frames are loaded and drawn. There is never a black frame, and the fade can't start early.

myEase360.changeFrames(frames, {
    fade:        500,   // fade the old picture out over 500ms (true = 500ms)
    minLoadTime: 800,   // keep the old picture and loading state up at least this long
    loadStart:   () => showSpinner(),
    loaded:      () => hideSpinner()
});

The options, all optional:

The loading flag

From the moment a swap starts until the new set is loaded, myEase360.isLoading() returns true (it reads states.loading). The status callback also reports "loading" and then "loaded", so a UI can disable controls or show a state without any options at all. This works with or without fade.

While the flag is up the viewer is locked: a swap stops any spin that is coasting or any angleTo() tween, and drag, angleTo(), angleStep() and spinOver() are ignored until the new set is in. Otherwise a visitor could spin through frames that are half old and half new. myEase360.isLocked() reports it, and lock: false leaves the viewer live during the swap.

It is also why progressUpdate needs one line while a swap is running: the swap raises progress from 0% to 100% like the first load did, but the loaded callback is the right place to hide the spinner for a swap.

const onProgress = () => {
    // A color swap reports through its own `loaded` callback
    if (myEase360.isLoading()) return;

    // ...first-load progress as in the earlier examples
};

Failed loads

A frame that 404s, or a set that is still not in after timeout milliseconds (default 15000), fails the swap. By default ease360° then puts the previous frames back behind the old picture, which stays up the whole time, so the visitor never sees a half-loaded color. Your error callback is called first, to update the UI, and reverted is called once the old frames are back.

myEase360.changeFrames(frames, {
    fade:    500,
    timeout: 8000,                      // give up after 8s
    error:   (info) => {
        // info.reason: 'load' (info.file is the frame that failed) or 'timeout'
        // info.reverting: true when the previous frames are being put back
        // info.revertFailed: true if even the previous frames would not load
        showMessage('Could not load that color.');
    },
    reverted: () => hideSpinner()
});

With revert: false the old frames are not put back: the old picture fades out and error is left to decide, for instance by calling changeFrames() again to retry. The status callback reports "error" for a failed swap.

Wiring the swatches

Keep a map of which colors have been loaded at least once. The first visit to a color shows the spinner and holds for MIN_MS; a color that has been loaded before comes from the browser's cache, so it just fades.

// false = never loaded, true = loaded at least once
const colorLoaded = { green: true, blue: false, orange: false };
const MIN_MS = 1150;
let activeColor = 'green';

// Marks a swatch active
const selectColor = (key) => {
    activeColor = key;
    document.querySelectorAll('.swatch').forEach(s => s.classList.remove('active'));
    document.querySelector(`.swatch-${key}`)?.classList.add('active');
};

const showSpinner = () => {
    const loading = document.querySelector('.loading');
    loading.classList.remove('opacity0');
    loading.style.zIndex = '1000';
};

const hideSpinner = () => {
    const loading = document.querySelector('.loading');
    loading.classList.add('opacity0');
    setTimeout(() => { loading.style.zIndex = '0'; }, 700); // matches the CSS fade
};

const changeVariant = (key) => {
    if (key === activeColor) return;
    const frames = variantFrames[key];
    if (!frames) return;

    const previous = activeColor;
    selectColor(key);   // marks the swatch active

    const firstVisit = !colorLoaded[key];
    colorLoaded[key] = true;

    myEase360.changeFrames(frames, {
        fade:        500,
        minLoadTime: firstVisit ? MIN_MS : 0,
        loadStart:   firstVisit ? showSpinner : undefined,
        loaded:      hideSpinner,
        error:       () => {                  // the previous frames are put back for you
            colorLoaded[key] = false;
            selectColor(previous);            // switch the swatch back
            hideSpinner();
        }
    });
};

// Wire swatches — mousedown prevents focus bleed onto the swatch
document.querySelectorAll('.swatch').forEach(swatch => {
    swatch.addEventListener('mousedown', (e) => {
        e.preventDefault();
        changeVariant(swatch.dataset.color);
    });
});

The handler above swaps a single frame set with changeFrames(). The viewer on this page is set up with three responsive sets per color, so its handler calls changeFramesResponsive() with all three instead, and selectColor() also points currentFrames at the chosen color's sets. Everything else, including the options, the first-visit logic and the error handling, is the same. See the next section, and view the page source for the full version.

Swapping responsive sets

This example uses three breakpoints — desktop, tablet, and mobile — each with its own frame size. changeFramesResponsive() updates all three slots simultaneously when a swatch is clicked. Pass an array of frame arrays in the exact same order as the responsive init array.

// Each color has frames at three sizes matching the responsive array order
const framesGreen  = {
    1920: makeFrames('green',  540), // desktop  > 1024px — 540px frames
    1024: makeFrames('green',  400), // tablet   641–1024px — 400px frames
    640:  makeFrames('green',  320)  // mobile   ≤ 640px — 320px frames
};
// Same structure for blue and orange...

let currentFrames = framesGreen;

const myEase360 = ease360('#myEase360', {
    responsive: [
        { breakpoint: 1920, frames: currentFrames[1920], sourceWidth: 540, sourceHeight: 540, flex: { w: true } },
        { breakpoint: 1024, frames: currentFrames[1024], sourceWidth: 400, sourceHeight: 400, flex: { w: true } },
        { breakpoint: 640,  frames: currentFrames[640],  sourceWidth: 320, sourceHeight: 320, flex: { w: true } }
    ]
});

// On swatch click — swap all three breakpoints simultaneously, with the same
// options as changeFrames(). The optional second argument lists the delivered size of
// each set, for sets that differ from the original: [{ sourceWidth, sourceHeight }, ...]
currentFrames = framesBlue;
myEase360.changeFramesResponsive(
    [currentFrames[1920], currentFrames[1024], currentFrames[640]],
    { fade: 500, minLoadTime: 1150, loadStart: showSpinner, loaded: hideSpinner }
);