わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

テキストの描画

HTML5のキャンバスにテキストを描画します。テキストの描画には、fillText関数を使います。

<!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.fillStyle    = '#000000';
				ctx.textAlign    = 'center';
				ctx.textBaseline = 'middle';
				ctx.fillText( "テキストの描画", centerX, canvasHeight / 4 );
			}


			/*
				テキストの描画(italic)
			*/
			{
				ctx.font= 'italic 30pt Hiragino Kaku Gothic ProN, meiryo, sans-serif';
				ctx.fillStyle    = '#FF0000';
				ctx.textAlign    = 'center';
				ctx.textBaseline = 'middle';
				ctx.fillText( "italic", centerX, ( canvasHeight * 2 ) / 4 );
			}


			/*
				テキストの描画(太字)
			*/
			{
				ctx.font= 'bold 30pt Hiragino Kaku Gothic ProN, meiryo, sans-serif';
				ctx.fillStyle    = '#0000FF';
				ctx.textAlign    = 'center';
				ctx.textBaseline = 'middle';
				ctx.fillText( "太字で描画", 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>






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。