<clipPath>

<clipPath> SVG 元素定義了一個剪輯路徑,供 clip-path 屬性使用。

剪輯路徑限制了可以應用繪製的區域。從概念上講,繪製中位於剪輯路徑邊界區域之外的部分不會被繪製。

示例

html
<svg viewBox="0 0 100 100">
  <clipPath id="myClip">
    <!--
      Everything outside the circle will be
      clipped and therefore invisible.
    -->
    <circle cx="40" cy="35" r="35" />
  </clipPath>

  <!-- The original black heart, for reference -->
  <path
    id="heart"
    d="M10,30 A20,20,0,0,1,50,30 A20,20,0,0,1,90,30 Q90,60,50,90 Q10,60,10,30 Z" />

  <!--
    Only the portion of the red heart
    inside the clip circle is visible.
  -->
  <use clip-path="url(#myClip)" href="#heart" fill="red" />
</svg>
css
/* With a touch of CSS for browsers who *
 * implemented the r Geometry Property. */

@keyframes openYourHeart {
  from {
    r: 0;
  }
  to {
    r: 60px;
  }
}

#myClip circle {
  animation: openYourHeart 15s infinite;
}

剪輯路徑在概念上等效於引用元素的自定義視口。因此,它會影響元素的渲染,但不會影響元素的固有幾何形狀。被剪輯元素(即,透過 clip-path 屬性或引用元素的子元素引用 <clipPath> 元素的元素)的邊界框必須保持與未被剪輯時相同。

預設情況下,在剪輯區域上不會分派 pointer-events。例如,半徑為 10 的圓被剪輯到半徑為 5 的圓,則在較小半徑之外將不會接收“點選”事件。

屬性

clipPathUnits

定義 <clipPath> 元素內容的座標系。值型別userSpaceOnUse|objectBoundingBox預設值userSpaceOnUse可動畫

使用上下文

類別
允許的內容任意數量的以下元素,以任意順序
動畫元素
描述性元素
形狀元素
<text><use>

規範

規範
CSS 遮罩模組級別 1
# ClipPathElement

瀏覽器相容性

BCD 表格僅在啟用 JavaScript 的瀏覽器中載入。