Form
context.drawImage(imageObj,x,y)
context.drawImage(imageObj,x,y,w,h)
context.drawImage(imageObj,srcX,srcY,srcW,srcH,x,y,w,h)
Parameters
parameter |
description |
imageObj |
secifies the image object |
x |
specifies the X coordinate |
y |
specifies the Y coordinate |
w |
specifies the width of image to display |
h |
specifies the height of image to display |
srcX |
specifies the start X coordinate on an image to display (clipping X coordinate) |
srcY |
specifies the start Y coordinate on an image to display (clipping Y coordinate) |
srcW |
specifies the width of image to display (clipping width) |
srcH |
specifies the height of image to display (clipping height) |
Explanation
The drawimage displays an image in the specified position. The display processing is different depending on parameter numbers, and so pay attention on this point.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=shift_jis">
<title>Sample</title>
<script type="text/javascript"><!--
function drawPoint()
{
canvasObj = document.getElementById("imgCanvas");
conObj = canvasObj.getContext("2d");
imgObj = new Image(120,80);
imgObj.src = "photo.jpg";
x = Math.random() * 200;
y = Math.random() * 160;
conObj.drawImage(imgObj,x,y);
}
// --></script> </head>
<body onload="setInterval('drawPoint()',100)">
<h1>Sample</h1>
<canvas id="imgCanvas" width="320" height="240" style="border:1px black solid"></canvas>
<img src="photo.jpg" style="visibility:hidden">
</body>
</html>