We would like to know how to scale and flip an image.
<!--from ww w . j a v a 2s . c om-->
<!DOCTYPE html>
<html>
<head>
<title>Manipulating images and video</title>
<meta charset="utf-8">
<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 and flipping
var image = new Image();
image.src = "http://www.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>
The code above is rendered as follows: