Home  >  Article  >  Web Front-end  >  Canvas draws rotating graphics

Canvas draws rotating graphics

高洛峰
高洛峰Original
2016-11-16 11:38:431290browse

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); 
    }

Canvas draws rotating graphics

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn