Metode fillText() kanvas HTML
Contoh
Tulis "Halo dunia!" dan "Senyum yang lebar!" (dengan gradien) di kanvas, menggunakan fillText():
JavaScript:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "20px Georgia";
ctx.fillText("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.fillStyle = gradient;
ctx.fillText("Big smile!", 10, 90);
Dukungan Peramban
Angka-angka dalam tabel menentukan versi browser pertama yang sepenuhnya mendukung metode ini.
Method | |||||
---|---|---|---|---|---|
fillText() | 4.0 | 9.0 | 3.6 | 4.0 | 10.1 |
Definisi dan Penggunaan
Metode fillText() menggambar teks yang diisi di kanvas. Warna default teks adalah hitam.
Tips: Gunakan properti font untuk menentukan font dan ukuran font, dan gunakan properti fillStyle untuk merender teks dalam warna/gradien lain.
Sintaks JavaScript: | konteks .fillText( teks, x, y, maxWidth ); |
---|
Nilai Parameter
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 |
Objek Kanvas