This should be a simple Air App. I am using Dreamweaver CS5.5 and creating an Air App. I cannot get the Canvas to load an image on function. I can get the image to load onLoad. The objective is to get an image to draw on canvas then resize and toDataURL to an img tag. What am I doing wrong? Here is the code:
<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>
I think the that line is giving you problems:
<button onClick="javascript:getImage('https://www.google.com/images/srpr/logo3w.png');">Get Image</button>
The code defined in a link using the javascript: URL scheme is ignored in the application sandbox. No unsafe JavaScript error is generated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.