【发布时间】:2012-08-28 22:18:15
【问题描述】:
我正在使用类似于 this: (sorry that I have to post this as a link instead of as a picture, but as a new user I didn't have permissions to post images) 的 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>
我现在的问题是掩码中的最后两个矩形(不是绘制边框的矩形)必须具有静态宽度。如果用户改变了文本宽度,用户还需要计算一个新的文本宽度并更新这两个矩形。
有没有办法屏蔽与 text 本身宽度完全相同的块并跳过掩码中的矩形。或者这是制造这种面具的唯一方法?如果“外面”的任何人有更好、更直观的方式来实现这种布局,我会非常有兴趣收到你的来信。
感谢您的帮助。
【问题讨论】:
-
如果没有 javascript,这比乍看之下要复杂得多。这是一个可能的解决方法stackoverflow.com/a/8680391/524555
-
我担心如果不编写脚本会很困难。有没有其他方法可以在不使用口罩的情况下达到类似的效果?我一直在尝试使用带有 stroke="green" 的 textPath 的想法,并尝试调整笔触的开始,以便绿线在文本之后开始。
标签: svg