HTML canvas font 属性

定义和用法

font 属性设置或返回画布上文本内容的当前字体属性。

font 属性使用的语法与 CSS font 屬件相同。

实例

在画布上写一段 40 像素的文本,使用的字体是 "Arial":

Your browser does not support the canvas tag.

JavaScript:

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="40px Arial";
ctx.fillText("Hello World",10,50);

亲自试一试

语法

context.font="italic small-caps bold 12px arial";

属性值

描述
font-style

规定字体样式。

  • normal
  • italic
  • oblique
font-variant

规定字体变体。

  • normal
  • small-caps
font-weight

规定字体的粗细。

  • 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 ใช้ตัวอักษรสำหรับแถวระดับหลังของหน้าต่าง

รายละเอียดเทคนิค

ค่าเริ่มต้น: 10px sans-serif

การสนับสนุนโปรแกรมนักเทรน

ตัวเลขในตารางนี้บอกเล่าว่าเวอร์ชั่นแรกที่สนับสนุนคุณสมบัตินี้

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
4.0 9.0 3.6 4.0 10.1

หมายเหตุ:Internet Explorer 8 และเวอร์ชั่นเก่ากว่าไม่สนับสนุนสิ่งที่ชื่อว่า <canvas> อิเล็กทรอนิก