图案单位

Baseline 已广泛支持

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

patternUnits 属性指示用于 <pattern> 元素的几何属性的坐标系。

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

示例

html
<svg viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
  <!-- All geometry properties are relative to the current user space -->
  <pattern
    id="p1"
    x="12.5"
    y="12.5"
    width="25"
    height="25"
    patternUnits="userSpaceOnUse">
    <circle cx="10" cy="10" r="10" />
  </pattern>

  <!-- All geometry properties are relative to the target bounding box -->
  <pattern
    id="p2"
    x=".125"
    y=".125"
    width=".25"
    height=".25"
    patternUnits="objectBoundingBox">
    <circle cx="10" cy="10" r="10" />
  </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>patternUnits 定义了元素几何属性(xywidthheight)所使用的坐标系。

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

此值表示几何属性的所有坐标均指图案应用时定义的用户坐标系

objectBoundingBox

此值表示几何属性的所有坐标均表示应用图案的元素的边界框的分数或百分比。边界框可被视为与将 <pattern> 的内容绑定到 "0 0 1 1" viewBox 时相同。

规范

规范
Scalable Vector Graphics (SVG) 2
# PatternElementPatternUnitsAttribute

浏览器兼容性