Canvas: rotate image with 'canvas' - Javascript Canvas

Javascript examples for Canvas:image

Description

Canvas: rotate image with 'canvas'

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script> 
      <style id="compiled-css" type="text/css">

canvas, img {/* w ww .ja  v a2  s  . c  om*/
   border:1px solid red;
}


      </style> 
      <script type="text/javascript">
    $(window).load(function(){
    });

      </script> 
   </head> 
   <body>
       canvas
      <br> 
      <canvas id="canvasH"></canvas> 
      <br>
      <br>
      <br>
      orig
      <br> 
      <img id="i" src="https://ssl.gstatic.com/gb/images/i1_71651352.png" onload="loaded();"> 
      <script>
    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();
    }

      </script>  
   </body>
</html>

Related Tutorials