遮罩內容單位

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

maskContentUnits 屬性指示使用哪個座標系來表示 <mask> 元素的 contents。

你可以將此屬性與以下 SVG 元素一起使用

示例

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

  <mask id="myMask2" maskContentUnits="objectBoundingBox">
    <rect fill="black" x="0" y="0" width="100%" height="100%" />
    <circle fill="white" cx=".5" cy=".5" 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>maskContentUnits 定義了元素內容的座標系。

userSpaceOnUse | objectBoundingBox
預設值 userSpaceOnUse
可動畫的
userSpaceOnUse

此值表示 <mask> 元素內的所有座標都引用蒙版建立時定義的座標系。

objectBoundingBox

此值表示 <mask> 元素內的所有座標都相對於應用蒙版元素的邊界框。邊界框可以被認為與將 <mask> 的內容繫結到 "0 0 1 1" viewBox 時相同。

規範

規範
CSS 蒙版模組 Level 1
# element-attrdef-mask-maskcontentunits

瀏覽器相容性