[ html canvas getImageData Object.data.length ] canvas绘图属性 getImageData Object.data.length 属性讲解

 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,canvas {
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 /* url('./images/index.jpg') no-repeat center; backgrond-size: cover */;
25     display: block; margin: 25px auto; border-radius: 2px; cursor: pointer;
26 }
27 </style>
28 <script type='text/javascript'>
29     $( function(){
30         var can = $( '#can' ).get( 0 );
31         var oCan = can.getContext( '2d' );
32         var oImg = new Image();
33         oImg.src = './images/index.jpg';
34         oImg.onload = function(){
35             oCan.drawImage( this , 0 , 0 , this.width , this.height );
36             var img = oCan.getImageData( 50 , 50 , 100 , 100 ); /* 参数 x,y,w,h 在这里面获取的是100*100宽高的颜色值:其中计算方法为100px*100px = 10000px 但是在canvas标签中每一个像素中都有rgba四个颜色值所以当我们在下面打印长度的时候显示的是40000个 */
37             console.log( img.data.length ); /* 选定区域的数据(颜色值)长度 */
38         }
39     } );
40 </script>
41 </head>
42 <body>
43     <canvas id='can' width='300' height='168'>检测到您的浏览器版本过低请升级您的浏览器版本以获取更好的用户体验...</canvas>
44 </body>
45 </html>

 

posted @ 2016-10-11 14:03  窗棂  Views(309)  Comments(0Edit  收藏  举报