Question

I can draw text using the below code,

myCanvas.fillStyle = "Red";
myCanvas.font = "30pt Arial";
myCanvas.fillText("Some Text", 10, 30);

But I want to add a border around "Some Text", any ideas on that?

Was it helpful?

Solution

Use strokeText() and the strokeStyle. eg:

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>

OTHER TIPS

We can use strokeStyle method to draw border around the text or outline, and We can use lineWidth method to define the width of the stroke line.

 <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>

What about

myCanvas.style.border = "red 1px solid";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top