HTML Canvas Scale off and on

Description

HTML Canvas Scale off and on

View in separate window

<!DOCTYPE html>

<html>
  <head>
    <title>Pushing canvas further</title>
    <meta charset="utf-8">
    //from ww w . j a v  a 2  s .  co m
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    
    <script type="text/javascript">
      $(document).ready(function() {  
        var canvas = $("#myCanvas");
        var context = canvas.get(0).getContext("2d");
        
        // Scaling off
        context.fillRect(150, 150, 100, 100);
        
        // Scaling on, but the origin is being scaled too
        context.scale(2, 2);
        context.fillRect(150, 150, 100, 100);
        
        // Scaling on, without scaling the origin
        context.save();
        context.translate(150, 150);
        context.scale(2, 2);
        context.fillRect(0, 0, 100, 100);
        
        context.restore();
        context.fillRect(0, 0, 100, 100);
        
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
      <!-- Insert fallback content here -->
    </canvas>
  </body>
</html>



PreviousNext

Related