本文實(shí)例分析了HTML5中drawImage使用時(shí)遇到的問(wèn)題及解決方法。分享給大家供大家參考。具體分析如下:
使用Image遇到的問(wèn)題:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function() {
var jsCanv = document.getElementById("canv");
var oCanv = jsCanv.getContext("2d");
var img = new Image();
img.src = "img.png";
oCanv.drawImage(img, 220, 30);
})
</script>
</head></p>
<p> <body>
<canvas id="canv" width="500" height="500">
瀏覽器不支持
</canvas>
</body>
</html>
其實(shí)這種寫(xiě)法是有錯(cuò)誤的,實(shí)際上只要一刷新圖片就不顯示出來(lái)。要想保證刷新正常顯示需要在Image onload的時(shí)候重繪一次才行。測(cè)試在chrome 19下會(huì)出現(xiàn)的問(wèn)題。
解決方案
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function() {
var jsCanv = document.getElementById("canv");
var oCanv = jsCanv.getContext("2d");
var img = new Image();
img.src = "img.png";
img.onload = function() {
oCanv.drawImage(img, 220, 30);
}
})
</script>
</head>
<body>
<canvas id="canv" width="500" height="500">
瀏覽器不支持
</canvas>
</body>
</html>
希望本文所述對(duì)大家的html5程序設(shè)計(jì)有所幫助。