html - 在 SVG 中的蒙版上放置轮廓

标签 html svg masking

是否可以在蒙版上放置轮廓或其他内容,以便您可以实际看到蒙版的位置?我有一个想要掩盖的元素,但实际上我无法看到它是否排列在应该的位置。

我想做的是使用 endScreen ID 作为掩码,并使用 startOrder ID 作为被掩码的对象。没错,它遮盖了它,但不正确,如果能够明显地看到 mask 在设计中的位置,那就太好了。

<svg id="demo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 337.32 386.57">
            <style>
                .st0 {
                    fill: #fff
                }

                .st5 {
                    fill: #c32034
                }
            </style>

            <defs>
                <mask id="button-mask" width="1" height="1">
                     <path d="M51.24 372.52V52.27c0-15.4 12.6-28 28-28h180.79c15.4 0 28 12.6 28 28v320.25" fill="white" />
                </mask>
            </defs>
            <path id="endScreen" class="st0" d="M51.24 372.52V52.27c0-15.4 12.6-28 28-28h180.79c15.4 0 28 12.6 28 28v320.25" />
            <circle id="startScreen" class="st0" cx="167.67" cy="199.37" r="91" />
            <path id="base" fill="none" stroke="#c1a88b" stroke-width="4" stroke-linecap="round" stroke-miterlimit="10" d="M6 374.88h326.27" />
            <path id="phoneOutline" d="M302.12 372.43V55.31c0-25.15-21.05-45.73-46.78-45.73H82.26c-25.73 0-46.78 20.58-46.78 45.73v317.13" fill="none" stroke="#c1a88b" stroke-width="7.358" stroke-linecap="round" stroke-miterlimit="10" />
            <circle id="speakerSmall" cx="204.78" cy="43.04" r="4.4" fill="#c1a88b" />
            <path id="speakerLarge" fill="none" stroke="#c1a88b" stroke-width="9" stroke-linecap="round" stroke-miterlimit="10" d="M132.04 43.23h59.45" />
            <path id="startOrder" class="st5" d="M236.62 337.2H99.44c-6.6 0-12-5.4-12-12v-20.48c0-6.6 5.4-12 12-12h137.17c6.6 0 12 5.4 12 12v20.48c.01 6.6-5.39 12-11.99 12z" mask="url(#button-mask)" />
            <path id="star" class="st5" d="M168.66 91.97l4.94 10.01 11.04 1.6-7.99 7.79 1.89 11.01-9.88-5.2-9.89 5.2 1.89-11.01-7.99-7.79 11.05-1.6z" />
            <circle id="endOrder" cx="165.72" cy="313.53" r="36.5" fill="#89bd40" />
            <path id="check" fill="none" stroke="#fdfeff" stroke-width="7" stroke-miterlimit="10" d="M147.32 255.76l12.88 11.5 23.93-23.46" />
        </svg>

最佳答案

首先将要用于蒙版的路径定义为模板本身,不带任何表示属性,然后引用它两次:首先,在蒙版内使用 fill="white",然后再次在图片顶部添加 fill="none"stroke="blue"

您会注意到您的路径并未闭合,但显然填充会在端点之间创建隐式闭合,而笔划不会显示该闭合。一个简单的解决方案是在路径定义的末尾添加一个 z 命令,该解决方案不会对您的掩码进行任何有效的更改。

.st0 {
    fill: #fff
}

.st5 {
    fill: #c32034
}
<svg id="demo" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 337.32 386.57">
    <defs>
        <path id="shape" d="M51.24 372.52V52.27c0-15.4 12.6-28 28-28h180.79c15.4 0 28 12.6 28 28v320.25" fill="white" />
        <mask id="button-mask" width="1" height="1">
             <use href="#shape" fill="white" />
        </mask>
    </defs>
    <path id="endScreen" class="st0" d="M51.24 372.52V52.27c0-15.4 12.6-28 28-28h180.79c15.4 0 28 12.6 28 28v320.25" />
    <circle id="startScreen" class="st0" cx="167.67" cy="199.37" r="91" />
    <path id="base" fill="none" stroke="#c1a88b" stroke-width="4" stroke-linecap="round" stroke-miterlimit="10" d="M6 374.88h326.27" />
    <path id="phoneOutline" d="M302.12 372.43V55.31c0-25.15-21.05-45.73-46.78-45.73H82.26c-25.73 0-46.78 20.58-46.78 45.73v317.13" fill="none" stroke="#c1a88b" stroke-width="7.358" stroke-linecap="round" stroke-miterlimit="10" />
    <circle id="speakerSmall" cx="204.78" cy="43.04" r="4.4" fill="#c1a88b" />
    <path id="speakerLarge" fill="none" stroke="#c1a88b" stroke-width="9" stroke-linecap="round" stroke-miterlimit="10" d="M132.04 43.23h59.45" />
    <path id="startOrder" class="st5" d="M236.62 337.2H99.44c-6.6 0-12-5.4-12-12v-20.48c0-6.6 5.4-12 12-12h137.17c6.6 0 12 5.4 12 12v20.48c.01 6.6-5.39 12-11.99 12z" mask="url(#button-mask)" />
    <path id="star" class="st5" d="M168.66 91.97l4.94 10.01 11.04 1.6-7.99 7.79 1.89 11.01-9.88-5.2-9.89 5.2 1.89-11.01-7.99-7.79 11.05-1.6z" />
    <circle id="endOrder" cx="165.72" cy="313.53" r="36.5" fill="#89bd40" />
    <path id="check" fill="none" stroke="#fdfeff" stroke-width="7" stroke-miterlimit="10" d="M147.32 255.76l12.88 11.5 23.93-23.46" />
    <use href="#shape" fill="none" stroke="blue" />
</svg>

值得注意的是,这是一个仅适合您的具体情况的解决方案。如果 mask 定义了一个笔划,则该笔划周围的另一笔划无法像这样显示。实际上,该技术比 mask (任意图形结构)更适合剪辑路径(即纯形式)。

关于html - 在 SVG 中的蒙版上放置轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53120721/

相关文章:

php - 无法在 html 文本框中显示 0 值

html - 如何更改 Foundation 中的模态链接颜色?

svg - 如何将 svg 导入 antd -> Icon 组件

javascript - Jquery 脚本不起作用

c - 如何防止缓冲区溢出造成的密码屏蔽

html - 文本和边框之间的空间

javascript - 为什么我的 contenteditable div 的内容消失了?

javascript - 如何补偿圆形 slider 组件中的旋转容器 View ?

javascript - D3 : Separating data exit/remove/merge from drawing of elements

java - 如何使用参数屏蔽 Java 中除最后 4 个字符以外的所有字符串字符?