<clipPath>

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

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

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

使用語境

分類None
允許內容可包含任意數量、任意順序的下列元素
動畫元素
描述性元素
形狀元素
<text>, <use>

屬性

剪下路徑單位

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

DOM 介面

此元素實現了 SVGClipPathElement 介面。

示例

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 的圓,將不會在較小半徑之外接收“點選”事件。

規範

規範
CSS 蒙版模組 Level 1
# ClipPathElement

瀏覽器相容性

另見