標籤:
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 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 id=‘can‘ width=‘500‘ height=‘500‘>您的瀏覽器版本過低,請升級您的瀏覽器版本以擷取更好的使用者體驗...</canvas>58 </body>59 </html>
[ html canvas 繪製文本 ] canvas繪圖實現繪製文本 strokeText fillText方法及textAlign textBaseline font 屬性執行個體示範