Drawing a 1px thick line in canvas creates a 2px thick line

前端 未结 8 1772
抹茶落季
抹茶落季 2020-12-08 04:01

In this jsfiddle there\'s a line with a lineWidth of 1.

http://jsfiddle.net/mailrox/9bMPD/350/

e.g:

ctx.lineWidth = 1;

How

相关标签:
8条回答
  • 2020-12-08 04:37

    Did you see the first hit on google? (search for canvas line width 1px). Though I have to admit this isn't exactly "clean" or "lean". Ferry Kobus' solution is much better. Then again: it sucks you need to use "half pixels" in the first place...

    0 讨论(0)
  • 2020-12-08 04:42

    You can also translate by half a pixel in the X and Y directions and then use whole values for your coordinates (you may need to round them in some cases):

    context.translate(0.5, 0.5)
    
    context.moveTo(5,5);
    context.lineTo(55,5);
    

    Keep in mind that if you resize your canvas the translate will be reset - so you'll have to translate again.

    You can read about the translate function and how to use it here:

    https://www.rgraph.net/canvas/reference/translate.html

    This answer explains why it works that way.

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