I'm rotating an image by dragging using canvas rotate() function. Obviously the image gets cut during rotation.
To tackle this problem I followed some stackoverflow instructions and implemented something like this:
//Resizing Canvas - this part is creating problem
var w = imgW;
var h = imgH;
var newWidth,newHeight;
var c = Math.cos(15*rot*0.0174532925);
var s = Math.sin(15*rot*0.0174532925);
if (s < 0) { s = -s; }
if (c < 0) { c = -c; }
newWidth = h * s + w * c;
newHeight = h * c + w * s ;
cnv2.width = newWidth;
cnv2.height= newHeight;
translateX = cnv2.width/axisFactor;
translateY = cnv2.height/axisFactor
ctx2.clearRect(0,0,cnv2.width,cnv2.height);
ctx2.save();
ctx2.translate(translateX,translateY);
ctx2.rotate(15*rot*0.0174532925);
ctx2.drawImage(img,-translateX,-translateY);
ctx2.restore();
Though I'm able to get the correct size of the canvas the redraw of the image still gets cut.
What am I missing?
Heres the fiddle:http://jsfiddle.net/anubhabB/wwas5jag/
EDIT: Problem solved by correcting
ctx2.drawImage(img,-translateX,-translateY);
with
ctx2.drawImage(img,-img.width/axisFactor,-img.height/axisFactor);
But this doesn't work for any axis which is not 1/2 of the image, say axis factor 3!
How to tackle this?