Canvas draws rotating graphics

高洛峰
Release: 2016-11-16 11:38:43
Original
1246 people have browsed it

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); 
    }
Copy after login

Canvas draws rotating graphics

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!