1

我有一个小错误,这似乎表明在图像完全加载之前调用 onload 函数,当您将图像数据作为其源传递时。

这是 HTML

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
</head>
<body>
    <canvas id='finalImage'></canvas>
</body>
</html>​

这是javascript:

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');

var image = new Image();
image.onload = function() {
    // I've also put a console.log here at one point, and it does get called.
    context.drawImage(image, 0, 0, canvas.width, canvas.height);
};
image.src = "data:image/jpeg;base64,/9j/4QNMR..."; // cut for sanity.

var outputCanvas = $("#finalImage")[0];
var outputContext = outputCanvas.getContext('2d');

// If I uncomment this line, the image is drawn.
//context.drawImage(image, 0, 0, canvas.width, canvas.height);

// If I change canvas to image, its also drawn, which means it is being loaded.
// But it seems to indicate to me that onload gets called too early.
outputContext.drawImage(canvas, 0, 0);​

这是 JSFiddle 的链接。.

如果您发现它有什么问题,请告诉我,现在已经搞砸了。

谢谢!

4

1 回答 1

1
于 2012-12-17T04:54:28.273 回答