使用 SVG 勾勒出一组感人的形状

发布于 2025-01-07 22:38:02 字数 388 浏览 0 评论 0原文

对于我想要的某种风格,我想在 SVG 中勾勒出一组形状。应用于组时,lines 属性似乎单独勾画出每个形状的轮廓,有效地位于附近其他形状的顶部。为了更清楚地解释我的情况:我有一组触摸矩形,每个矩形都是 8x8 像素。然而,它们并没有形成更大的矩形。

为了简单起见,我们假设它们形成一个十字。所以我有 5 个矩形——1 个在中心,另外一个在该矩形的每一侧。我想把它们全部勾勒出来,就好像它们是一个形状一样。鉴于这种“十字”形状发生变化,我宁愿不使用路径,因为这需要更多的编码。有没有什么方法可以让效果过滤器将这组识别为单个形状?

如果没有,是否至少可以制作该组的黑色副本,其宽度和高度恰好大 2px,我可以将其放置在该组后面以创建纯黑色轮廓?如果是这样,是否可以不重复该组?

感谢您的帮助。

For a certain style I'm going for, I want to outline a group of shapes in SVG. Applied to a group, the stroke property appears to outline each shape individually--effectively going on top of other shapes nearby. To explain my situation more clearly: I have a group of touching rectangles that are 8x8 pixels each. They do not form a larger rectangle, however.

For simplicity's sake, let's say they form a cross. So I have 5 rectangles--1 in the center and one more on each side of that one. I want to outline them all as if they were 1 shape. Given that this "cross" shape changes, I would prefer not to use paths since that would require a lot more coding. Isn't there any way that I could get the effects filter to recognize this group as a single shape?

If not, is it at least possible to make a black copy of this group that's exactly 2px larger in width and height that I can position behind the group to create a solid black outline? And if so, is it possible without duplicating the group?

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深者入戏 2025-01-14 22:38:02

例如,您可以使用像这样的 svg 过滤器:

<filter id="outline">
  <feMorphology operator="dilate" in="SourceAlpha" radius="1"/>
  <feComposite in="SourceGraphic"/>
</filter>

使用像这样的过滤器:

<g filter="url(#outline)">
  <circle fill="lime" cx="20" cy="10" r="5"/>
  <rect x="40" y="10" width="100" height="10" fill="lime"/>
  <line x1="20" y1="10" x2="80" y2="15" stroke="lime"/>
</g>

另一种可能有效的替代方案,具体取决于您的内容的外观,如下所示:

<use xlink:href="#g" stroke-width="10" stroke="black"/>
<g id="g">
  <circle fill="lime" cx="20" cy="10" r="5"/>
  <rect x="40" y="10" width="100" height="10" fill="lime"/>
  <circle fill="lime" cx="140" cy="10" r="5"/>
  <circle fill="lime" cx="120" cy="10" r="5"/>
</g>

You could use an svg filter like this one for example:

<filter id="outline">
  <feMorphology operator="dilate" in="SourceAlpha" radius="1"/>
  <feComposite in="SourceGraphic"/>
</filter>

Use the filter like this:

<g filter="url(#outline)">
  <circle fill="lime" cx="20" cy="10" r="5"/>
  <rect x="40" y="10" width="100" height="10" fill="lime"/>
  <line x1="20" y1="10" x2="80" y2="15" stroke="lime"/>
</g>

Another alternative that might work, depending on how your content looks is something like this:

<use xlink:href="#g" stroke-width="10" stroke="black"/>
<g id="g">
  <circle fill="lime" cx="20" cy="10" r="5"/>
  <rect x="40" y="10" width="100" height="10" fill="lime"/>
  <circle fill="lime" cx="140" cy="10" r="5"/>
  <circle fill="lime" cx="120" cy="10" r="5"/>
</g>
小清晰的声音 2025-01-14 22:38:02

像这样:

<svg xmlns="http://www.w3.org/2000/svg">
    <defs>
        <filter id="biggerbwcopy">
          <feColorMatrix values="0 0 0 0 0
                                 0 0 0 0 0
                                 0 0 0 0 0
                                 0 0 0 1 0"/>
          <feMorphology operator="dilate" radius="2"/>
        </filter>
    </defs>
    <rect id="r" x="10" y="10" width="20" height="20" fill="blue" onclick="biggerbw()"/>
    <script>

      function biggerbw() {
        document.getElementById("r").style.filter="url(#biggerbwcopy)";
      }
    </script>
</svg>

http://jsfiddle.net/longsonr/LrDHT/1/ 单击矩形,它变得黑色并且更大。

您可以使用 feMerge 扩展过滤器以将原始形状放在顶部

Like this:

<svg xmlns="http://www.w3.org/2000/svg">
    <defs>
        <filter id="biggerbwcopy">
          <feColorMatrix values="0 0 0 0 0
                                 0 0 0 0 0
                                 0 0 0 0 0
                                 0 0 0 1 0"/>
          <feMorphology operator="dilate" radius="2"/>
        </filter>
    </defs>
    <rect id="r" x="10" y="10" width="20" height="20" fill="blue" onclick="biggerbw()"/>
    <script>

      function biggerbw() {
        document.getElementById("r").style.filter="url(#biggerbwcopy)";
      }
    </script>
</svg>

http://jsfiddle.net/longsonr/LrDHT/1/ click on the rectangle and it becomes black and bigger.

You could extend the filter to put the original shape on top using feMerge

情话难免假 2025-01-14 22:38:02

这是没有 SVG 过滤器的答案:

svg { fill: pink; }
rect { fill: lightblue; }
#outline-me {
    filter: 
      drop-shadow( 2px  2px 0px black) 
      drop-shadow(-2px  2px 0px black) 
      drop-shadow(-2px -2px 0px black)
      drop-shadow( 2px -2px 0px black);
}
<svg xmlns="http://www.w3.org/2000/svg" height="344" width="440">    
 <g id="outline-me">
  <rect x="130" y="10" width="20" height="20" />
  <rect x="130" y="30" width="20" height="20" />
  <rect x="110" y="30" width="20" height="20" />
  <rect x="150" y="30" width="20" height="20" />
  <rect x="130" y="50" width="20" height="20" />
 </g>
</svg>

这样做的好处是,在 HTML 中,您可以使矩形透明,并仅使用以下方式显示轮廓:(

rect { fill: white; /* background: white */ }
#outline-me { mix-blend-mode: darken; }

不幸的是,对于 SVG 情况,这对我不起作用)

http://jsfiddle.net/EoghanM/9ua5fyjx

Here's an answer without SVG filters:

svg { fill: pink; }
rect { fill: lightblue; }
#outline-me {
    filter: 
      drop-shadow( 2px  2px 0px black) 
      drop-shadow(-2px  2px 0px black) 
      drop-shadow(-2px -2px 0px black)
      drop-shadow( 2px -2px 0px black);
}
<svg xmlns="http://www.w3.org/2000/svg" height="344" width="440">    
 <g id="outline-me">
  <rect x="130" y="10" width="20" height="20" />
  <rect x="130" y="30" width="20" height="20" />
  <rect x="110" y="30" width="20" height="20" />
  <rect x="150" y="30" width="20" height="20" />
  <rect x="130" y="50" width="20" height="20" />
 </g>
</svg>

The nice thing about this is that in HTML you can have the rectangles transparent, and just show the outline using:

rect { fill: white; /* background: white */ }
#outline-me { mix-blend-mode: darken; }

(Unfortunately that bit wouldn't work for me for the SVG case)

http://jsfiddle.net/EoghanM/9ua5fyjx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文