大网数据广告活动
Web 标准与前端开发

DOMMatrixReadOnly: scale() method

· 52CE 我爱测

The **`scale()`** method of the DOMMatrixReadOnly interface creates a new matrix being the result of the original matrix with a scale transform applied.

Geometry Interfaces

The scale() method of the DOMMatrixReadOnly interface creates a new matrix being the result of the original matrix with a scale transform applied.

Syntax

scale(scaleX)
scale(scaleX, scaleY)
scale(scaleX, scaleY, scaleZ)
scale(scaleX, scaleY, scaleZ, originX)
scale(scaleX, scaleY, scaleZ, originX, originY)
scale(scaleX, scaleY, scaleZ, originX, originY, originZ)

Parameters

  • scaleX
    • : A multiplier for the scale value on the x-axis.
  • scaleY
    • : A multiplier for the scale value on the y-axis. If not supplied, this defaults to the value of scaleX.
  • scaleZ
    • : A multiplier for the scale value on the z-axis. If this value is anything other than 1, the resulting matrix will be 3D.
  • originX
    • : An x-coordinate for the origin of the transformation. If no origin is supplied, this defaults to 0.
  • originY
    • : A y-coordinate for the origin of the transformation. If no origin is supplied, this defaults to 0.
  • originZ
    • : A z-coordinate for the origin of the transformation. If no origin is supplied, this defaults to 0. If this value is anything other than 0, the resulting matrix will be 3D.

Return value

Returns a DOMMatrix containing a new matrix being the result of the matrix x and y dimensions being scaled by the given factor, centered on the origin given. The original matrix is not modified.

If a scale is applied about the z-axis, the resulting matrix will be a 4✕4 3D matrix.

Examples

This SVG contains three squares, one red, one blue, and one green, each positioned at the document origin:

<svg width="250" height="250" viewBox="0 0 25 25">
  <rect width="25" height="25" fill="red" />
  <rect id="transformed" width="25" height="25" fill="blue" />
  <rect id="transformedOrigin" width="25" height="25" fill="green" />
</svg>

This JavaScript first creates an identity matrix, then uses the scale() method to create a new matrix with a single parameter.

We test if the browser supports a six parameter scale() method by creating a new matrix using three parameters and observing its is2D property. If this is false then the third parameter has been accepted by the browser as a scaleZ parameter, making this a 3D matrix.

We then create a new matrix scaled about a given origin, using either three or six parameters depending on the browser support.

These new matrices are then applied to the blue and green squares as a transform, changing their dimensions and position. The red square is left in place.

const matrix = new DOMMatrixReadOnly();
const scaledMatrix = matrix.scale(0.5);

let scaledMatrixWithOrigin = matrix.scale(0.5, 25, 25);

// if the browser has interpreted these parameters as scaleX, scaleY, scaleZ, the resulting matrix is 3D
const browserExpectsSixParamScale = !scaledMatrixWithOrigin.is2D;
if (browserExpectsSixParamScale) {
  scaledMatrixWithOrigin = matrix.scale(0.5, 0.5, 1, 25, 25, 0);
}

document
  .querySelector("#transformed")
  .setAttribute("transform", scaledMatrix.toString());
document
  .querySelector("#transformedOrigin")
  .setAttribute("transform", scaledMatrixWithOrigin.toString());

Examples

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。