Image crop after rotate by canvas

前端 未结 3 992
庸人自扰
庸人自扰 2020-12-09 14:10

I have a html5 canvas. I want to draw/rotate the image in the canvas and image size does not change.I set width/height of canvas base on size of image.I have a problem to r

3条回答
  •  盖世英雄少女心
    2020-12-09 14:37

    If you rotate your image, you can recalculate the boundingbox needed to contain it.

    enter image description hereenter image description here

    This code will takes width/height/rotation angle and returns the new bounding box size:

    function newSize(w,h,a){
        var rads=a*Math.PI/180;
        var c = Math.cos(rads);
        var s = Math.sin(rads);
        if (s < 0) { s = -s; }
        if (c < 0) { c = -c; }
        size.width = h * s + w * c;
        size.height = h * c + w * s ;
    }
    

    Here is example code and a Fiddle: http://jsfiddle.net/m1erickson/h65yr/

    
    
    
     
    
        
        
    
    
    
    
    
    
    

提交回复
热议问题