SVGPatternElement: patternContentUnits 屬性

Baseline 已廣泛支援

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

SVGPatternElement 介面的只讀屬性 patternContentUnits 反映了給定 <pattern> 元素的 patternContentUnits 屬性。它指定了圖案內容的座標系統,並取自 SVGUnitTypes 中定義的其中一個常量。

一個 SVGAnimatedEnumeration 物件。

示例

給定以下 SVG

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern
      id="pattern1"
      width="10"
      height="10"
      patternContentUnits="userSpaceOnUse">
      <circle cx="5" cy="5" r="5" fill="blue" />
    </pattern>
    <pattern
      id="pattern2"
      width="10"
      height="10"
      patternContentUnits="objectBoundingBox">
      <circle cx="5" cy="5" r="5" fill="red" />
    </pattern>
  </defs>
  <rect x="0" y="0" width="100" height="100" fill="url(#pattern1)" />
  <rect x="100" y="0" width="100" height="100" fill="url(#pattern2)" />
</svg>

我們可以訪問 patternContentUnits 屬性

js
const patterns = document.querySelectorAll("pattern");

console.log(patterns[0].patternContentUnits.baseVal); // output: 1 (SVGUnitTypes.USERSPACEONUSE)
console.log(patterns[1].patternContentUnits.baseVal); // output: 2 (SVGUnitTypes.OBJECTBOUNDINGBOX)

規範

規範
Scalable Vector Graphics (SVG) 2
# __svg__SVGPatternElement__patternContentUnits

瀏覽器相容性

另見