Skip to content
Animate a Series of Images

Animate a Series of Images

Cycle through a series of images to create an animation on the map.

const API_KEY = 'toursprung';

    const frames = [
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar0.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar1.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar2.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar3.gif'
    ];

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [-75.789, 41.874],
        zoom: 5,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        let frameIndex = 0;

        function loadFrame(index) {
            const id = `frame-${index}`;
            if (map.hasImage(id)) return;
            map.loadImage(frames[index], (err, img) => {
                if (err || map.hasImage(id)) return;
                map.addImage(id, img);
            });
        }

        frames.forEach((_, i) => loadFrame(i));

        map.addSource('radar', {
            type: 'image',
            url: frames[0],
            coordinates: [
                [-80.425, 46.437],
                [-71.516, 46.437],
                [-71.516, 37.936],
                [-80.425, 37.936]
            ]
        });

        map.addLayer({ id: 'radar-layer', type: 'raster', source: 'radar', paint: { 'raster-opacity': 0.8 } });

        setInterval(() => {
            frameIndex = (frameIndex + 1) % frames.length;
            map.getSource('radar').updateImage({ url: frames[frameIndex] });
        }, 500);
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate a Series of Images – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Cycle through a series of images to create an animation on the map." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
    <style>
        html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
        #map { width: 100%; height: 100%; }
    </style>
</head>
<body>
<div id="map"></div>
<script>
    const API_KEY = 'toursprung';

    const frames = [
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar0.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar1.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar2.gif',
        'https://maplibre.org/maplibre-gl-js/docs/assets/radar3.gif'
    ];

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [-75.789, 41.874],
        zoom: 5,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        let frameIndex = 0;

        function loadFrame(index) {
            const id = `frame-${index}`;
            if (map.hasImage(id)) return;
            map.loadImage(frames[index], (err, img) => {
                if (err || map.hasImage(id)) return;
                map.addImage(id, img);
            });
        }

        frames.forEach((_, i) => loadFrame(i));

        map.addSource('radar', {
            type: 'image',
            url: frames[0],
            coordinates: [
                [-80.425, 46.437],
                [-71.516, 46.437],
                [-71.516, 37.936],
                [-80.425, 37.936]
            ]
        });

        map.addLayer({ id: 'radar-layer', type: 'raster', source: 'radar', paint: { 'raster-opacity': 0.8 } });

        setInterval(() => {
            frameIndex = (frameIndex + 1) % frames.length;
            map.getSource('radar').updateImage({ url: frames[frameIndex] });
        }, 500);
    });
</script>
</body>
</html>