图案内容单位

Baseline 已广泛支持

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

patternContentUnits 属性指示要用于 <pattern> 元素内容的坐标系。

注意:如果 <pattern> 元素上指定了 viewBox 属性,则此属性无效。

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

示例

html
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  <!--
  A pattern tile that content coordinates and values are
  computed against the current coordinate user space.
  Note that the size of the tile is computed against
  the bounding box of the target element
  -->
  <pattern
    id="p1"
    width="20%"
    height="20%"
    patternContentUnits="userSpaceOnUse">
    <circle cx="10" cy="10" r="10" />
  </pattern>

  <!--
  A pattern tile that content coordinates and values are
  computed against the bounding box of the target element.
  Note that the size of the tile is also computed against
  the bounding box of the target element
  -->
  <pattern
    id="p2"
    width="20%"
    height="20%"
    patternContentUnits="objectBoundingBox">
    <circle cx=".1" cy=".1" r=".1" />
  </pattern>

  <!-- Left square with user space tiles -->
  <rect x="10" y="10" width="80" height="80" fill="url(#p1)" />

  <!-- Right square with bounding box tiles -->
  <rect x="110" y="10" width="80" height="80" fill="url(#p2)" />
</svg>

元素

你可以在下面部分描述的 SVG 元素中使用此属性。

<pattern>

对于 <pattern>patternContentUnits 定义了元素内容所使用的坐标系。

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

此值表示 <pattern> 元素内的所有坐标都引用在创建图案瓦片时定义的the user coordinate system(用户坐标系)。

objectBoundingBox

此值表示 <pattern> 元素内的所有坐标都相对于应用该图案的元素的 bounding box(边界框)。边界框可以被视为与图案瓦片的宽度和高度均为 100% 时,内容具有 "0 0 1 1" viewBox 的情况相同。

规范

规范
Scalable Vector Graphics (SVG) 2
# PatternElementPatternContentUnitsAttribute

浏览器兼容性