Dmitry
Dmitry

Reputation: 867

Canvas: rotate image with 'canvas'

Rotating image and resize canvas.

        var img = document.getElementById("i");
        width = img.width;
        height = img.height;

        canvasH.width = width;
        canvasH.height = height;
        ctxH.clearRect(0, 0, width, height);
        ctxH.save();
        ctxH.translate(width / 2, height / 2);
        ctxH.rotate(90 * Math.PI / 180);
        ctxH.translate(-(width / 2), -(height / 2));
        ctxH.drawImage(img, 0, 0);
        ctxH.restore();

        var w = canvasH.width;
// Resize canvas to meet rotated image size
// Comment last two lines and see how image rotated
        canvasH.width = height;
        canvasH.height = w;

Canvas rotated (resized) but image out of visible area.

What can I do to get rotated image?

FIDDLE: http://jsfiddle.net/ouh5845c/1/

Upvotes: 1

Views: 220

Answers (2)

Andrija Petrovic
Andrija Petrovic

Reputation: 209

It is important to know that changing the width/height of canvas implicitly clears the canvas. So, whatever you have to do related to sizing the canvas, do it before rendering to it.

Here's a trigonometrically correct approach, working for any angle:

        var img = document.getElementById("i"),
            angrad = angle * Math.PI /180,
            sin = Math.sin(angrad),
            cos = Math.cos(angrad);
        width = Math.abs(img.width*cos)+Math.abs(img.height*sin);
        height = Math.abs(img.height*cos)+Math.abs(img.width*sin);
        console.log(img.width,img.height,width,height);

        canvasH.width = width;
        canvasH.height = height;
        ctxH.clearRect(0, 0, width, height);
        ctxH.save();
        ctxH.translate(width / 2, height / 2);
        ctxH.rotate(angrad);
        ctxH.translate(-img.width / 2, -img.height / 2);
        ctxH.drawImage(img, 0, 0);
        ctxH.restore();

http://jsfiddle.net/ouh5845c/5/

===========================

and the following story you can forget:

Now, because you need to rotate, width and height are interpreted differently before rotation and after rotation. (Of course, for a "hairy" situation when the angle is not 90 degrees, some trigonometry would come in handy).

I believe this fiddle does what you needed:

        var img = document.getElementById("i");
        width = img.width;
        height = img.height;

        canvasH.width = height;
        canvasH.height = width;
        ctxH.clearRect(0, 0, width, height);
        ctxH.save();
        ctxH.translate(height / 2, width / 2);
        ctxH.rotate(90 * Math.PI / 180);
        ctxH.translate(-width / 2, -height / 2);
        ctxH.drawImage(img, 0, 0);
        ctxH.restore();

http://jsfiddle.net/ouh5845c/4/

Upvotes: 3

Sam Greenhalgh
Sam Greenhalgh

Reputation: 6136

If you are trying to rotate the image 90 degrees this should work.

var canvasH = document.getElementById("canvasH"),
        ctxH = canvasH.getContext("2d"),
        x = 0,
        y = 0,
        width = 0,
        height = 0,
        angle = 180,
        timeOut = null;

function loaded() {
        var img = document.getElementById("i");
        canvasH.width = img.height;
        canvasH.height = img.width;
        ctxH.clearRect(0, 0, img.width, img.height);
        ctxH.save();
        ctxH.translate(img.height, 0);
        ctxH.rotate(1.57079633);
        ctxH.drawImage(img, 0, 0);
        ctxH.restore();
}

jsFiddle

Upvotes: 0

Related Questions