0

我有这个在画布中绘制图像的简单代码,但它只适用于 Firefox

这是代码:

<!DOCTYPE html>
<html>
<head>
<title>CANVAS</title>
</head>
<body>
<canvas id="smallImage" width="600" height="600"></canvas>
<script>
var imgid = document.getElementById('smallImage');
var ctx = imgid.getContext('2d');
var img = new Image();
var immmagine = "flowers.jpg";
img.src = immmagine;
ctx.drawImage(img,0,0);
</script>
</body>
</html>
4

2 回答 2

0

您需要在图像上监听 onload 事件:

var imgid = document.getElementById('smallImage');
var ctx = imgid.getContext('2d'); 
var img = new Image();
var immmagine = "flowers.jpg";
img.src = immmagine;
img.onload = function ()
{
 ctx.drawImage(img,0,0);
} 
于 2012-10-10T11:36:58.303 回答
0

您需要使用 onload 事件。

img.onload = function() {
   ctx.drawImage(img,0,0);
}
于 2012-10-10T11:00:15.467 回答