how to display part of the canvas after scaling - Javascript Canvas

Javascript examples for Canvas:Example

Description

how to display part of the canvas after scaling

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.8.3.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){//from   ww w .  jav  a 2 s .co m
var c = document.getElementById("mycanvas");
var ctx = c.getContext("2d");
var img = document.createElement("img");
img.src = "https://www.java2s.com/style/demo/Google-Chrome.png";
function renderToScale(s) {
    ctx.clearRect(0,0,c.width,c.height);

    ctx.save();
    ctx.scale(s, s);
    var halfx = c.width / (2 * s);
    var halfy = c.height / (2 * s);
    var clipx = Math.min(img.width, halfx);
    var clipy = Math.min(img.height, halfy);
    ctx.drawImage(img,0,0,clipx,clipy,0,0,clipx,clipy);
    ctx.drawImage(img,0,0,clipx,clipy,halfx,halfy,clipx,clipy);
    ctx.restore();
}
$("form").submit(function() {
    renderToScale($(":input[name=scale]").val());
    return false;
}).submit();
    });

      </script> 
   </head> 
   <body> 
      <canvas id="mycanvas" width="400" height="400"></canvas> 
      <form> 
         <label>
            Scale 
            <input type="text" name="scale" value="1"> 
         </label> 
         <button type="submit">Submit</button> 
      </form>  
   </body>
</html>

Related Tutorials