Método HTML canvas fillText()
Ejemplo
Escribe "¡Hola mundo!" y "¡Gran sonrisa!" (con degradado) en el lienzo, usando 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);
Compatibilidad con navegador
Los números en la tabla especifican la primera versión del navegador que soporta totalmente este método.
Method | |||||
---|---|---|---|---|---|
fillText() | Yes | 9.0 | Yes | Yes | Yes |
Nota: El parámetro maxWidth no es compatible con Safari 5.1 y versiones anteriores.
Definición y uso
El método fillText() dibuja texto relleno en el lienzo. El color predeterminado del texto es negro.
Sugerencia: use la propiedad de fuente para especificar la fuente y el tamaño de la fuente, y use la propiedad fillStyle para representar el texto en otro color/degradado.
Sintaxis de JavaScript: | contexto .fillText( texto,x,y,maxWidth ); |
---|
Valores paramétricos
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 |
❮ Referencia de lienzo HTML