SVGPatternElement: preserveAspectRatio 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGPatternElement 接口的只读属性 preserveAspectRatio 反映了给定元素的 preserveAspectRatio 属性。它定义了如何缩放图案内容以适应给定空间,同时保留其纵横比。

一个 SVGAnimatedPreserveAspectRatio 对象。

示例

给定以下 SVG,我们可以使用 preserveAspectRatio 属性来检索图案元素的缩放行为

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

我们可以访问 preserveAspectRatio 属性。

js
const pattern = document.querySelector("pattern");

console.log(pattern.preserveAspectRatio.baseVal); // output: SVGPreserveAspectRatio {align: 1, meetOrSlice: 1}

规范

规范
Scalable Vector Graphics (SVG) 2
# __svg__SVGFitToViewBox__preserveAspectRatio

浏览器兼容性

另见