SVGSVGElement: checkEnclosure() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

SVGSVGElement 接口的 checkEnclosure() 方法用于检查给定元素的渲染内容是否完全包含在提供的矩形区域内。

每个候选图形元素仅当与 pointer-events 处理中定义的可以作为指针事件目标的图形元素相同时,才被视为匹配。

语法

js
checkEnclosure(element, rect)

参数

element

一个代表要检查的 SVG 图形元素的 Element

rect

一个定义要进行检查的矩形的 SVGRect 对象。

返回值

布尔值。

示例

检查一个元素是否包含在矩形内

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

浏览器兼容性

另见