我正在处理一个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>
我的问题是遮罩中最后两个矩形(不画边框的矩形)必须有固定宽度。如果用户改变了文本的宽度,用户还需要计算文本的新宽度并更新这两个矩形。
是否有一种方法可以屏蔽与文本本身完全相同宽度的区块,并跳过遮罩中的矩形?或者这是创建这样一个遮罩的唯一方法?如果有人有更好和更直观的方法来实现此布局,我将非常感兴趣听取您的意见。
谢谢您的帮助。