<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

浏览器兼容性

另见