HTML5 Game - Scaling and flipping Image

Description

Scaling and flipping Image

Demo

ResultView the demo in separate window

<!DOCTYPE html>

<html>
  <head>
    <title>Manipulating images and video</title>
    <meta charset="utf-8">
    /*from  w  w  w .ja v a 2s  .c o  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() {  
        let canvas = $("#myCanvas");
        let context = canvas.get(0).getContext("2d");
  
        // Scaling and flipping
        let image = new Image();
        image.src = "http://java2s.com/style/download.png";
        $(image).load(function() {
          // Top left
          context.translate(50, 50);
          context.drawImage(image, 0, 0, 500, 500, 0, 0, 200, 200);
          
          // Bottom left
          context.setTransform(1, 0, 0, 1, 0, 0); // Reset the transformation matrix
          context.translate(50, 450);
          context.scale(1, -1);
          
          context.drawImage(image, 0, 0, 500, 500, 0, 0, 200, 200);
        
          // Bottom right
          context.setTransform(1, 0, 0, 1, 0, 0);
          context.translate(450, 450);
          context.scale(-1, -1);
          
          context.drawImage(image, 0, 0, 500, 500, 0, 0, 200, 200);
          
          // Top right
          context.setTransform(1, 0, 0, 1, 0, 0);
          context.translate(450, 50);
          context.scale(-1, 1);
          
          context.drawImage(image, 0, 0, 500, 500, 0, 0, 200, 200);
        });
      
      });
    </script>
  </head>
  
  <body>
    <canvas id="myCanvas" width="500" height="500">
      <!-- Insert fallback content here -->
    </canvas>
  </body>
</html>

Related Topic