svg - 在 SVG 中绘制文本但删除背景

标签 svg

我正在使用一个 SVG 元素,它应该看起来像 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 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>

我现在的问题是掩码中的最后两个矩形(不绘制边框的矩形)必须具有静态宽度。如果用户改变了文本宽度,用户还需要计算一个新的文本宽度并更新这两个矩形。

有没有办法屏蔽与文本本身宽度完全相同的块并跳过蒙版中的矩形。或者这是创建这样一个面具的唯一方法?如果有人“在那里”有更好、更直观的方式来实现这种布局,我会非常有兴趣听取您的意见。

谢谢你的帮助。

最佳答案

可以使用 svg 过滤器完成没有脚本的 svg 文本的“删除背景”。

例如:

<filter x="0" y="0" width="1" height="1" id="removebackground">
   <feFlood flood-color="blue"/>
   <feComposite in="SourceGraphic"/>
</filter>

可以被这样的文本使用:
<use xlink:href="#text1" fill="black" filter="url(#removebackground)"/>

这将自动适应字符串宽度。如果您需要一些填充,您可以调整过滤器元素上的 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>

关于svg - 在 SVG 中绘制文本但删除背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12260370/

相关文章:

angular - 在 Angular 2 中解析 HTML 字符串并提取和更新值

css - FF25 和 IE10 中的内嵌 SVG 边框为 1px(或更小?亚像素?),Chrome 除外

pdf - Ghostscript SVG 输出设备

javascript - 使用 JavaScript 更改 SVG 图像的大小?

svg - 克隆 svg 组

css - 文本上的透明删除线

SVG 文本质量差?

javascript - jsPDF大表,标题在第二页重叠

javascript - 将文本元素添加到路径元素的中心

svg - 如何在不缩放其图案的情况下缩放形状?