Home >Web Front-end >HTML Tutorial >Canvas draws rotating graphics
Rotate the elements drawn on the canvas:
1. When drawing, operate the coordinate axis status of the canvas: translate the canvas origin, rotate the coordinate axis, etc., to achieve the purpose of rotating the graphics
2. Manipulate DOM elements directly Rotate canvas
Manipulate the coordinate axis status of the canvas:
var rect = {w:200,h:50,x:100,y:100}; function rotateCanvas(){ var angle = document.getElementById('angle').value; ctx.clearRect(0,0,myCanvas.width,myCanvas.height); ctx.translate(rect.x+rect.w/2,rect.y+rect.h/2); //坐标原点平移至该图形的中心点 ctx.rotate(angle*Math.PI/180); //旋转坐标轴 //ctx.scale(0.5,0.5); ctx.translate(-(rect.x+rect.w/2),-(rect.y+rect.h/2)); //坐标原点平移至初始位置 //ctx.scale(1,1); ctx.fillRect(rect.x,rect.y,rect.w,rect.h); }