【发布时间】:2017-09-19 19:53:47
【问题描述】:
这在某种程度上是Humongous height value for <filter> not preventing cutoff 的延续:我仍在尝试在<path> 上应用<filter>,但我遇到了被剪裁的问题。
通过使用<filter> 上的x/y 属性移动过滤器画布的中心,问题在另一个线程中得到了解决,仍然所有内容都以百分比表示,因此相对于你的东西的大小正在尝试应用效果,但问题是边长可以是0,即使在您看到某些东西的情况下,参见例如以下示例中的第一行:
.pathWrapper path {
stroke: grey;
fill: none;
stroke-width: 1.5;
marker-start: url(#circle);
marker-end: url(#arrow);
}
.pathWrapper:hover {
filter: url(#colorFilter);
}
<svg style="height:400px;width:100%;background-color:LightCyan">
<defs>
<filter id="colorFilter" x="-300%" y="-300%" width="600%" height="600%">
<feGaussianBlur in="SourceAlpha" stdDeviation="1" result="blur"></feGaussianBlur>
<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 3 0" result="lightenedBlur"></feColorMatrix>
<feMerge>
<feMergeNode in="lightenedBlur"></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
</feMerge>
</filter>
<marker id="arrow" viewBox="0 -5 10 10" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto" style="fill: grey;">
<path d="M0,-5L10,0L0,5"></path>
</marker>
<marker id="circle" viewBox="0 -4 8 8" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto-start-reverse" style="fill: grey;"><circle r="4" cx="4"></circle></marker>
</defs>
<g transform="scale(2)">
<g class="pathWrapper" transform="translate(70,20)">
<path d="M52,10L45,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(70,50)">
<path d="M42,20L35,20L30,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(200,20)">
<path d="M42,140L35,140L-30,70L-30,10L-30,10L-37,10"></path>
</g>
</g>
</svg>
将鼠标悬停在线条上可查看那里应用的滤镜效果。 悬停时您会看到顶线变得不可见。这是因为边界框高度为0,笔划宽度和标记小玩意不计在内:
我可以使用绝对单位,例如就像下面的例子:
.pathWrapper path {
stroke: grey;
fill: none;
stroke-width: 1.5;
marker-start: url(#circle);
marker-end: url(#arrow);
}
.pathWrapper:hover {
filter: url(#colorFilter);
}
<svg style="height:400px;width:100%;background-color:LightCyan">
<defs>
<filter id="colorFilter" filterUnits="userSpaceOnUse" x="-125" y="-125" width="250" height="250">
<feGaussianBlur in="SourceAlpha" stdDeviation="1" result="blur"></feGaussianBlur>
<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 3 0" result="lightenedBlur"></feColorMatrix>
<feMerge>
<feMergeNode in="lightenedBlur"></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
</feMerge>
</filter>
<marker id="arrow" viewBox="0 -5 10 10" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto" style="fill: grey;">
<path d="M0,-5L10,0L0,5"></path>
</marker>
<marker id="circle" viewBox="0 -4 8 8" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto-start-reverse" style="fill: grey;"><circle r="4" cx="4"></circle></marker>
</defs>
<g transform="scale(2)">
<g class="pathWrapper" transform="translate(70,20)">
<path d="M52,10L45,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(70,50)">
<path d="M42,20L35,20L30,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(200,20)">
<path d="M42,140L35,140L-30,70L-30,10L-30,10L-37,10"></path>
</g>
</g>
</svg>
问题在于,在我的用例中,应用效果的元素的大小可能会有很大差异,所以我必须在其中设置一个巨大的值,否则总是会发生变化足够大(如示例中跨越最大高度的线所示)。
我发现使用 CSS calc() 可能是一个解决方案:
#colorFilter {
width: calc(100% + 100);
height: calc(100% + 100);
x: calc(-50% - 50);
y: calc(-50% - 50);
}
.pathWrapper path {
stroke: grey;
fill: none;
stroke-width: 1.5;
marker-start: url(#circle);
marker-end: url(#arrow);
}
.pathWrapper:hover {
filter: url(#colorFilter);
}
<svg style="height:400px;width:100%;background-color:LightCyan">
<defs>
<filter id="colorFilter" filterUnits="userSpaceOnUse">
<feGaussianBlur in="SourceAlpha" stdDeviation="1" result="blur"></feGaussianBlur>
<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 3 0" result="lightenedBlur"></feColorMatrix>
<feMerge>
<feMergeNode in="lightenedBlur"></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
</feMerge>
</filter>
<marker id="arrow" viewBox="0 -5 10 10" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto" style="fill: grey;">
<path d="M0,-5L10,0L0,5"></path>
</marker>
<marker id="circle" viewBox="0 -4 8 8" refX="0" refY="0" markerWidth="8" markerHeight="8" orient="auto-start-reverse" style="fill: grey;"><circle r="4" cx="4"></circle></marker>
</defs>
<g transform="scale(2)">
<g class="pathWrapper" transform="translate(70,20)">
<path d="M52,10L45,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(70,50)">
<path d="M42,20L35,20L30,10L-30,10L-37,10"></path>
</g>
<g class="pathWrapper" transform="translate(200,20)">
<path d="M42,140L35,140L-30,70L-30,10L-30,10L-37,10"></path>
</g>
</g>
</svg>
这适用于当前版本的 Google Chrome 和 Mozilla Firefox,但似乎不适用于 Micosoft Edge 或 IE11(通过一些搜索,似乎calc() 支持即使对于 HTML 内容也非常敏感,请参阅已知问题部分https://caniuse.com/#search=calc)。
那么是否存在比calc() 方法更好的替代方案?
(也许值得注意的是,我正在使用动态 d3.js 生成的内容。)
【问题讨论】:
标签: svg svg-filters