HTML canvas strokeText() 方法
定義和用法
strokeText()
方法在畫布上繪制文本(沒有填色)。文本的默認顏色是黑色。
提示:請使用 font 屬性來定義字體和字號,并使用 strokeStyle 屬性以另一種顏色/漸變來渲染文本。
實例
使用 strokeText(),在畫布上寫文本 "Hello world!" 和 "codew3c.com":
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="20px Georgia"; ctx.strokeText("Hello World!",10,50); ctx.font="30px Verdana"; // 創建漸變 var gradient=ctx.createLinearGradient(0,0,c.width,0); gradient.addColorStop("0","magenta"); gradient.addColorStop("0.5","blue"); gradient.addColorStop("1.0","red"); // 用漸變填色 ctx.strokeStyle=gradient; ctx.strokeText("codew3c.com",10,90);
語法
context.strokeText(text,x,y,maxWidth);
參數值
參數 | 描述 |
---|---|
text | 規定在畫布上輸出的文本。 |
x | 開始繪制文本的 x 坐標位置(相對于畫布)。 |
y | 開始繪制文本的 y 坐標位置(相對于畫布)。 |
maxWidth | 可選。允許的最大文本寬度,以像素計。 |
瀏覽器支持
表中的數字注明了首個完全支持該屬性的瀏覽器版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
注釋:Internet Explorer 8 以及更早的版本不支持 <canvas> 元素。