Properti textBaseline

Properti textBaseline mengatur perataan vertikal teks yang digambar menggunakan metode fillText atau metode strokeText. Menerima salah satu dari nilai yang mungkin: top, hanging, middle, alphabetic (default), ideographic, bottom (untuk memahami nilai-nilai lihat www.w3schools.com/tags/canvas_textbaseline.asp).

Sintaks

javascriptkonteks.textBaseline = nilai;

Contoh

Mari atur perataan vertikal teks ke garis atas:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptconst canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

ctx.strokeStyle = 'red';
ctx.moveTo(0, 100);
ctx.lineTo(400, 100);
ctx.stroke();
ctx.font="14px Arial";
ctx.textBaseline = 'top'; 
ctx.fillText('text', 80, 100);

Результат:

Contoh

Dan sekarang mari atur perataan vertikal teks ke garis bawah:

html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
javascriptconst canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

ctx.strokeStyle = 'red';
ctx.moveTo(0, 100);
ctx.lineTo(400, 100);
ctx.stroke();
ctx.font="14px Arial";
ctx.textBaseline="bottom"; 
ctx.fillText('text', 80, 100); 

Результат:

Lihat juga

  • properti textAlign,
    yang meratakan teks secara horizontal
  • properti font,
    yang mengatur ukuran dan jenis font