CanvasRenderingContext2D.transform() - Web APIs 编辑
The CanvasRenderingContext2D
.transform()
method of the Canvas 2D API multiplies the current transformation with the matrix described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.
Note: See also the setTransform()
method, which resets the current transform to the identity matrix and then invokes transform()
.
Syntax
void ctx.transform(a, b, c, d, e, f);
The transformation matrix is described by:
Parameters
a
(m11)- Horizontal scaling. A value of
1
results in no scaling. b
(m12)- Vertical skewing.
c
(m21)- Horizontal skewing.
d
(m22)- Vertical scaling. A value of
1
results in no scaling. e
(dx)- Horizontal translation (moving).
f
(dy)- Vertical translation (moving).
Examples
Skewing a shape
This example skews a rectangle both vertically (.2
) and horizontally (.8
). Scaling and translation remain unchanged.
HTML
<canvas id="canvas"></canvas>
JavaScript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.transform(1, .2, .8, 1, 0, 0);
ctx.fillRect(0, 0, 100, 100);
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D.transform' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
See also
- The interface defining this method:
CanvasRenderingContext2D
CanvasRenderingContext2D.setTransform()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论