gpt4 book ai didi

css - 如何使用 SVG 滤镜创建模糊的标签背景?

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:13 24 4
gpt4 key购买 nike

我希望有一个好的工作方法来解决以下问题:

在条形图上,条形图填充了不同但未知的颜色,我想放置固定黑色的标签,以便它们易于阅读。

我正在搜索的方法应该类似于图像中窗口的标题栏。标签是黑色的,位于任何颜色栏顶部的模糊白色背景之上。

enter image description here

有没有办法用 SVG 滤镜做到这一点?

以下是我尝试过的一些方法:

    .black {
fill: black;
}
.blue {
fill: blue;
}
.red {
fill: red;
}
.yellow {
fill: yellow;
}
.label{
fill: black;
font-size: 14px;
}
.label-background{
fill: white;
font-size: 16px;
}
.text-background{
paint-order: stroke;
stroke: #fff;
fill: black;
stroke-width: 5px;
}
.header{
font-size: 20px;
}
.background{
fill: white;
opacity: 0.6;
}
  <svg width="500" height="500">
<defs>
<filter x="-0.05" y="-0.1" width="1.08" height="1.2" id="solid">
<feFlood flood-color="lightgrey"/>
<feComposite in="SourceGraphic" operator="xor" />
</filter>
</defs>

<g class="row-1" transform="translate(0,30)">
<text class="header" x="20" y="-10">text label on top of bar </text>
<g>
<rect class="black" width=100 height=30></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<text class="label" x="20" y="20">my text label</text>
</g>
</g>

<g class="row-2" transform="translate(0,100)">
<text class="header" x="20" y="-10">text label on top of rect.background on top of bar </text>
<g>
<rect class="black" width=100 height=30></rect>
<rect class="background" x=17 y=6 width=78 height=19></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<rect class="background" x=17 y=6 width=78 height=19></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<rect class="background" x=17 y=6 width=78 height=19></rect>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<rect class="background" x=17 y=6 width=78 height=19></rect>
<text class="label" x="20" y="20">my text label</text>
</g>
</g>

<g class="row-3" transform="translate(0, 170)">
<text class="header" x="20" y="-10">text label with filter #solid on top of bar </text>
<g>
<rect class="black" width=100 height=30></rect>
<text class="label" filter="url(#solid)" x="20" y="20">my text label</text>
</g>

<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<text class="label" filter="url(#solid)" x="20" y="20">my text label</text>
</g>

<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<text class="label" filter="url(#solid)" x="20" y="20">my text label</text>
</g>

<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<text class="label" filter="url(#solid)" x="20" y="20">my text label</text>
</g>
</g>

<g class="row-4" transform="translate(0, 240)">
<text class="header" x="20" y="-10">text label on top of larger text label with background color on top of bar </text>
<g>
<rect class="black" width=100 height=30></rect>
<text class="label-background" x="18" y="22">my text label</text>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<text class="label-background" x="18" y="22">my text label</text>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<text class="label-background" x="18" y="22">my text label</text>
<text class="label" x="20" y="20">my text label</text>
</g>

<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<text class="label-background" x="18" y="22">my text label</text>
<text class="label" x="20" y="20">my text label</text>
</g>
</g>

<g class="row-5" transform="translate(0, 310)">
<text class="header" x="20" y="-10">text label with paint-order: stroke on top of bar </text>
<g>
<rect class="black" width=100 height=30></rect>
<text class="label text-background" x="20" y="20">my text label</text>
</g>

<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<text class="label text-background" x="20" y="20">my text label</text>
</g>

<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<text class="label text-background" x="20" y="20">my text label</text>
</g>

<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<text class="label text-background" x="20" y="20">my text label</text>
</g>
</g>
</svg>

最佳答案

由于您要求的是 SVG 过滤器解决方案:

  1. SourceAlpha 开始,它会为您提供元素(即文本)的黑色轮廓,然后使用 feGaussianBlur 对其进行模糊以创建柔和阴影。<
  2. 这个阴影是黑色的,所以使用 feColorMatrix 让它变成白色。
  3. 最后,使用 feBlend 将原始文本放在阴影之上。

引用资料:

.black {
fill: black;
}
.blue {
fill: blue;
}
.red {
fill: red;
}
.yellow {
fill: yellow;
}
.label {
fill: black;
font-size: 14px;
}
<svg width="500" height="80" color-interpolation-filters="sRGB">
<defs>
<filter id="shadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur" />
<feColorMatrix in="blur" type="matrix" values="0 0 0 0 1
0 0 0 0 1
0 0 0 0 1
0 0 0 8 0" result="white" />
<feColorMatrix in="white" type="matrix" values="1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 .8 0" result="dim" />
<feBlend in="SourceGraphic" in2="dim" mode="normal" />
</filter>
</defs>

<g class="row-1" transform="translate(10,30)">
<text class="header" x="0" y="-10">feGaussianBlur on SourceAlpha</text>
<g>
<rect class="black" width=100 height=30></rect>
<text class="label" x="20" y="20" filter="url(#shadow)">my text label</text>
</g>
<g transform="translate(120,0)">
<rect class="blue" width=100 height=30></rect>
<text class="label" x="20" y="20" filter="url(#shadow)">my text label</text>
</g>
<g transform="translate(240,0)">
<rect class="red" width=100 height=30></rect>
<text class="label" x="20" y="20" filter="url(#shadow)">my text label</text>
</g>
<g transform="translate(360,0)">
<rect class="yellow" width=100 height=30></rect>
<text class="label" x="20" y="20" filter="url(#shadow)">my text label</text>
</g>
</g>
</svg>

关于css - 如何使用 SVG 滤镜创建模糊的标签背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58784904/

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