繪製圖片有兩種方式:
1、加載圖片
2、使用圖片元素
文件名:img.html。
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>繪製圖片</title>
<style>
#canvas{background-color: #99cc33;}
#jielun{display: none;}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<img id="jielun" src="resource/jielun.jpg">
<script type="text/javascript">
window.onload = function(){
var canvas = document.getElementById('canvas'),
context = canvas.getContext("2d"),
img1 = document.getElementById("jielun"),//使用圖片元素
img = new Image();//創建對象,加載圖片
img.src = "resource/jielun.jpg";
img.onload = function(){
context.drawImage(img,100,100,200,200);
}
context.drawImage(img1,0,0,100,100);
}
</script>
</body>
</html>
效果圖(無與倫比、爲傑沉迷):
參見《HTML5+Javascript動畫基礎》。