这应该是一个简单的Air App。我正在使用 Dreamweaver CS5.5 并创建一个 Air 应用程序。我无法让 Canvas 在功能上加载图像。我可以让图像加载 onLoad。目标是让图像在画布上绘制,然后调整大小并将 toDataURL 调整为 img 标签。我究竟做错了什么?这是代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
function getImage(url){
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var img = new Image();
img.src=url;
img.onload = function(){
context.drawImage(img, 0,0,width,height);
var dataurl = canvas.toDataURL();
document.getElementById('image').src = dataurl;
};
var MAX_WIDTH = 140;
var MAX_HEIGHT = 200;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
<img id="image" src="" />
<button onClick="javascript:getImage('https://www.google.com/images/srpr/logo3w.png');">Get Image</button>
</body>
</html>