Animate a styled line #177
-
Hey all! Interested to see if anyone has done any line animation work and wondering how you achieved such. I searched within this repo and couldn't find anything related to it (or I missed something). I'm interested to animate the line in a way to indicate the direction of a line as the lines indicate a driven route. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I’ve done it in a non-open-source project as part of some contract work and it works great. Basically you do what’s in this Mapbox example, just with Svelte components instead of mapbox for the sources/layers/etc: https://docs.mapbox.com/mapbox-gl-js/example/animate-ant-path/ Then instead of calling Oh and make sure you have some way to detect when to stop calling let active = true;
onDestroy(() => active = false);
function animateDashArray(timestamp) {
// the rest of the function
if(active) {
requestAnimationFrame(animateDashArray);
}
} |
Beta Was this translation helpful? Give feedback.
I’ve done it in a non-open-source project as part of some contract work and it works great. Basically you do what’s in this Mapbox example, just with Svelte components instead of mapbox for the sources/layers/etc: https://docs.mapbox.com/mapbox-gl-js/example/animate-ant-path/
Then instead of calling
setPaintProperty
you update thepaint
property on the relevant LineLayer component instead.Oh and make sure you have some way to detect when to stop calling
requestAnimationFrame
. That demo doesn't bother since it runs forever but you'll want to make sure that you stop calling it once the page is torn down. Something like this will work if you don't have some other interlock: