Rotate a rectangle on the HTML5 canvas

Example of how you can rotate a rectangle on the HTML5 canvas.
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="600" height="300"></canvas>
    <script>

    var canvas = document.getElementById('myCanvas'); 
    var ctx = canvas.getContext('2d'); 

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = 'red';
        ctx.rotate(Math.PI / 5);
        ctx.fillRect(100, 10, 100, 100); 

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

Responses

0 Replies