HTML 5 canvas font being ignored

后端 未结 2 1301
臣服心动
臣服心动 2020-12-19 03:09

I\'m trying to write some text to a canvas element, but it seems that the font options I put in are being completely ignored. No matter what I change them to, it all comes o

相关标签:
2条回答
  • 2020-12-19 03:25

    That this can also happen if you reset the size of the canvas. At least, I saw this in Chrome 23 today.

    context.font = 'bold 20px arial';
    canvas.width = 100;
    canvas.height = 100;
    console.log(context.font); // gives '10px sans-serif'
    
    0 讨论(0)
  • 2020-12-19 03:30

    As it turns out, the ctx.font change needs to be used in the same function that is doing the fillText()

    This makes it work like a charm.

    EDIT

    As richtaur mentioned in his comment, this answer is wrong. Your context needs to be saved and restored using ctx.save() and ctx.restore() as it currently gets reset when you call canvas.getContext('2d') again.

    0 讨论(0)
提交回复
热议问题