½Ó×ÅÉÏÎÄÏßÌõÑùʽ[js¸ßÊÖ֮·] html5 canvasϵÁÐ½Ì³Ì - ÏßÌõÑùʽ(lineWidth,lineCap,lineJoin,setLineDash)¼ÌÐø.
canvasÌṩÁ½ÖÖÊä³öÎı¾µÄ·½Ê½£º
strokeText£ºÃè±ßÎı¾
fillText£ºÌî³äÎı¾
fillStyleÅäºÏfillTextʹÓã¬strokeStyleÅäºÏstrokeTextʹÓÃ
strokeTextÓ÷¨£º
cxt.strokeText( text£¬ x, y, [maxwidth] )
text£ºÐèÒªÊä³öµÄÎı¾ÄÚÈÝ
x£º×î×ó±ßµÄÎı¾Êä³öµÄºá×ø±êλÖÃ
y£º×î×ó±ßµÄÎı¾µÄ ×óϽǵÄ×Ý×ø±ê
maxWidth£ºÕâ¸öÊÇ¿ÉÑ¡²ÎÊý£¬Òâ˼¾ÍÊÇÎı¾×î¶àÄÜÕ¼ÓÃmaxWidthÕâô¿í£¬Èç¹ûÎı¾µÄʵ¼Ê¿í¶È±ÈmaxWidth¿í£¬¾Í»áÓÐÒ»ÖÖѹËõ(±»¼·±â)µÄЧ¹û¡£
1 <meta charset='utf-8' /> 2 <style> 3 body { 4 background: #000; 5 } 6 #canvas{ 7 background:white; 8 } 9 </style> 10 <script> 11 window.onload = function(){ 12 var oCanvas = document.querySelector( "#canvas" ), 13 oGc = oCanvas.getContext( '2d' ), 14 15 text = '¸ú×Åghostwuѧϰcanvas'; 16 oGc.font = 'bold 30px ΢ÈíÑźÚ'; 17 oGc.strokeStyle = '#09f'; 18 oGc.strokeText( text, 100, 100 ); 19 oGc.strokeText( text, 100, 200, 200 ); 20 } 21 </script> 22 </head> 23 <body> 24 <canvas></canvas> 25 </body>
fillText£ºÌî³äÎı¾£¬²ÎÊý¸ústrokeTextÒ»Ñù
text = '¸ú×Åghostwuѧϰcanvas';
oGc.font = 'bold 30px ΢ÈíÑźÚ';
oGc.fillStyle = '#09f';
oGc.fillText( text, 100, 100 );
oGc.fillText( text, 100, 200, 200 );
measureText£º»ñÈ¡Îı¾µÄ¿í¶È(³¤¶È)£¬Ëü·µ»ØµÄÊÇÒ»¸ö¶ÔÏ󣬶ÔÏóÓÐÒ»¸öÊôÐÔwidth£¬¾ÍÊÇÎı¾µÄ³¤¶ÈÁË.
cxt.measureText( text ).width
Êä³öÒ»¶Îˮƽ¾ÓÖеÄÎı¾
1 <meta charset='utf-8' /> 2 <style> 3 body { 4 background: #000; 5 } 6 #canvas{ 7 background:white; 8 } 9 </style> 10 <script> 11 window.onload = function(){ 12 var oCanvas = document.querySelector( "#canvas" ), 13 oGc = oCanvas.getContext( '2d' ), 14 width = oCanvas.width, 15 text = '¸ú×Åghostwuѧϰcanvas'; 16 17 oGc.font = 'bold 30px ΢ÈíÑźÚ'; 18 oGc.fillStyle = '#09f'; 19 oGc.fillText( text, ( width - oGc.measureText( text ).width ) / 2, 100 ); 20 } 21 </script> 22 </head> 23 <body> 24 <canvas></canvas> 25 </body>
fontÊôÐÔ¸úcssÊÇÒ»ÑùµÄÓ÷¨
cxt.font = "font-style font-weight font-size/line-height font-family"
textAlign£ºÎı¾Ë®Æ½¶ÔÆ뷽ʽ
cxt.textAlign = 'start/end/left/right/center';
start¸úleft²î²»¶à£¬end¸úright²î²»¶à.
1 <meta charset='utf-8' /> 2 <style> 3 body { 4 background: #000; 5 } 6 #canvas{ 7 background:white; 8 } 9 </style> 10 <script> 11 window.onload = function(){ 12 var oCanvas = document.querySelector( "#canvas" ), 13 oGc = oCanvas.getContext( '2d' ), 14 width = oCanvas.width, 15 height = oCanvas.height, 16 text = '¸ú×Åghostwuѧϰcanvas'; 17 18 oGc.font = 'bold 16px ΢ÈíÑźÚ'; 19 oGc.fillStyle = '#09f'; xPos = ( width ) / 2; 22 oGc.moveTo( xPos, 0 ); 23 oGc.lineTo( xPos, height ); 24 oGc.stroke(); 25 26 oGc.textAlign = 'start'; 27 oGc.fillText( text, 300, 30 ); 28 oGc.textAlign = 'left'; 29 oGc.fillText( text, 300, 60 ); 30 oGc.textAlign = 'right'; 31 oGc.fillText( text, 300, 90 ); 32 oGc.textAlign = 'end'; 33 oGc.fillText( text, 300, 120 ); 34 oGc.textAlign = 'center'; 35 oGc.fillText( text, 300, 150 ); 36 } 37 </script> 38 </head> 39 <body> 40 <canvas></canvas> 41 </body>
textBaseline£ºÉèÖÃÎı¾´¹Ö±·½ÏòµÄ¶ÔÆ뷽ʽ
cxt.textBaseline = 'ÊôÐÔÖµ'
³£¼ûµÄÊôÐÔÖµ: alphabetic, top, middle, bottomµÈ
¸úÉÏÃæµÄtextAlignµÄÓ÷¨²î²»¶à£¬Õâ¸ö²»ÊǺܳ£ÓÃ
¡¡