Skip to content
Add an Animated Icon to the Map

Add an Animated Icon to the Map

Create a pulsing dot icon using a custom animated image.

const API_KEY = 'toursprung';

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

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

    const size = 100;
    const pulsingDot = {
        width: size,
        height: size,
        data: new Uint8Array(size * size * 4),
        onAdd() {
            const canvas = document.createElement('canvas');
            canvas.width = size;
            canvas.height = size;
            this.context = canvas.getContext('2d');
        },
        render() {
            const duration = 1000;
            const t = (performance.now() % duration) / duration;
            const radius = (size / 2) * 0.3;
            const outerRadius = (size / 2) * 0.7 * t + radius;
            const ctx = this.context;

            ctx.clearRect(0, 0, size, size);

            ctx.beginPath();
            ctx.arc(size / 2, size / 2, outerRadius, 0, Math.PI * 2);
            ctx.fillStyle = `rgba(56, 135, 190, ${1 - t})`;
            ctx.fill();

            ctx.beginPath();
            ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2);
            ctx.fillStyle = 'rgba(56, 135, 190, 1)';
            ctx.strokeStyle = 'white';
            ctx.lineWidth = 2 + 4 * (1 - t);
            ctx.fill();
            ctx.stroke();

            this.data = ctx.getImageData(0, 0, size, size).data;
            map.triggerRepaint();
            return true;
        }
    };

    map.on('load', () => {
        map.addImage('pulsing-dot', pulsingDot, { pixelRatio: 2 });

        map.addSource('dot', {
            type: 'geojson',
            data: { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] } }
        });

        map.addLayer({
            id: 'dot-layer',
            type: 'symbol',
            source: 'dot',
            layout: { 'icon-image': 'pulsing-dot', 'icon-allow-overlap': true }
        });
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add an Animated Icon to the Map – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Create a pulsing dot icon using a custom animated image." />
    <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 map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        attributionControl: { compact: false }
    });

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

    const size = 100;
    const pulsingDot = {
        width: size,
        height: size,
        data: new Uint8Array(size * size * 4),
        onAdd() {
            const canvas = document.createElement('canvas');
            canvas.width = size;
            canvas.height = size;
            this.context = canvas.getContext('2d');
        },
        render() {
            const duration = 1000;
            const t = (performance.now() % duration) / duration;
            const radius = (size / 2) * 0.3;
            const outerRadius = (size / 2) * 0.7 * t + radius;
            const ctx = this.context;

            ctx.clearRect(0, 0, size, size);

            ctx.beginPath();
            ctx.arc(size / 2, size / 2, outerRadius, 0, Math.PI * 2);
            ctx.fillStyle = `rgba(56, 135, 190, ${1 - t})`;
            ctx.fill();

            ctx.beginPath();
            ctx.arc(size / 2, size / 2, radius, 0, Math.PI * 2);
            ctx.fillStyle = 'rgba(56, 135, 190, 1)';
            ctx.strokeStyle = 'white';
            ctx.lineWidth = 2 + 4 * (1 - t);
            ctx.fill();
            ctx.stroke();

            this.data = ctx.getImageData(0, 0, size, size).data;
            map.triggerRepaint();
            return true;
        }
    };

    map.on('load', () => {
        map.addImage('pulsing-dot', pulsingDot, { pixelRatio: 2 });

        map.addSource('dot', {
            type: 'geojson',
            data: { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] } }
        });

        map.addLayer({
            id: 'dot-layer',
            type: 'symbol',
            source: 'dot',
            layout: { 'icon-image': 'pulsing-dot', 'icon-allow-overlap': true }
        });
    });
</script>
</body>
</html>