【问题标题】:Multi-color SVG polyline多色 SVG 折线
【发布时间】:2013-02-06 02:35:56
【问题描述】:

我正在使用 Leaflet 在带有 javascript 的地图上绘制 SVG 格式的地图数据。我有一组数千个坐标,我正在其中绘制一个传单路径(扩展 L.Browser.svg)。

我想用第三个变量对线进行颜色编码(因为这是一张地图,比如说海拔,蓝色低,红色高,或类似的东西)。我是 SVG 新手,但似乎我只能为整个路径设置笔触颜色。

例如我现在所拥有的——线条只是一种颜色(为简单起见,概念代码被剥离):

// create the SVG group and path element
this._container = this._createElement('g');
this._path = this._createElement('path');
// set the stoke color -- I wish I could make this dynamic per segment!
this._path.setAttribute('stroke', '#00000');

// Not real code, but simplified...generate lots of coordinates for the polyline
var myPath = "M" + p.x + "," + p.y + " L";
points.each(function(item, index){
    poly += item.x + "," + item.y + " ";                    
});

// update
this._path .setAttribute('d', poly);

有没有比创建数千个路径元素并将它们添加到 SVG 组中更好的方法来做到这一点,每个元素都有自己的描边颜色?

【问题讨论】:

    标签: javascript svg vector-graphics leaflet geojson


    【解决方案1】:

    有一种方法可以实现这一点,但也需要大量工作。如果您有一个具有正确颜色编码(something like this) 的完整图像,您可以使用 SVG 过滤器将该图像与您的路径合成:即时路径着色。您也可以使用 SVG 蒙版来实现相同的效果。如果您没有该图像,那么您可以在 SVG 中构建一个,但这可能需要做类似的工作量。

    【讨论】:

    • 我没有图片。我有一个 X 点、Y 点和颜色的数组。这有助于澄清吗?
    • 我认为你最好设置每个路径段的笔触颜色。
    • 除了创建路径元素数组(createElement('path'))之外,还有更好的方法吗?我可以访问组或路径的子分段吗?我在搜索中找不到任何东西。
    • 据我所知。我相信只有路径对象作为一个整体可以设置样式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2016-10-14
    • 2021-10-25
    • 2011-10-13
    • 2014-07-07
    • 2020-05-04
    • 2014-08-06
    相关资源
    最近更新 更多