[ html canvas 绘制文本 ] canvas绘图实现绘制文本 strokeText fillText方法及textAlign textBaseline font 属性实例演示

 1 <!DOCTYPE html>
 2 <html >
 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     background: #999;
17 }
18 
19 #can {
20     background: #FFF; display: block; margin: 75px auto; border-radius: 2px;
21 }
22 </style>
23 <script type='text/javascript'>
24     $( function(){
25         var oCan = $( '#can' ).get( 0 ).getContext( '2d' );
26         oCan.beginPath();
27 
28         /* 
29         
30             oCan.strokeText(text,x,y) 和 oCan.fillText(text,x,y)之间的区别:
31                 实心和空心字体之间的区别;
32             
33             oCan.font : 设置字体样式和大小
34             
35             oCan.textAlign : 设置文本在基线上的水平对齐方式
36             
37             oCan.textBaseLine : 设置文本在垂直方向上的对齐方式
38                 
39                 
40         
41          */
42 
43         oCan.font = '900 40px Microsoft yahei'; /* 这个属性和 旋转 缩放 平移一样是需要设置在前面的否则无效 并且字体大小和字体样式缺一不可*/
44         oCan.lineWidth = 1;
45         oCan.strokeStyle = '#F00';
46         oCan.textAlign = 'center'; /* 在基线的水平位置上实现属性值  left right start center end */
47         oCan.textBaseline = 'bottom'; /* 在基线的垂直位置上实现属性值  top bottom middle hanging...*/
48         oCan.strokeText( '窗棂老师好帅xem' , 250 , 250 ); /* 设置空心字体 */
49         oCan.arc( 250 , 250 , 2 , 0 , 2 * Math.PI , false );
50         oCan.fillText( '窗棂老师好帅' , 30 , 300 ); /* 设置实心填充字体 */
51         oCan.stroke();
52         oCan.closePath();
53     } );
54 </script>
55 </head>
56 <body>
57     <canvas  width='500' height='500'>您的浏览器版本过低,请升级您的浏览器版本以获取更好的用户体验...</canvas>
58 </body>
59 </html>