[ html canvas font textAlign textBaseline measureText().width ] canvas绘图属性 font textAlign textBaseline measureText().width 属性演示

 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; display: block; margin: 25px auto; border-radius: 2px;line-height:0;
25 }
26 </style>
27 <script type='text/javascript'>
28     $( function(){
29         var can = $( '#can' ).get( 0 );
30         var oCan = can.getContext( '2d' );
31         var w = can.width;
32         var h = can.height;
33         oCan.font = '50px 楷体';    /* 字号 字体缺一不可 */
34         oCan.strokeText( '窗棂' , 200 , 175 );
35         oCan.font = '900 50px Courier New';    /* 可以补充加粗方式但字号 字体缺一不可 并且以不同的方式来进行填充是可以更改字体的 */
36         oCan.strokeStyle = '#F00';
37         oCan.textAlign ='center'    /* 设置水平居中方式 */
38         oCan.textBaseline = 'middle';    /* 设置垂直居中方式 */
39         var text = 'chuangling';
40         oCan.fillText( text , w/2 , h/2 );    /* 创建文本的自适应居中方式  */
41         alert(oCan.measureText(text).width);    /* 可以获取文本的实际宽度值,只有宽度 measure : 测量 */
42     } );
43 </script>
44 </head>
45 <body>
46     <canvas id='can' width='500' height='450'>检测到您的浏览器版本过低请升级您的浏览器版本,以获取更好的使用体验...</canvas>
47 </body>
48 </html>
原文地址:https://www.cnblogs.com/mysearchblog/p/5933733.html