Skip to content

Animate a series of images

Use a series of image sources to create an animation.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Animate a series of images</title>
    <meta property="og:description" content="Use a series of image sources to create an animation." />
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/trackasia-gl.css" />
    <script src="https://unpkg.com/[email protected]/dist/trackasia-gl.js"></script>
    <style>
        body { margin: 0; padding: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>
<script>
    const map = new trackasiagl.Map({
        container: 'map',
        style: 'https://maps.track-asia.com/styles/v2/streets.json?key=public_key',
        maxZoom: 6,
        minZoom: 3,
        zoom: 4,
        center: [107.595111,16.577234]
    });

    var frameCount = 5;
    var currentImage = 0;

    function getPath() {
        return (
            'https://docs.track-asia.com/assets/radar' + currentImage + '.gif'
        );
    }

    map.on('load', () => {
        map.addSource('radar', {
            type: 'image',
            url: getPath(),
            coordinates: [[105.484359,16.071151],[116.301964,16.071151],[116.301964,6.9526],[105.484359,6.9526]]
        });
        map.addLayer({
            id: 'radar-layer',
            'type': 'raster',
            'source': 'radar',
            'paint': {
                'raster-fade-duration': 0
            }
        });

        setInterval(() => {
            currentImage = (currentImage + 1) % frameCount;
            map.getSource('radar').updateImage({url: getPath()});
        }, 200);
    });
</script>

</body>
</html>