遮罩單位

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

瀏覽器相容性