html - 使用 SVG 过滤器为文本添加边框
问题描述
我从这里学到了使用过滤器为SVG中的文本添加背景。但是如何为文本添加边框?
<svg width="100%" height="100%">
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<text filter="url(#solid)" x="20" y="50" font-size="50">solid background</text>
</svg>
解决方案
使用另一个稍大的过滤器来创建边框。
<svg width="100%" height="100%">
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow"/>
<feComposite in="SourceGraphic"/>
</filter>
<filter x="-0.005" y="-0.01" width="1.01" height="1.02" id="border">
<feFlood flood-color="black"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<g filter="url(#border)">
<text filter="url(#solid)" x="20" y="50" font-size="50">solid background</text>
<g>
</svg>
看起来 Chrome 不支持在同一个元素上使用多个过滤器,但如果你有一个支持例如 Firefox 的浏览器,你可以避免额外的<g>
元素......
<svg width="100%" height="100%">
<defs>
<filter x="0" y="0" width="1" height="1" id="solid">
<feFlood flood-color="yellow"/>
<feComposite in="SourceGraphic"/>
</filter>
<filter x="-0.005" y="-0.01" width="1.01" height="1.02" id="border">
<feFlood flood-color="black"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<text filter="url(#solid) url(#border)" x="20" y="50" font-size="50">solid background</text>
</svg>
推荐阅读
- symfony - 使用变量名中的 index.loop 创建和设置 twig 变量的值
- java - 如何在 Google Places Api 中同时按距离和使用半径进行排序?
- amazon-ec2 - 获取当前的 ec2 计数 - 或另一个来自数字 id
- c++ - "setprecision()" 和浮点数
- javascript - 如何更改作为内部 html 一部分的一个元素的引导类?
- apache-kafka - 当存储值为 Avro SpecificRecord 时,KafkaStreamsStateStore 不起作用
- html - 向右渲染控件不适用于 style="float: right"
- r - 如何使用 R 从 Zillow API 响应输出中获取 XML 元素的文本?
- c# - 在表单发布之前捕获 URL
- c++ - 如何查看 .ktx 纹理内部?