SVGSVGElement: checkEnclosure() 方法
SVGSVGElement
接口的 checkEnclosure()
方法用于检查给定元素的渲染内容是否完全包含在提供的矩形区域内。
每个候选图形元素仅当与 pointer-events
处理中定义的可以作为指针事件目标的图形元素相同时,才被视为匹配。
语法
js
checkEnclosure(element, rect)
参数
返回值
布尔值。
示例
检查一个元素是否包含在矩形内
html
<svg id="exampleSVG" width="200" height="200">
<rect
id="checkRect"
x="10"
y="10"
width="100"
height="100"
fill="none"
stroke="red" />
<circle id="targetElement" cx="50" cy="50" r="30" fill="blue" />
</svg>
<button id="checkEnclosureBtn">Check Enclosure</button>
<pre id="result"></pre>
js
const svgElement = document.getElementById("exampleSVG");
const checkRect = svgElement.getElementById("checkRect");
const targetElement = svgElement.getElementById("targetElement");
const resultDisplay = document.getElementById("result");
document.getElementById("checkEnclosureBtn").addEventListener("click", () => {
const rect = svgElement.createSVGRect();
rect.x = checkRect.x.baseVal.value;
rect.y = checkRect.y.baseVal.value;
rect.width = checkRect.width.baseVal.value;
rect.height = checkRect.height.baseVal.value;
const isEnclosed = svgElement.checkEnclosure(targetElement, rect);
resultDisplay.textContent = `Is the circle enclosed in the rectangle? ${isEnclosed}`;
});
规范
规范 |
---|
Scalable Vector Graphics (SVG) 2 # __svg__SVGSVGElement__checkEnclosure |
浏览器兼容性
加载中…