这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
CanvasRenderingContext2D
.resetTransform()
method of the Canvas 2D API resets the current transform to the identity matrix.
void ctx.resetTransform();
This example draws a rotated rectangle after modifying the matrix, and then resets the matrix using the
resetTransform()
方法。
<canvas id="canvas"></canvas>
rotate()
method rotates the transformation matrix by 45°. The
fillRect()
method draws a filled rectangle, adjusted according to that matrix.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Draw a rotated rectangle
ctx.rotate(45 * Math.PI / 180);
ctx.fillRect(60, 0, 100, 30);
// Reset transformation matrix to the identity matrix
ctx.resetTransform();
Whenever you're done drawing transformed shapes, you should call
resetTransform()
before rendering anything else. In this example, the first two shapes are drawn with a skew transformation, and the last two are drawn with the identity (regular) transformation.
<canvas id="canvas"></canvas>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Skewed rectangles
ctx.transform(1, 0, 1.7, 1, 0, 0);
ctx.fillStyle = 'gray';
ctx.fillRect(40, 40, 50, 20);
ctx.fillRect(40, 90, 50, 20);
// Non-skewed rectangles
ctx.resetTransform();
ctx.fillStyle = 'red';
ctx.fillRect(40, 40, 50, 20);
ctx.fillRect(40, 90, 50, 20);
skewed rectangles are gray ,和 non-skewed rectangles are red .
还可以使用
setTransform()
method to reset the current transform to the identity matrix, like so:
ctx.setTransform(1, 0, 0, 1, 0, 0);
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'CanvasRenderingContext2D.resetTransform' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
resetTransform
|
Chrome 31 | Edge 79 | Firefox 36 | IE No | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 36 | Opera Android No | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
不支持
实验。期望将来行为有所改变。
CanvasRenderingContext2D
CanvasRenderingContext2D
addHitRegion()
arc()
arcTo()
beginPath()
bezierCurveTo()
clearHitRegions()
clearRect()
clip()
closePath()
createImageData()
createLinearGradient()
createPattern()
createRadialGradient()
drawFocusIfNeeded()
drawImage()
drawWidgetAsOnScreen()
drawWindow()
ellipse()
fill()
fillRect()
fillText()
getImageData()
getLineDash()
getTransform()
isPointInPath()
isPointInStroke()
lineTo()
measureText()
moveTo()
putImageData()
quadraticCurveTo()
rect()
removeHitRegion()
resetTransform()
restore()
rotate()
save()
scale()
scrollPathIntoView()
setLineDash()
setTransform()
stroke()
strokeRect()
strokeText()
transform()
translate()