[ html drawImage 绘制图片 ] canvas绘制图片 drawImage 属性实例演示之二
1 <!DOCTYPE html>
2 <html lang='zh-cn'>
3 <head>
4 <title>Insert you title</title>
5 <meta name='description' content='this is my page'>
6 <meta name='keywords' content='keyword1,keyword2,keyword3'>
7 <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
8 <link rel='stylesheet' type='text/css' href='./css/index.css' />
9 <script type='text/javascript' src='./js/jquery-1.12.1.min.js'></script>
10 <style type='text/css'>
11 html,body {
12 margin: 0; padding: 0;
13 }
14
15 html {
16 height: 100%;
17 }
18
19 body {
20 background: #000;
21 }
22
23 #can {
24 background: #FFF; display: block; margin: 25px auto; border-radius: 2px;
25 }
26
27 img {
28 -moz-user-select: none;
29 }
30 </style>
31 <script type='text/javascript'>
32 var oCan = null;
33 var oImg = null;
34 $( function(){
35 oCan = $( '#can' ).get( 0 ).getContext( '2d' );
36 /* 在这里需要注意的就是我们的drawImg()不能将其放置在jquery中,因为jquery的$(document).ready(function(){})有等待页面加载完成之后在加载JS文件的功效,而我们又告诉HTML文档要预加载图片所以他们无法同时使用: 报错内容是drawImg()为define*/
37 oImg = $( 'img' ).get( 0 );
38 } );
39 function drawImg(){
40 oCan.drawImage( oImg , 0 , 0 , oImg.width , oImg.height );
41 }
42 </script>
43 </head>
44 <body onload='drawImg()'>
45 <canvas id='can' width='500' height='450'>您的浏览器版本过低请升级您的浏览器以获取更好的用户体验...</canvas>
46 <img src='./images/demoImg.jpg' width='25%' height='15%' />
47 </body>
48 </html>