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

DOMMatrix: scaleSelf() method

· 52CE 我爱测

The **`scaleSelf()`** method of the DOMMatrix interface is a mutable transformation method that modifies a matrix by applying a specified scaling factor, centered on the given origin, with a default origin of `(0, 0)`, returning the scaled matrix.

Geometry Interfaces

The scaleSelf() method of the DOMMatrix interface is a mutable transformation method that modifies a matrix by applying a specified scaling factor, centered on the given origin, with a default origin of (0, 0), returning the scaled matrix.

To scale a matrix without mutating it, see DOMMatrixReadOnly.scale(), which creates a new scaled matrix while leaving the original unchanged.

Syntax

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

Parameters

  • scaleX
    • : A multiplier for the scale value on the x-axis. If not supplied, this defaults to 1.
  • 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 itself; a DOMMatrix.

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

Examples

This SVG contains two semi-opaque squares, one red and one blue, each positioned at the document origin:

<svg viewBox="0 0 50 50" height="200">
  <rect width="25" height="25" fill="#ff000099" />
  <rect id="transformed" width="25" height="25" fill="#0000ff99" />
</svg>

This JavaScript first creates a matrix, then scales the matrix to one that halves the width and doubles the height, using the scaleSelf() method.

The matrix is then applied to the blue square as a transform, changing its dimensions and position. The red square is left unchanged.

const matrix = new DOMMatrix();
matrix.scaleSelf(0.5, 2);

document
  .querySelector("#transformed")
  .setAttribute("transform", matrix.toString());

Examples

Specifications

Browser compatibility

See also

  • DOMMatrixReadOnly.scale()
  • DOMMatrix.scale3dSelf()
  • CSS transform property and the transform-function/scale and transform-function/matrix functions
  • CSS transforms module
  • SVG transform attribute
  • CanvasRenderingContext2D interface CanvasRenderingContext2D.transform() method

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

资料来源与适用范围

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

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

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