HTML5のキャンバスにテキストのふちどり描画します。テキストのふちどり描画には、strokeText関数を使います。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<title>テキストの描画(ふちどり)</title>
<script type="text/javascript">
/*
ページロード完了
*/
function OnLoad() {
// キャンバス取得
var canvas = document.getElementById( "canvas" );
// キャンバスの幅と高さを取得
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
// コンテキスト取得
var ctx = canvas.getContext('2d');
// キャンバスのクリア
ctx.fillStyle = '#FFFFFF';
ctx.fillRect( 0, 0, canvasWidth, canvasHeight );
// キャンバスの中心
var centerX = canvasWidth / 2;
var centerY = canvasHeight / 2;
/*
テキストの描画
*/
{
ctx.font= '30pt Hiragino Kaku Gothic ProN, meiryo, sans-serif';
ctx.strokeStyle = '#000000';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.strokeText( "テキストの描画", centerX, canvasHeight / 4 );
}
/*
テキストの描画(italic)
*/
{
ctx.font= 'italic 30pt Hiragino Kaku Gothic ProN, meiryo, sans-serif';
ctx.strokeStyle = '#FF0000';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.strokeText( "italic", centerX, ( canvasHeight * 2 ) / 4 );
}
/*
テキストの描画(太字)
*/
{
ctx.font= 'bold 30pt Hiragino Kaku Gothic ProN, meiryo, sans-serif';
ctx.strokeStyle = '#0000FF';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.strokeText( "太字で描画", centerX, ( canvasHeight * 3 ) / 4 );
}
}
</script>
</head>
<body bgcolor="#c0c0ff" onload="OnLoad()">
<h1>テキストの描画(ふちどり)</h1>
<canvas width="300" height="240" id="canvas"></canvas>
<br />
<a href="../html5_list.html">戻る</a>
</body>
</html>