キャンバスに表示する文字のサイズを指定する

説明

キャンバスに表示する文字のサイズを指定するにはコンテキストのmozTextStyleプロパティに文字列として文字サイズ(48pt)を指定します。

サンプルプログラム

window.onload = function(){
document.getElementById("drawCanvasText").onclick = function(){
initCanvas();
textDraw("Canvas Sample");
}
initCanvas();
}
function initCanvas(){
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "rgba(0,0,255,1)"; // 青色にする
context.fillRect(0,0,640,480); // 塗りつぶす
}
function textDraw(txt){
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.save(); // 現在の状態を保存
context.fillStyle = "rgba(255,255,255,1)";
context.mozTextStyle = document.getElementById("tSize").value+" bold sans serif";
context.translate(70,240);
context.mozDrawText(txt); //
context.restore(); // 以前の状態を復活
}
サンプルを実行
[戻る]