我正在使用一个看起来像这样的SVG元素:(抱歉,我必须将其作为链接发布而不是作为图片,但作为新用户,我没有权限发布图像)
页面中间带圆角的边框,但删除边框的位置将插入页眉/页脚.用户指定的文本将插入页眉,页脚和框架本身.矩形被绘制在另一个背景(图片,另一个带有颜色的矩形等)的顶部.我需要找到一种方法来保持原始背景,仅绘制边框的一部分并将文本放在原始背景的顶部.
我想出了这个解决方案:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 620 1100" preserveAspectRatio="xMidYMid meet">
<defs>
<!--Define some texts-->
<text id="text1" x="90" y="100" style="text-anchor:start;font-size:30px;">THIS IS MY HEADER</text>
<text id="text2" x="525" y="1010" style="text-anchor:end;font-size:30px;">MY TEXT HERE</text>
<mask id="Mask1">
<!--draw the entire screen-->
<rect x="0" y="0" width="620" height="1100" style="fill:white;" />
<!--Mask out the two rectangles where text is to be placed-->
<rect x="300" y="980" width="350" height="50" style="fill:black;" />
<rect x="90" y="90" width="350" height="40" style="fill:black;" />
</mask>
</defs>
<!--The original background (here a rect with a fill color, but could also be an image)-->
<rect x="0" y="0" width="620" height="1100" style="fill:#f0ffb6"/>
<!--Draw the rectangle but applying the mask-->
<rect x="100" y="100" rx="20" ry="20" width="420" height="900" mask="url(#Mask1)" style="fill:none;stroke:green;stroke-width:5"/>
<!--Draw the text-->
<use xlink:href="#text1" fill="black" stroke="black" stroke-width="1" />
<use xlink:href="#text2" fill="black" stroke="black" stroke-width="1" />
<text x="200" y="200">This text goes into the border</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
我现在的问题是掩码中的最后两个rects(不绘制边框的矩形)必须具有静态宽度.如果用户更改文本宽度,则用户还需要计算文本的新内容并更新这两个矩形.
有没有办法屏蔽一个块与文本本身完全相同的宽度,并跳过掩码中的矩形.或者这是创建这种面具的唯一方法吗?如果任何人"在那里"有一个更好,更直观的方式来实现这种布局,我会非常有兴趣听取你的意见.
谢谢你的帮助.
Eri*_*röm 18
可以使用svg过滤器在没有脚本的情况下"删除svg文本的背景".
例如:
<filter x="0" y="0" width="1" height="1" id="removebackground">
<feFlood flood-color="blue"/>
<feComposite in="SourceGraphic"/>
</filter>
Run Code Online (Sandbox Code Playgroud)
哪个文本可以像这样使用:
<use xlink:href="#text1" fill="black" filter="url(#removebackground)"/>
Run Code Online (Sandbox Code Playgroud)
这将自动适应字符串宽度.如果需要一些填充,可以调整滤镜元素上的x,y,width,height属性.
嗯,再想一想,这可能不是你想要的.但是可以适应上述情况.这是您使用此解决方案的文件:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 620 1100" preserveAspectRatio="xMidYMid meet">
<defs>
<!--Define some texts-->
<text id="text1" x="90" y="100" style="text-anchor:start;font-size:30px;">THIS IS MY HEADER</text>
<text id="text2" x="525" y="1010" style="text-anchor:end;font-size:30px;">MY TEXT HERE</text>
<filter x="0" y="0" width="1" height="1" id="removebackground">
<feFlood flood-color="black"/>
</filter>
<mask id="Mask1">
<!--draw the entire screen-->
<rect x="0" y="0" width="620" height="1100" style="fill:white;" />
<!--Mask out the two rectangles where text is to be placed-->
<use xlink:href="#text1" filter="url(#removebackground)"/>
<use xlink:href="#text2" filter="url(#removebackground)"/>
</mask>
</defs>
<!--The original background (here a rect with a fill color, but could also be an image)-->
<rect x="0" y="0" width="620" height="1100" style="fill:#f0ffb6"/>
<!--Draw the rectangle but applying the mask-->
<rect x="100" y="100" rx="20" ry="20" width="420" height="900" mask="url(#Mask1)" style="fill:none;stroke:green;stroke-width:5"/>
<!--Draw the text-->
<use xlink:href="#text1" fill="black" stroke="black" stroke-width="1" />
<use xlink:href="#text2" fill="black" stroke="black" stroke-width="1" />
<text x="200" y="200">This text goes into the border</text>
</svg>
Run Code Online (Sandbox Code Playgroud)
我相信影响此问题的一个简单方法是使用用户的文本作为具有大描边宽度设置和黑色描边的亮度蒙版。接下来,将文本正常放置在蒙版边框上。
| 归档时间: |
|
| 查看次数: |
10659 次 |
| 最近记录: |