HTML5 Canvas的文本设置字体和大小

作者: 来源: 更新时间:2012-04-16 21:54:29 点击:
网页制作文章简介:HTML5 Canvas的文本设置字体和大小.

HTML5 Canvas的文本设置字体和大小,我们可以使用的字体在画布范围内的属性。
下面我们就做一个简单的实例为大家讲解下,基本语法:

context.font=[value];


HTML5 Canvas Text Font & Size 例子

<!DOCTYPE HTML>
<html>
<head>
<title>html5_canvas_text_size</title>
<style>
body {margin: 0px;padding: 0px;}
#myCanvas {border: 1px solid #9C9898; margin:0 auto;margin-top:200px; margin-left:100px;}
</style>
<script>
   window.onload = function(){
                var canvas = document.getElementById("myCanvas");
                var context = canvas.getContext("2d");
               
                var x = 150;
                var y = 100;
                context.font = "40pt Calibri";
                context.fillText("Hello WEBJX!", x, y);
            };
</script>
</head>
<body>
 <canvas id="myCanvas" width="578" height="200">
 </canvas>
</body>
</html>