patternContentUnits

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Thuộc tính patternContentUnits chỉ định hệ tọa độ nào sẽ được dùng cho nội dung của phần tử <pattern>.

Note: Thuộc tính này không có tác dụng nếu thuộc tính viewBox được chỉ định trên phần tử <pattern>.

Bạn có thể dùng thuộc tính này với phần tử SVG sau:

Ví dụ

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>

Các phần tử

Bạn có thể dùng thuộc tính này với các phần tử SVG được mô tả trong các phần bên dưới.

<pattern>

Đối với <pattern>, patternContentUnits xác định hệ tọa độ được dùng cho nội dung của phần tử.

Giá trị userSpaceOnUse | objectBoundingBox
Giá trị mặc định userSpaceOnUse
Có thể animate
userSpaceOnUse

Giá trị này cho biết mọi tọa độ bên trong phần tử <pattern> đều tham chiếu đến hệ tọa độ người dùng như được định nghĩa khi tile pattern được tạo.

objectBoundingBox

Giá trị này cho biết mọi tọa độ bên trong phần tử <pattern> đều tương đối với hộp bao của phần tử mà pattern được áp dụng lên. Một hộp bao có thể được xem như thể nội dung của <pattern> được ràng buộc với một viewBox "0 0 1 1" cho một tile pattern có chiều rộng và chiều cao bằng 100%.

Thông số kỹ thuật

Specification
Scalable Vector Graphics (SVG) 2
# PatternElementPatternContentUnitsAttribute

Tương thích trình duyệt