3

I'm using html5 canvas to draw some texts, but I got some ugly results, here is my sample code to draw the text:

  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');
  var x = 80;
  var y = 110;

  context.fillStyle = "white"
  context.font = '13px Arial';
  context.lineWidth = 3;
  // stroke color
  context.strokeStyle = 'black';
  context.strokeText('ABCDEFGHIJKLMNOPQRSTUVWXYZ!', x, y);
  context.fillText('ABCDEFGHIJKLMNOPQRSTUVWXYZ!', x, y); 

And I got this result

enter image description here

then I change the text to "abs" and got this

enter image description here

You can see the "M" and "s" looks ugly, does anyone have an idea on how to solve this?

Edgar
  • 273
  • 1
  • 3
  • 12
  • Its due to font size , try to increase the fontsize – SeeTheC Nov 27 '13 at 11:30
  • 1
    See this previous Stackoverflow answer: http://stackoverflow.com/questions/19988099/how-to-prevent-ugly-spikes-in-canvas-font-rendering/19988202#19988202 – markE Nov 27 '13 at 17:46

3 Answers3

4

It's not due to the typeface nor size, but due to how the lines in the stroke path are connected.

By changing the line-join method the spikes will go away:

context.lineJoin = 'round';
1

Also, if you prefer sharp corners (without the miter-spikes,) try this:

context.lineJoin = 'miter';
context.miterLimit = 2;

http://jsfiddle.net/hwG42/3/

It does the trick.

Jack
  • 2,229
  • 2
  • 23
  • 37
0

You might get better results when you use a different font.

Alternatively, you could use only fillText and create the outline with a shadow.

Philipp
  • 67,764
  • 9
  • 118
  • 153