DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> HTML基礎知識 >> HTML和Xhtml >> HTML5 Canvas的文本垂直對齊
HTML5 Canvas的文本垂直對齊
編輯:HTML和Xhtml     

HTML畫布垂直對齊的文本,我們可以使用的textBaseline在畫布范圍內的屬性值。textBaseline可以設置以下值之一 :top, hanging, middle, alphabetic, ideographic, and bottom。除非另有規定,textBaseline屬性默認為字母。

  1. context.textBaseline=[value];

<!DOCTYPE HTML>
<html>
<head>
<title>html5_canvas_text_baseline</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 = canvas.width / 2;
    var y = canvas.height / 2;
    context.font = "30pt Calibri";
    // textAlign aligns text horizontally relative to placement
    context.textAlign = "center";
    // textBaseline aligns text vertically relative to font style
    context.textBaseline = "middle";
    context.fillStyle = "blue";
    context.fillText("Hello World!", x, y);
};
</script>
</head>
<body>
 <canvas id="myCanvas" width="578" height="200">
 </canvas>
</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved