弄了很久才用<canvas>标签显示出图像来,可是启动浏览器之后必须刷新一下才能看到图像,不然就是空白的什么都没有。
哪里的问题。。。。。。
[html文件]
<!DOCTYPE html>
<html>
<head><title>javascript</title>
<script type="text/javascript" src="xxxx.js"></script>
<meta http-quive="Pragma" content="no-cache">
<meta http-quive="Cache-Control" content="no-cache">
</head>
<body onload="draw();">
<canvas id="canvas" width="800" height="800"></canvas>
</body>
</html>
[javascript文件]
function draw(){
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
var img=new Image();
img.src="0001.png";
context.drawImage(img,50,50);
}
------解决方案--------------------
- HTML code
<!DOCTYPE html> <html> <head><title>javascript</title> <script> function draw(){ var canvas=document.getElementById("canvas"); var context=canvas.getContext("2d"); var img = new Image(); img.src="http://avatar.profile.csdn.net/2/4/4/2_dadedu.jpg"; img.onload = function(){ context.drawImage(img,50,50); } } </script> </head> <body onload="draw();"> <canvas id="canvas" width="800" height="800"></canvas> </body> </html>