方法:translate(x,y):x,y代表移动后x,y坐标
方法:rotate(angle), 旋转角度,整数为逆时针,负数为顺时针。
实例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <canvas id="c2d" width="500" height="500">不支持canvas</canvas> <script> const canvas = document.querySelector('#c2d'); if(canvas.getContext) { const ctx = canvas.getContext('2d'); ctx.fillRect(0,0,10,30); ctx.translate(10,30); ctx.fillRect(0,0,10,30); ctx.translate(10,30); ctx.rotate(-Math.PI * 1/2); ctx.fillStyle='red'; ctx.fillRect(0,0,10,30); } </script></body></html>