Metode strokeText() kanvas HTML
Contoh
Tulis "Halo dunia!" dan "Senyum yang lebar!" (dengan gradien) di kanvas, menggunakan strokeText():
JavaScript:
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);
Dukungan Peramban
Angka-angka dalam tabel menentukan versi browser pertama yang sepenuhnya mendukung metode ini.
Method | |||||
---|---|---|---|---|---|
strokeText() | Yes | 9.0 | Yes | Yes | Yes |
Catatan: Parameter maxWidth tidak didukung di Safari 5.1 dan versi sebelumnya.
Definisi dan Penggunaan
Metode strokeText() menggambar teks (tanpa isian) di kanvas. Warna default teks adalah hitam.
Tip: Gunakan properti font untuk menentukan font dan ukuran font, dan gunakan properti strokeStyle untuk merender teks dalam warna/gradien lain.
Sintaks JavaScript: | konteks .strokeText( 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 |
Referensi Kanvas HTML