Through the learning of leaflet and its plug-ins, we learned that the use of Leaflet.Path.DashFlow
plug-ins can achieve dynamic display of trajectories, dynamic display of pipeline flow, dynamic display of river flow, etc., to achieve the effect of enhanced visualization. The plug-in is very simple to use, just add dashArray
and dashSpeed
parameters when adding a line normally . The core code is as follows:
Note that when it
dashSpeed
is negative, the line flows in a positive direction.
The effect is as follows:
But in the process of using it, it was discovered that the plug-in has a drawback, that is, when the Canvas
map is drawn using the method (initialized map preferCanvas
parameter is true
), the dynamic effect is not available. How to solve this problem?
Through the study Leaflet.Path.DashFlow
of the leaflet
source code and the source code, it is found that the effect of the dynamic line is mainly achieved by dynamically refreshing dashOffset
the value of the parameter to change the line style.
L.SVG
In _updateStyle
time, update the dashOffset
parameters, but L.Canvas
at _updateStyle
the time, and did not update dashOffset
parameters. This is Canvas
the reason why there is no dynamic effect when the mode is drawn.
L.SVG:
L.Canvas:
As a result, we have found a solution, that is, in L.Canvas
the _updateStyle
method, refer to L.SVG
the processing method, and add dashOffset
the control of the parameters. The core code is as follows:
For ease of use, we will L.Path.DashFlow
plug reseal, we refer to this plug-in to Canvas
and SVG
under are two ways to use this plug-in.
to sum up
- Use
Leaflet.Path.DashFlow
plug-ins to achieve dynamic line effects such as trajectory dynamic display and pipeline flow dynamic display. - When the default plug-in is used
Canvas
to draw a map (initialized mappreferCanvas
parameter istrue
), the dynamic effect is not available. - By modifying the
L.Canvas
middle code, theCanvas
dynamic line effect can be realized in the mode. - The
L.Path.DashFlow
plug reseal, reference plug can inCanvas
andSVG
the line dynamic effects in two ways.
Online example
Original address: http://gisarmory.xyz/blog/index.html?blog=LeafletPathDashFlow .
Pay attention to the " GIS Weapon Library " public account and get more high-quality GIS articles in the first time.
This article is licensed under the Creative Commons Attribution-Non-Commercial Use-Same Method Sharing 4.0 International License Agreement . Welcome to reprint, use, and republish, but be sure to keep the article's signature "GIS Weapon Library" (including link: http://gisarmory.xyz/blog/ ), and it must not be used for commercial purposes. The modified works based on this article must be the same License issuance.