HTML canvas measureText() method
Definisiyon at paggamit
measureText()
Ang paraan ay ibibigay ang isang bagay na naglalaman ng lapad ng font na naka-ayos sa pixels.
Mga payo:Kung gusto mong malaman ang lapad ng teksto bago ito ilagay sa laman, gamitin ang paraan na ito.
Mga halimbawa
Bago i-output ang teksto sa laman, suriin ang lapad ng font:
JavaScript:
var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="30px Arial"; var txt="Hello World" ctx.fillText("width:" + ctx.measureText(txt).width,10,50) ctx.fillText(txt,10,100);
การใช้
context.measureText(text).width;
ค่าประกาศ
ประกาศ | คำอธิบาย |
---|---|
text | ข้อความที่ต้องการวัด |
การรองรับโปรแกรมนักเขียน
ตัวเลขในตารางนี้บอกเกี่ยวกับเวอร์ชั่นแรกที่รองรับอัตริยภาพนี้
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
หมายเหตุ:Internet Explorer 8 และเวอร์ชั่นเก่ากว่าไม่สนับสนุนสิ่งที่ชื่อว่า <canvas> อิเล็กทรอนิกส์