font 是 Canvas 2D API 描述繪制文字時,當前字體樣式的屬性。 使用和 CSS font 規(guī)范相同的字符串值。。
在畫布上寫一段 30 像素的文本,使用的字體是 "Arial":
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML canvas font屬性使用-菜鳥教程(cainiaoplus.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
您的瀏覽器不支持 HTML5 canvas 標簽。
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("菜鳥教程 (cainiaoplus.com)",10,50);
</script>
</body>
</html>測試看看 ?/?IEFirefoxOperaChromeSafari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 font 屬性。
注意:Internet Explorer 8 及更早版本不支持 <canvas> 元素。
font屬性設置或返回畫布上文本內容的當前字體屬性。
font 屬性使用的語法與 CSS font 屬性 相同。
| 默認值: | 10px sans-serif |
|---|---|
| JavaScript 語法: | context.font="italic small-caps bold 12px arial"; |
| 值 | 描述 |
|---|---|
| font-style | 指定字體樣式??赡艿闹担?ul class=" list-paddingleft-2"> normal italic oblique |
| font-variant | 指定字體變體??赡艿闹担?ul class=" list-paddingleft-2"> normal small-caps |
| font-weight | 指定字體的粗細??赡艿闹担?ul class=" list-paddingleft-2"> normal bold bolder lighter 100 200 300 400 500 600 700 800 900 |
| font-size/line-height | 指定字號和行高,以像素為單位。 |
| font-family | 指定字體系列。 |
| caption | 使用標題控件的字體(比如按鈕、下拉列表等)。 |
| icon | 使用用于標記圖標的字體。 |
| menu | 使用用于菜單中的字體(下拉列表和菜單列表)。 |
| message-box | 使用用于對話框中的字體。 |
| small-caption | 使用用于標記小型控件的字體。 |
| status-bar | 使用用于窗口狀態(tài)欄中的字體。 |