For AI agents: a documentation index is available at /llms.txt. Markdown versions of all pages are available by appending .md to any URL.
Marker lifecycle animations: enter, idle, and exit
This example demonstrates how to configure lifecycle animations on MapTiler Marker in SDK v5 across three phases:
- Enter animations: Play when a marker is added to the map. Built-in presets include
drop,grow,pop,bounce, andfade, or a custom function. - Idle animations: Continuous looped animations while the marker stays on the map. Built-in presets include
pulsescale,ring,pulseopacity, andbounce, or a custom function. - Exit animations: Play when a marker is removed from the map before it leaves the DOM. Built-in presets include
fade,shrink,pop, andexplode, or a custom function. - Lifecycle events: Markers emit events across each phase (
enteranimationstart,enteranimationend,idleanimationiteration,exitanimationstart,exitanimationend) to coordinate application logic.
Use the dropdown controls to choose presets for each phase, then click Add Marker to test the combination or Remove to trigger the exit animation. You can also click any showcase marker directly to remove it with its exit animation.
NPM module setup
npm install --save @maptiler/sdk
import { Map, MapStyle, config, Marker } from "@maptiler/sdk";
import "@maptiler/sdk/dist/maptiler-sdk.css";
config.apiKey = "YOUR_MAPTILER_API_KEY";
const CENTER = [14.42, 50.08];
const SPACING = 0.0036;
const map = new Map({
container: "map",
style: MapStyle.STREETS,
center: CENTER,
zoom: 15,
});
const statusEl = document.getElementById("status");
function logEvent(text) {
statusEl.textContent = text;
}
function addCaption(map, lngLat, text) {
const captionEl = document.createElement("div");
captionEl.textContent = text;
captionEl.style.font = "700 10px system-ui, -apple-system, sans-serif";
captionEl.style.letterSpacing = "0.03em";
captionEl.style.color = "rgba(0, 0, 0, 0.75)";
captionEl.style.background = "rgba(255, 255, 255, 0.85)";
captionEl.style.padding = "2px 6px";
captionEl.style.borderRadius = "4px";
captionEl.style.whiteSpace = "nowrap";
captionEl.style.pointerEvents = "none";
captionEl.style.boxShadow = "0 1px 3px rgba(0, 0, 0, 0.1)";
const caption = new Marker({
element: captionEl,
anchor: "top",
offset: [0, 34],
htmlAttributes: { tabindex: "-1" },
});
caption.setLngLat(lngLat);
map.addMarker(caption);
return caption;
}
function customEnter(alpha, marker) {
marker.setScale([alpha, alpha]);
marker.setRotation(360 * (1 - alpha));
marker.getElement().style.opacity = String(alpha);
}
function customExit(alpha, marker) {
marker.setScale([1 - alpha, 1 - alpha]);
marker.setRotation(360 * alpha);
marker.getElement().style.opacity = String(1 - alpha);
}
function customIdle(alpha, marker) {
marker.setRotation(Math.sin(alpha * Math.PI * 2) * 15);
}
function bindLifecycleEvents(marker, label) {
const events = [
"enteranimationstart",
"enteranimationend",
"idleanimationstart",
"idleanimationiteration",
"exitanimationstart",
"exitanimationend",
];
events.forEach((type) => {
marker.on(type, (e) => {
logEvent(`${label}: ${type} (${e.preset || "custom"})`);
});
});
}
let showcaseItems = [];
const dynamicMarkers = [];
function removeMarker(marker) {
const dynIndex = dynamicMarkers.indexOf(marker);
if (dynIndex !== -1) {
dynamicMarkers.splice(dynIndex, 1);
}
const showcaseIndex = showcaseItems.findIndex((item) => item.marker === marker);
if (showcaseIndex !== -1) {
const [item] = showcaseItems.splice(showcaseIndex, 1);
if (item.caption) {
item.caption.remove();
}
}
marker.remove();
}
const showcaseDefinitions = [
{
caption: "PULSESCALE",
color: "#0070F3",
content: "P",
idle: { preset: "pulsescale", iterations: Infinity },
exit: { preset: "fade", duration: 300 },
},
{
caption: "RING",
color: "#10B981",
content: "R",
idle: { preset: "ring", iterations: Infinity },
exit: { preset: "fade", duration: 300 },
},
{
caption: "PULSEOPACITY",
color: "#8B5CF6",
content: "O",
idle: { preset: "pulseopacity", iterations: Infinity },
exit: { preset: "fade", duration: 300 },
},
{
caption: "BOUNCE",
color: "#F59E0B",
content: "B",
idle: { preset: "bounce", iterations: Infinity },
exit: { preset: "fade", duration: 300 },
},
{
caption: "CUSTOM WOBBLE",
color: "#EC4899",
content: "C",
idle: { custom: customIdle, duration: 1500, iterations: Infinity },
exit: { custom: customExit, duration: 500 },
},
];
function initShowcaseMarkers() {
showcaseDefinitions.forEach(({ caption, color, content, idle, exit }, i) => {
const lngLat = [CENTER[0] + (i - 2) * SPACING, CENTER[1]];
const marker = new Marker({
shape: "circle",
size: "l",
innerColor: color,
content,
lngLat,
animations: {
enter: { preset: "drop", duration: 500 + i * 100 },
idle,
exit,
},
});
bindLifecycleEvents(marker, caption);
map.addMarker(marker);
const captionMarker = addCaption(map, lngLat, caption);
marker.getElement().addEventListener("click", (e) => {
e.stopPropagation();
removeMarker(marker);
});
showcaseItems.push({ marker, caption: captionMarker });
});
}
initShowcaseMarkers();
function buildAnimationsFromSelection() {
const enterVal = document.getElementById("select-enter").value;
const idleVal = document.getElementById("select-idle").value;
const exitVal = document.getElementById("select-exit").value;
const animations = {};
if (enterVal === "custom") {
animations.enter = { custom: customEnter, duration: 800 };
} else if (enterVal !== "none") {
animations.enter = { preset: enterVal, duration: 600 };
}
if (idleVal === "custom") {
animations.idle = { custom: customIdle, duration: 1500, iterations: Infinity };
} else if (idleVal !== "none") {
animations.idle = { preset: idleVal, iterations: Infinity };
}
if (exitVal === "custom") {
animations.exit = { custom: customExit, duration: 800 };
} else if (exitVal !== "none") {
animations.exit = { preset: exitVal, duration: 400 };
}
return animations;
}
document.getElementById("btn-add").addEventListener("click", () => {
const centerPos = map.getCenter();
const colors = ["#0070F3", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B", "#06B6D4"];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
const jitterLng = (Math.random() - 0.5) * 0.003;
const jitterLat = (Math.random() - 0.5) * 0.002;
const lngLat = [centerPos.lng + jitterLng, centerPos.lat + jitterLat];
const animations = buildAnimationsFromSelection();
const marker = new Marker({
shape: "maptiler",
size: "m",
innerColor: randomColor,
outerColor: "#FFFFFF",
shadow: "medium",
animations,
}).setLngLat(lngLat);
bindLifecycleEvents(marker, `Marker #${dynamicMarkers.length + 1}`);
marker.getElement().addEventListener("click", (e) => {
e.stopPropagation();
removeMarker(marker);
});
map.addMarker(marker);
dynamicMarkers.push(marker);
});
document.getElementById("btn-remove").addEventListener("click", () => {
if (dynamicMarkers.length > 0) {
const lastDynamic = dynamicMarkers[dynamicMarkers.length - 1];
removeMarker(lastDynamic);
} else if (showcaseItems.length > 0) {
const lastShowcase = showcaseItems[showcaseItems.length - 1];
removeMarker(lastShowcase.marker);
}
});
document.getElementById("btn-reset").addEventListener("click", () => {
while (dynamicMarkers.length > 0) {
const m = dynamicMarkers.pop();
m.remove();
}
while (showcaseItems.length > 0) {
const item = showcaseItems.pop();
if (item.caption) {
item.caption.remove();
}
item.marker.remove();
}
initShowcaseMarkers();
logEvent("Showcase markers reset");
});
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex" />
<title>Marker lifecycle animations</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<div id="map"></div>
<details class="panel" open>
<summary class="panel-header">
<span>Lifecycle Controls</span>
<span class="toggle-icon">▼</span>
</summary>
<div class="panel-content">
<div class="panel-desc">
Test presets or click any marker to trigger its exit animation.
</div>
<div class="panel-row">
<label for="select-enter">Enter:</label>
<select id="select-enter">
<option value="drop" selected>drop</option>
<option value="grow">grow</option>
<option value="pop">pop</option>
<option value="bounce">bounce</option>
<option value="fade">fade</option>
<option value="custom">custom (spin)</option>
<option value="none">none</option>
</select>
</div>
<div class="panel-row">
<label for="select-idle">Idle:</label>
<select id="select-idle">
<option value="pulsescale" selected>pulsescale</option>
<option value="ring">ring</option>
<option value="pulseopacity">pulseopacity</option>
<option value="bounce">bounce</option>
<option value="custom">custom (wobble)</option>
<option value="none">none</option>
</select>
</div>
<div class="panel-row">
<label for="select-exit">Exit:</label>
<select id="select-exit">
<option value="fade" selected>fade</option>
<option value="shrink">shrink</option>
<option value="pop">pop</option>
<option value="explode">explode</option>
<option value="custom">custom (spin)</option>
<option value="none">none</option>
</select>
</div>
<div class="panel-buttons">
<button id="btn-add">Add</button>
<button id="btn-remove" class="secondary">Remove</button>
<button id="btn-reset" class="secondary">Reset</button>
</div>
<div id="status" class="panel-status">Ready</div>
</div>
</details>
<script type="module" src="./main.js"></script>
</body>
</html>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
details.panel {
position: absolute;
top: 10px;
left: 10px;
z-index: 10;
background: rgba(255, 255, 255, 0.95);
border-radius: 6px;
padding: 8px 10px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
font-family:
system-ui,
-apple-system,
sans-serif;
font-size: 11px;
width: 210px;
}
details.panel summary {
cursor: pointer;
user-select: none;
list-style: none;
display: flex;
align-items: center;
justify-content: space-between;
font-weight: 600;
font-size: 12px;
color: #0f172a;
}
details.panel summary::-webkit-details-marker {
display: none;
}
details.panel[open] summary {
margin-bottom: 6px;
padding-bottom: 4px;
border-bottom: 1px solid #e2e8f0;
}
.toggle-icon {
font-size: 10px;
color: #64748b;
transition: transform 0.2s ease;
}
details:not([open]) .toggle-icon {
transform: rotate(-90deg);
}
.panel-content {
display: flex;
flex-direction: column;
gap: 5px;
}
.panel-desc {
color: #64748b;
font-size: 10.5px;
line-height: 1.3;
}
.panel-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 6px;
}
.panel-row label {
font-weight: 500;
font-size: 11px;
color: #1e293b;
}
.panel select {
padding: 2px 4px;
border: 1px solid #cbd5e1;
border-radius: 4px;
font-size: 11px;
background: #f8fafc;
cursor: pointer;
width: 120px;
}
.panel-buttons {
display: flex;
gap: 4px;
margin-top: 2px;
}
.panel button {
background: #0070f3;
color: #fff;
border: none;
border-radius: 4px;
padding: 4px 6px;
cursor: pointer;
font-size: 11px;
font-weight: 500;
transition: background 0.2s;
flex: 1;
}
.panel button:hover {
background: #0051b3;
}
.panel button.secondary {
background: #f1f5f9;
color: #334155;
border: 1px solid #cbd5e1;
}
.panel button.secondary:hover {
background: #e2e8f0;
}
.panel-status {
font-size: 10px;
color: #64748b;
background: #f8fafc;
padding: 3px 5px;
border-radius: 4px;
border: 1px solid #e2e8f0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Related examples
Marker animations and states
ExamplesCreate animated markers with interactive UI states and transitions.
Marker shapes and customization
ExamplesConfigure and create customizable, draggable markers in real-time with custom shapes, embedded letters, emojis, sizes, colors, and shadows.