Resizing Canvas by adding space on top and left - Javascript Canvas

Javascript examples for Canvas:Example

Description

Resizing Canvas by adding space on top and left

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://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*from   w  ww.j  a  v  a2 s .c o  m*/
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(0,0,50,50);
ctx.fillRect(150,0,50,50);
ctx.fillRect(150,150,50,50);
ctx.fillRect(0,150,50,50);
var button = document.getElementById('button1');
button.addEventListener('click', function() {
    var inMem = document.createElement('canvas');
    inMem.width = can.width;
    inMem.height = can.height;
    var inMemCtx = inMem.getContext('2d');
    inMemCtx.drawImage(can, 0, 0);
    var oldw = can.width;
    var oldh = can.height;
    can.width = 300;
    can.height = 300;
    var offsetX = 300 - oldw;
    var offsetY = 300 - oldh;
    ctx.drawImage(inMem, offsetX, offsetY);
}, false);
    });

      </script> 
   </head> 
   <body> 
      <canvas id="canvas1" width="200" height="200" style="border: 1px solid black;"></canvas> 
      <br> 
      <input id="button1" type="button" value="enlarge">  
   </body>
</html>

Related Tutorials