HTMLキャンバスstrokeText()メソッド
例
「Helloworld!」と書いてください。と「笑顔!」(グラデーション付き)キャンバス上で、strokeText()を使用:
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);
ブラウザのサポート
表の数字は、このメソッドを完全にサポートする最初のブラウザバージョンを示しています。
Method | |||||
---|---|---|---|---|---|
strokeText() | Yes | 9.0 | Yes | Yes | Yes |
注: maxWidthパラメーターは、Safari5.1以前のバージョンではサポートされていません。
定義と使用法
strokeText()メソッドは、キャンバスにテキスト(塗りつぶしなし)を描画します。テキストのデフォルトの色は黒です。
ヒント:fontプロパティを使用してフォントとフォントサイズを指定し、strokeStyle プロパティを使用してテキストを別の色/グラデーションでレンダリングします。
JavaScript構文: | context .strokeText(text、x、y、maxWidth); |
---|
パラメータ値
Parameter | Description | Play it |
---|---|---|
text | Specifies the text that will be written on the canvas | |
x | The x coordinate where to start painting the text (relative to the canvas) | |
y | The y coordinate where to start painting the text (relative to the canvas) | |
maxWidth | Optional. The maximum allowed width of the text, in pixels |
❮HTMLキャンバスリファレンス