HTML canvas strokeText()メソッド
例
strokeText() を使用して、キャンバスにテキスト "Hello world!" と "Big smile!"(グラデーション付き)を描画します:
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";
// Create gradient
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Fill with gradient
ctx.strokeStyle=gradient;
ctx.strokeText("Big smile!",10,90);
var ctx=c.getContext("2d");
ctx.font="20px Georgia";
ctx.strokeText("Hello World!",10,50);
ctx.font="30px Verdana";
// Create gradient
var gradient=ctx.createLinearGradient(0,0,c.width,0);
gradient.addColorStop("0","magenta");
gradient.addColorStop("0.5","blue");
gradient.addColorStop("1.0","red");
// Fill with gradient
ctx.strokeStyle=gradient;
ctx.strokeText("Big smile!",10,90);
試してみる »
ブラウザサポート
![]()
![]()
![]()
![]()
![]()
Internet Explorer 9、Firefox、Opera、Chrome、Safari は strokeText() メソッドをサポートしています。
注意:Internet Explorer 8 以前のバージョンは <canvas> 要素をサポートしていません。
注意:Safari は maxWidth パラメータをサポートしていません。
定義と使い方
strokeText() メソッドはキャンバス上にテキストを描画します(塗りつぶしなし)。テキストのデフォルト色は黒です。
ヒント:使用してくださいfont属性を使用してフォントとフォントサイズを定義し、strokeStyle属性を使用して別の色/グラデーションでテキストをレンダリングします。
| JavaScript 構文: | context.strokeText(text,x,y,maxWidth); |
|---|
パラメータ値
| パラメータ | 説明 |
|---|---|
| text | キャンバスに出力するテキストを指定します。 |
| x | テキストの描画を開始する x 座標位置(キャンバスに対する相対位置)。 |
| y | テキストの描画を開始する y 座標位置(キャンバスに対する相対位置)。 |
| maxWidth | オプション。許容される最大テキスト幅(ピクセル単位)。 |
その他の拡張
Canvas オブジェクト