¿Cómo agregar un borde en el texto HTML5 Canvas '?
-
07-07-2019 - |
Pregunta
Puedo dibujar texto usando el siguiente código,
myCanvas.fillStyle = "Red";
myCanvas.font = "30pt Arial";
myCanvas.fillText("Some Text", 10, 30);
Pero quiero agregar un borde alrededor de "Algún texto", ¿alguna idea sobre eso?
Solución
Use strokeText ()
y el strokeStyle.
por ejemplo:
canvas = document.getElementById("myc");
context = canvas.getContext('2d');
context.fillStyle = 'red';
context.strokeStyle = 'black';
context.font = '20pt Verdana';
context.fillText('Some text', 50, 50);
context.strokeText('Some text', 50, 50);
context.fill();
context.stroke();
<canvas id="myc"></canvas>
Otros consejos
Podemos usar el método strokeStyle para dibujar el borde alrededor del texto o el contorno, y podemos usar el método lineWidth para definir el ancho de la línea de trazo.
<script>
var canvas = document.getElementById('Canvas01');
var ctx = canvas.getContext('2d');
ctx.strokeStyle= "red"; //set the color of the stroke line
ctx.lineWidth = 3; //define the width of the stroke line
ctx.font = "italic bold 35pt Tahoma"; //set the font name and font size
ctx.strokeText("StackOverFlow",30,80); //draw the text
</script>
</body>
¿Qué pasa con
?myCanvas.style.border = "red 1px solid";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow