SVG Mask Transparent Circle - 只显示没有黄色背景的文本

SVG Mask Transparent Circle - Show only text without yellow background

您好,我是 svg 蒙版的新手,我遇到以下问题:

圆圈是一个 svg 蒙版,它跟随图像上的光标。 图像后面是另一个带有标签云的图像,它是具有透明背景的 png。

现在我只想看到图片后面没有黄色的文字 来自网站的背景。这可能吗?如果有人能帮助我,我会很高兴。

这是我的演示代码:

https://jsfiddle.net/ayyuqrw4/1/

    <svg>
        <defs>
            <mask id="cursorMask" maskUnits="objectBoundingBox" maskContentUtils="objectBoundingBox">
                <g>
                    <rect x="0" y="0" width="1600" height="700" fill="#FFFFFF" />
                    <circle cx="0" cy="0" r="60" stroke="black" stroke-width="0" fill="black" fill-opacity="1" />
                </g>
            </mask>
        </defs>
        <image width="1600" height="700" xlink:href="https://41.media.tumblr.com/e91c999c320c29a28053c6a933da1e81/tumblr_mkjnibe1xH1s9yt1no1_500.png" />
        <image width="1600" height="700" xlink:href="http://www.liftopia.com/media/product/275/102102_Ski-Banff-Lake-Louise-Sunshine-1-Day-Lift-Tickets.jpg" />
    </svg>

var img = document.getElementsByTagName("image")[1];
var imgPos = img.getBoundingClientRect();
var imgX = imgPos.left;
var imgY = imgPos.top;
var rect = document.getElementsByTagName("circle")[0];
var rectHalfWidth = rect.getAttribute("width") / 2;
var rectHalfHeight = rect.getAttribute("height") / 2;
img.addEventListener("mousemove", function (e) {
    rect.setAttribute("cx", e.clientX - imgX - rectHalfWidth);
    rect.setAttribute("cy", e.clientY - imgY - rectHalfHeight);
}, false);

body {
    background-color: yellow;
}

svg {
    width: 1600px;
    height: 700px;
}
image:hover {
    mask: url("#cursorMask");
}

这就是我想要的:

Final Result

将原始图像贴在所有内容后面。

请注意我是如何调整你的 getElementsByTagName 以适应的,因为我很懒,但如果你给你想要的图像 select 一个 ID 并转换为 getElementById 会更好。

var img = document.getElementsByTagName("image")[2];
var imgPos = img.getBoundingClientRect();
var imgX = imgPos.left;
var imgY = imgPos.top;
var rect = document.getElementsByTagName("circle")[0];
var rectHalfWidth = rect.getAttribute("width") / 2;
var rectHalfHeight = rect.getAttribute("height") / 2;
img.addEventListener("mousemove", function (e) {
    rect.setAttribute("cx", e.clientX - imgX - rectHalfWidth);
    rect.setAttribute("cy", e.clientY - imgY - rectHalfHeight);
}, false);
body {
    background-color: yellow;
}

svg {
    width: 1600px;
    height: 700px;
}
image:hover {
    mask: url("#cursorMask");
}
    <svg>
        <defs>
            <mask id="cursorMask" maskUnits="objectBoundingBox" maskContentUtils="objectBoundingBox">
                <g>
                    <rect x="0" y="0" width="1600" height="700" fill="#FFFFFF" />
                    <circle cx="0" cy="0" r="60" stroke="black" stroke-width="0" fill="black" fill-opacity="1" />
                </g>
            </mask>
        </defs>
        <image width="1600" height="700" xlink:href="http://www.liftopia.com/media/product/275/102102_Ski-Banff-Lake-Louise-Sunshine-1-Day-Lift-Tickets.jpg" />
        <image width="1600" height="700" xlink:href="https://41.media.tumblr.com/e91c999c320c29a28053c6a933da1e81/tumblr_mkjnibe1xH1s9yt1no1_500.png" />
        <image width="1600" height="700" xlink:href="http://www.liftopia.com/media/product/275/102102_Ski-Banff-Lake-Louise-Sunshine-1-Day-Lift-Tickets.jpg" />
    </svg>