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

标签 javascript html image svg mask

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

圆圈是一个 svg 蒙版,它跟随图像上的光标。 图像后面是另一个带有 tagcloud 的图像,它是具有透明背景的 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 以适应的,因为我很懒,但如果你给你想要的图像选择一个 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>

关于javascript - SVG Mask Transparent Circle - 只显示没有黄色背景的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616855/

相关文章:

html - 填充显示 : flex

html - float :left; problem in ie6?

javascript - 使用javascript检测图像模糊

Javascript toLocaleTimeString() 不工作

javascript - Tesseract Node.js - 使用 "lang"选项导致崩溃

javascript - 如何使用 Highcharts 从数据库传入一系列数据来制作多个 Y 轴

html - 带有 img 的圆形 div width<height height<width HTML/CSS

JavaScript:按类单击元素不起作用

javascript - Jquery 幻灯片菜单 x 滚动问题

image - 带有非 map 图像的 TileMill