Feature Move Animation
Runnable OpenLayers example: Feature Move Animation. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
示例:要素位移动画
要点:按线要素长度换算进度,requestAnimationFrame 驱动点要素沿路线移动
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>要素位移动画</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{padding:4px 10px;border:1px solid #d5dae2;background:#fff;border-radius:4px;cursor:pointer}
.bar input[type=range]{width:120px}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<button id="toggle">暂停</button>
<label for="sp">速度 <b id="spv">1.0×</b></label>
<input type="range" id="sp" min="0.2" max="3" step="0.1" value="1">
<span>用时 <b id="eta">—</b></span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var fromLonLat = ol.proj.fromLonLat;
var path = [
[116.286, 39.905], [116.320, 39.930], [116.352, 39.905], [116.386, 39.935],
[116.418, 39.910], [116.452, 39.940], [116.486, 39.912], [116.520, 39.880]
].map(function (p) { return fromLonLat(p); });
var route = new ol.geom.LineString(path);
var routeFeature = new ol.Feature({ geometry: route });
routeFeature.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({ color: 'rgba(31,95,208,0.85)', width: 4 })
}));
var car = new ol.Feature({ geometry: new ol.geom.Point(path[0]) });
car.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 8,
fill: new ol.style.Fill({ color: '#b42318' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 2.5 })
})
}));
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }),
new ol.layer.Vector({ source: new ol.source.Vector({ features: [routeFeature, car] }) })
],
view: new ol.View({ center: fromLonLat([116.40, 39.915]), zoom: 12 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
/* 路线长度按地图单位(米)算,速度用它换算成进度增量 */
var length = route.getLength();
var speedMps = 24; /* 基础速度 24 m/s 左右,便于肉眼观察 */
var factor = 1;
var progress = 0;
var running = true;
var last = 0;
function frame(ts) {
if (!last) last = ts;
var dt = Math.min(0.1, (ts - last) / 1000);
last = ts;
if (running) {
progress += (speedMps * factor * dt) / length;
if (progress > 1) progress -= 1;
car.getGeometry().setCoordinates(route.getCoordinateAt(progress));
}
var remain = (1 - progress) * length / (speedMps * factor);
document.getElementById('eta').textContent = remain.toFixed(1) + ' 秒到终点';
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
document.getElementById('toggle').addEventListener('click', function () {
running = !running;
this.textContent = running ? '暂停' : '继续';
last = 0;
});
document.getElementById('sp').addEventListener('input', function () {
factor = Number(this.value);
document.getElementById('spv').textContent = factor.toFixed(1) + '×';
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.