You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox虚线动画求助:圆形线帽下步行路径动画异常问题

问题:步行路线单向平滑动画的实现细节排查

我正在尝试通过动画展示单向平滑移动的点来呈现步行路线,目前在实现中遇到了两个关键问题,想确认是否遗漏了实现细节:

  • 使用版本为8.5.0的@react-native-mapbox-gl/maps,通过MapboxGL.LineLayer配合setInterval更新style.lineDasharray来模拟动画,该问题在mapbox-gl-js/v0.34中同样存在
  • 当设置lineCap: 'butt'时,动画表现正常,但路线上会出现1像素的静态线条
  • 切换为lineCap: 'round'时,动画直接失效,部分虚线处于静止状态,推测是库在创建圆形线帽时添加的左右扩展部分干扰了dasharray的动画逻辑

现寻求实现平滑单向动画的可行技巧,以下是复现问题的完整代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.34.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.34.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibHVjYXN3b2oiLCJhIjoiY2l5Nmg4cWU1MDA0ejMzcDJtNHJmZzJkcyJ9.WhcEdTYQH6sSw2pm0RSP9Q';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v9',
    center: [-122.486052, 37.830348],
    zoom: 15
});

map.on('load', function () {

    map.addLayer({
        "id": "route",
        "type": "line",
        "source": {
            "type": "geojson",
            "data": {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "LineString",
                    "coordinates": [
                        [-122.48369693756104, 37.83381888486939],
                        [-122.48348236083984, 37.83317489144141],
                        [-122.48339653015138, 37.83270036637107],
                        [-122.48356819152832, 37.832056363179625],
                        [-122.48404026031496, 37.83114119107971],
                        [-122.48404026031496, 37.83049717427869],
                        [-122.48348236083984, 37.829920943955045],
                        [-122.48356819152832, 37.82954808664175],
                        [-122.48507022857666, 37.82944639795659],
                        [-122.48610019683838, 37.82880236636284],
                        [-122.48695850372314, 37.82931081282506],
                        [-122.48700141906738, 37.83080223556934],
                        [-122.48751640319824, 37.83168351665737],
                        [-122.48803138732912, 37.832158048267786],
                        [-122.48888969421387, 37.83297152392784],
                        [-122.48987674713133, 37.83263257682617],
                    ]
                }
            }
        },
        "layout": {
            //"line-join": "round",
            "line-cap": "round"
        },
        "paint": {
            "line-color": "#888",
            "line-width": 6
        }
    });
  
    var dashLength = 1;
    var gapLength = 13; 
    // We divide the animation up into 40 steps to make careful use of the finite space in LineAtlas    
    var steps = 24;
    // A # of steps proportional to the dashLength are devoted to manipulating the dash
    var dashSteps = steps * dashLength / (gapLength + dashLength);
    // A # of steps proportional to the gapLength are devoted to manipulating the gap
    var gapSteps = steps - dashSteps;
  
    // The current step #
    var step = 0;
  
    setInterval(function() {
        step = step + 1;
        if (step >= steps) step = 0;
    
        var t, a, b, c, d;
        if (step < dashSteps) {
          t = step / dashSteps;
          a = (1 - t) * dashLength;
          b = gapLength;
          c = t * dashLength;
          d = 0;
        } else {
          t = (step - dashSteps) / (gapSteps);
          a = 0;
          b = (1 - t) * gapLength;
          c = dashLength;
          d = t * gapLength;          
        }

        map.setPaintProperty("route", "line-dasharray", [a, b, c, d]);
    }, 125);
  map.setPaintProperty("route", "line-dasharray", [1, 2, 1, 4]);
});
</script>

</body>
</html>

内容的提问来源于stack exchange,提问作者Ron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 19:20:20