遮罩单位

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流浏览器均已支持。

maskUnits 属性指示为 <mask> 元素设置的几何属性应使用哪个坐标系。

你可以将此属性与以下 SVG 元素一起使用

示例

html
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <mask
    id="myMask1"
    maskUnits="userSpaceOnUse"
    x="20%"
    y="20%"
    width="60%"
    height="60%">
    <rect fill="black" x="0" y="0" width="100%" height="100%" />
    <circle fill="white" cx="50" cy="50" r="35" />
  </mask>

  <mask
    id="myMask2"
    maskUnits="objectBoundingBox"
    x="20%"
    y="20%"
    width="60%"
    height="60%">
    <rect fill="black" x="0" y="0" width="100%" height="100%" />
    <circle fill="white" cx="50" cy="50" r="35" />
  </mask>

  <!-- Some reference rect to materialized the mask -->
  <rect id="r1" x="0" y="0" width="45" height="45" />
  <rect id="r2" x="0" y="55" width="45" height="45" />
  <rect id="r3" x="55" y="55" width="45" height="45" />
  <rect id="r4" x="55" y="0" width="45" height="45" />

  <!-- The first 3 rect are masked with useSpaceOnUse units -->
  <use mask="url(#myMask1)" href="#r1" fill="red" />
  <use mask="url(#myMask1)" href="#r2" fill="red" />
  <use mask="url(#myMask1)" href="#r3" fill="red" />

  <!-- The last rect is masked with objectBoundingBox units -->
  <use mask="url(#myMask2)" href="#r4" fill="red" />
</svg>

mask

对于 <mask> 元素,maskUnits 定义了该元素几何属性(xywidthheight)所使用的坐标系。

userSpaceOnUse | objectBoundingBox
默认值 objectBoundingBox
可动画的
userSpaceOnUse

此值表示几何属性的所有坐标都指向创建蒙版时定义的用户坐标系

objectBoundingBox

此值表示几何属性的所有坐标都表示应用于蒙版元素的边界框的分数或百分比。可以将边界框视为与蒙版内容的 "0 0 1 1" viewBox 相同。

规范

规范
CSS 蒙版模块 Level 1
# element-attrdef-mask-maskunits

浏览器兼容性