HTML canvas strokeText() Method
ตัวอย่าง
เขียนว่า "สวัสดีชาวโลก!" และ "ยิ้มกว้าง!" (พร้อมการไล่ระดับสี) บนผืนผ้าใบโดยใช้ strokeText():
จาวาสคริปต์:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "20px Georgia";
ctx.strokeText("Hello World!", 10, 50);
ctx.font = "30px Verdana";
// Create gradient
var gradient = ctx.createLinearGradient(0, 0, c.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// Fill with gradient
ctx.strokeStyle = gradient;
ctx.strokeText("Big smile!", 10, 90);
รองรับเบราว์เซอร์
ตัวเลขในตารางระบุเบราว์เซอร์รุ่นแรกที่รองรับวิธีการนี้อย่างเต็มที่
Method | |||||
---|---|---|---|---|---|
strokeText() | Yes | 9.0 | Yes | Yes | Yes |
หมายเหตุ:ไม่รองรับพารามิเตอร์ maxWidth ใน Safari 5.1 และเวอร์ชันก่อนหน้า
ความหมายและการใช้งาน
เมธอด strokeText() จะดึงข้อความ (ไม่มีการเติม) ลงบนผืนผ้าใบ สีเริ่มต้นของข้อความคือสีดำ
เคล็ดลับ:ใช้ คุณสมบัติ ฟอนต์เพื่อระบุฟอนต์และขนาดฟอนต์ และใช้ คุณสมบัติ strokeStyle เพื่อแสดงข้อความในสี/การไล่ระดับสีอื่น
ไวยากรณ์ JavaScript: | บริบท .strokeText ( text,x,y,maxWidth ); |
---|
ค่าพารามิเตอร์
Parameter | Description | Play it |
---|---|---|
text | Specifies the text that will be written on the canvas | |
x | The x coordinate where to start painting the text (relative to the canvas) | |
y | The y coordinate where to start painting the text (relative to the canvas) | |
maxWidth | Optional. The maximum allowed width of the text, in pixels |
❮ การอ้างอิงผ้าใบ HTML