Clip the image and draw the clipped part on the canvas - Javascript Canvas Reference

Javascript examples for Canvas Reference:drawImage

Description

Clip the image and draw the clipped part on the canvas

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Image to use:</p>
<img id="scream" src="http://java2s.com/resources/c.png" alt="The Scream" width="220" height="277">

<p>Canvas:</p>
<canvas id="myCanvas" width="300" height="250" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
window.onload = function() {/*from ww w. ja va  2s . c om*/
    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    var img = document.getElementById("scream");
    ctx.drawImage(img, 90, 130, 50, 60, 10, 10, 50, 60);
};
</script>


</body>
</html>

Related Tutorials