HTML canvas strokeText() Metodo
Esempio
Scrivi "Ciao mondo!" e "Grande sorriso!" (con gradiente) sulla tela, usando 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);
Supporto browser
I numeri nella tabella specificano la prima versione del browser che supporta completamente il metodo.
Method | |||||
---|---|---|---|---|---|
strokeText() | Yes | 9.0 | Yes | Yes | Yes |
Nota: il parametro maxWidth non è supportato in Safari 5.1 e versioni precedenti.
Definizione e utilizzo
Il metodo strokeText() disegna il testo (senza riempimento) sulla tela. Il colore predefinito del testo è il nero.
Suggerimento: usa la proprietà del carattere per specificare il carattere e la dimensione del carattere e usa la proprietà strokeStyle per eseguire il rendering del testo in un altro colore/sfumatura.
sintassi JavaScript: | contesto .strokeText( testo,x,y,maxWidth ); |
---|
Valori dei parametri
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 |
❮ Riferimento tela HTML