gpt4 book ai didi

css - 将阴影添加到 svg 多边形

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:15 25 4
gpt4 key购买 nike

在附加的代码片段中,我有一个多边形,我需要添加一个效果使其看起来像这样:

enter image description here

我不知道如何在 svg 中执行此操作,如果它是 html,那么我想我会使用 box-shadow。

似乎唯一的解决方案是使用 filter但我想我只能在 <svg/> 上使用它元素,所以我正在努力如何做到这一点。

polygon {
fill: #5091b9;
stroke: #4387b0;
stroke-width: 2;
}
<svg width="300" height="300">
<g transform="translate(100, 100)">
<polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node-vertical__hexagon node-vertical__inactive"> </polygon>
</g>
</svg>

最佳答案

您可以将 drop-shadow 滤镜应用于 SVG 或将 SVG 用作元素的背景并对其应用滤镜:

polygon {
fill: #5091b9;
stroke: #4387b0;
stroke-width: 2;
}
.filter {
filter: drop-shadow(10px 0 5px red);
}

.box {
height: 100px;
width: 100px;
background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100"><g transform="translate(50, 50)"><polygon stroke="%234387b0" stroke-width="2" fill="%235091b9" points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" ></polygon></g></svg>');
}
<p>SVG element</p>
<svg viewBox="0 0 100 100" width="100" class="filter"><g transform="translate(50, 50)" ><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" ></polygon></g></svg>
<p>SVG as background</p>
<div class="box filter"></div>

您还可以考虑 SVG 过滤器:

.node-vertical__inactive {
filter:url(#shadow);
}

.node-vertical__hexagon {
fill: #5091b9;
stroke: #4387b0;
stroke-width: 2;
}
<svg>
<defs>
<filter id="shadow" x="-20%" y="-20%" width="200%" height="200%">
<feDropShadow dx="20" dy="3" stdDeviation="5" flood-color="#5091b9" />
</filter>
</defs>
<g class="vx-group" transform="translate(0, 0)">
<g class="vx-group node-vertical__container" transform="translate(100, 100)"><svg class="node-vertical__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node-vertical__hexagon"></polygon></svg>
</g>
</g>
</svg>

关于css - 将阴影添加到 svg 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52264094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com