SVGSVGElement: checkIntersection() 方法

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

SVGSVGElement 介面的 checkIntersection() 方法用於檢查給定元素的渲染內容是否與提供的矩形相交。

只有當同一個圖形元素可以作為指標事件的目標時,才被視為匹配,具體定義請參見 pointer-events 處理。

語法

js
checkIntersection(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="80" cy="80" r="50" fill="blue" />
</svg>
<button id="checkIntersectionBtn">Check Intersection</button>
<pre id="result"></pre>
js
const svgElement = document.getElementById("exampleSVG");
const checkRect = document.getElementById("checkRect");
const targetElement = document.getElementById("targetElement");
const resultDisplay = document.getElementById("result");

document
  .getElementById("checkIntersectionBtn")
  .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 isIntersecting = svgElement.checkIntersection(targetElement, rect);
    resultDisplay.textContent = `Does the circle intersect with the rectangle? ${isIntersecting}`;
  });

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGSVGElement__checkIntersection

瀏覽器相容性

另見