SVG - 创建不同合并路径的外部描边

标签 svg

我有一个 SVG 文件,它表示使用不同路径的 map 。

/image/PB5zo.jpg

这些路径代表不同的区域;我需要将这些路径创建的外部笔画加粗,如图所示(请注意,单个文件中有 2 个 SVG)。

/image/pCoAp.jpg

是否可以通过简单的方式实现这一点?

谢谢建议

最佳答案

假设 map 的每个区域都有自己的路径 - 并且您还没有围绕外部的路径,那么最简单的解决方案是:

  1. 复制 map
  2. 放在后面
  3. 给 map 路径加粗描边

例如,让我们从以下简化示例开始。

<svg width="400" height="200">
  <rect x="50" y="50" width="200" height="50" fill="linen" stroke="black"/>
  <rect x="250" y="50" width="100" height="50" fill="linen" stroke="black"/>
  <rect x="50" y="100" width="150" height="50" fill="linen" stroke="black"/>
  <rect x="200" y="100" width="150" height="50" fill="linen" stroke="black"/>
</svg>

如果我们复制这些元素并给它加粗,我们会得到:

<svg width="400" height="200">
  <g fill="linen" stroke="black" stroke-width="5">
    <rect x="50" y="50" width="200" height="50"/>
    <rect x="250" y="50" width="100" height="50"/>
    <rect x="50" y="100" width="150" height="50"/>
    <rect x="200" y="100" width="150" height="50"/>
  </g>
  <rect x="50" y="50" width="200" height="50" fill="linen" stroke="black"/>
  <rect x="250" y="50" width="100" height="50" fill="linen" stroke="black"/>
  <rect x="50" y="100" width="150" height="50" fill="linen" stroke="black"/>
  <rect x="200" y="100" width="150" height="50" fill="linen" stroke="black"/>
</svg>

为了使文件尽可能小,我们可以重复使用 map 的两个副本的路径:

<svg width="400" height="200">
  <defs>
    <g id="map">
      <rect x="50" y="50" width="200" height="50"/>
      <rect x="250" y="50" width="100" height="50"/>
      <rect x="50" y="100" width="150" height="50"/>
      <rect x="200" y="100" width="150" height="50"/>
    </g>
  </defs>

  <use xlink:href="#map" fill="linen" stroke="black" stroke-width="5"/>
  <use xlink:href="#map" fill="linen" stroke="black"/>
</svg>

这不一定是最优雅的解决方案,因为所有 map 元素都会绘制两次。但这是最简单的解决方案。

关于SVG - 创建不同合并路径的外部描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46464105/

相关文章:

html - 如何在悬停时更改 SVG 图像的颜色?

javascript - 内联 SVG 和 jQuery 选择

javascript - svg rect 多种填充颜色

javascript - SVG 中文本的动态定位

javascript - 使用不透明的 jQuery 将图像转换为灰度

svg - 在D3模糊中对笔触进行描边

javascript - 1 个 svg 中的多个力导向图(文本显示问题)

jquery - 在svg中查找圆圈内的元素

html - 如何缩放具有相同位置但位于其他 svg 元素之上的 svg

html - 专门使用 SVG 图像文件有什么缺点吗?