svg - 应用过滤器之前的抗锯齿 SVG 文本?
问题描述
SVG 过滤器将其源图像栅格化,这意味着过滤后的文本不会被消除锯齿,从而导致锯齿状边缘。
有解决方法吗?也许可以使用另一个过滤器来模拟抗锯齿,或者我可以在应用过滤器之前以某种方式对文本进行抗锯齿处理?
相关过滤器:
<filter id="f">
<feGaussianBlur in="SourceGraphic" stdDeviation="0" result="blur" />
<feColorMatrix
in="blur"
mode="matrix"
values="1 0 0 0 0
0 1 0 0 0
1 0 1 0 0
0 0 0 15 -8"
result="goo"
/>
<feComposite in="SourceGraphic" in2="goo" operator="atop" />
</filter>
解决方案
有解决方法吗?也许可以使用另一个过滤器来模拟抗锯齿,或者我可以在应用过滤器之前以某种方式对文本进行抗锯齿处理?
这个答案详细介绍了这个问题。
作为折衷的解决方案,您可以尝试:
- 为字母和背景的颜色选择较少对比的颜色
- 将属性
shape-rendering =" crispEdges"
应用于字体 - 选择呈现更锐利边缘的字体
例如,您选择的字体font-family =" cursive "
如下所示:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="600" viewBox="0 0 400 400" >
<text x="35" y="150" font-size="100px" fill="black" font-family="cursive"> HELLO </text>
</svg>
font-family = "Monotype Corsiva"
看起来更好
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="200" viewBox="0 0 400 200" >
<rect width="100%" height="100%" fill="silver" />
<text x="35" y="120" font-size="100px" fill="#444444" shape-rendering="crispEdges" font-family="Monotype Corsiva" > HELLO </text>
</svg>
应用 SVG 滤镜来平滑锯齿状边缘
- 对于过滤器
feGaussianBlur
,选择参数(矩阵的最后一行)0 0 0 29 -1
- 对于过滤器
feComposite
operator="atop"
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="200" viewBox="0 0 400 200" >
<defs>
<filter id="f">
<feGaussianBlur in="SourceGraphic" stdDeviation="1" result="blur" >
</feGaussianBlur>
<feColorMatrix in="blur" type="matrix"
values="
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 29 -1"
result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
<rect width="100%" height="100%" fill="silver" />
<text filter="url(#f)" x="35" y="120" font-size="100px" fill="#444444" shape-rendering="crispEdges" font-family="Monotype Corsiva" > HELLO </text>
</svg>
过滤动画
我正在为 feGaussianBlur 的 stdDeviation 设置动画
正如我从@aleclarson的评论中了解到的那样,您想要为 filter 属性设置动画
<animate attributeName="stdDeviation" begin="0s" dur="8s"
repeatCount="indefinite" values="1;6;12;12;6;1;1" />
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="200" viewBox="0 0 400 200" >
<defs>
<filter id="f">
<feGaussianBlur in="SourceGraphic" stdDeviation="1" result="blur" >
<animate attributeName="stdDeviation" begin="0s" dur="8s" repeatCount="indefinite" values="1;6;12;12;6;1;1" />
</feGaussianBlur>
<feColorMatrix in="blur" type="matrix"
values="
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 29 -1"
result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="atop"/>
</filter>
</defs>
<text filter="url(#f)" x="35" y="120" font-size="100px" fill="#111111" shape-rendering="crispEdges" font-family="Monotype Corsiva" > HELLO </text>
</svg>
具有值和运算符 = "xor" 的变体
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="400" height="200" viewBox="0 0 400 200" >
<defs>
<filter id="f">
<feGaussianBlur in="SourceGraphic" stdDeviation="1" result="blur" >
<animate attributeName="stdDeviation" begin="0s" dur="8s" repeatCount="indefinite" values="1;6;12;12;6;1;1" />
</feGaussianBlur>
<feColorMatrix in="blur" type="matrix"
values="
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 29 -1"
result="goo" />
<feComposite in="SourceGraphic" in2="goo" operator="xor"/>
</filter>
</defs>
<text filter="url(#f)" x="35" y="120" font-size="100px" fill="#111111" shape-rendering="crispEdges" font-family="Monotype Corsiva" > HELLO </text>
</svg>
推荐阅读
- javascript - 我想将 javascript 上传到我的潜在客户页面
- javascript - 使用多条弧线从图像中裁剪圆圈
- functional-programming - 如何以非阻塞方式解析 Spring 5 WebClient 响应?
- variables - ErrorException [注意]:动态构建的现有变量上的未定义变量
- haskell - 堆栈构建导致“输出已使用 -o 重定向,但不会生成输出,因为没有主模块。”
- java - 如何在 Cucumber 报告中为所采取的每项操作嵌入屏幕截图?
- sql - 在oracle sql中设置连接的用户名密码
- html - 浏览器未将链接解析为链接
- java - 单例、线程和同步
- powershell - PowerShell:为什么有时我无法重定向外部方法的标准输出和标准错误?