คุณสมบัติ font
คุณสมบัติ font กำหนดการตั้งค่าฟอนต์
เมื่อวาดบน canvas โดยใช้วิธีการ
fillText
หรือวิธีการ strokeText
เมธอดรับพารามิเตอร์เช่นเดียวกับ
คุณสมบัติ CSS font
ไวยากรณ์
context.font = การตั้งค่าฟอนต์;
ตัวอย่าง
มาวาดข้อความบน canvas โดยใช้
fillText
และกำหนดขนาดเป็น 30px และชนิด
Arial โดยใช้ font:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
let canvas = document.querySelector('#canvas');
let ctx = canvas.getContext('2d');
ctx.font = '30px Arial';
ctx.fillText('Hello world', 30, 50);
:
ดูเพิ่มเติม
-
คุณสมบัติ
textAlign,
ซึ่งจัดแนวข้อความในแนวนอน -
คุณสมบัติ
textBaseline,
ซึ่งจัดแนวข้อความในแนวตั้ง