html - Canvas 元素中的线条粗细

标签 html canvas

我想在 Canvas 元素中制作粗细为 1px 的线条。

不过我似乎找不到一个正确的例子。我目前正在使用本网站上的方法 https://developer.mozilla.org/samples/canvas-tutorial/4_5_canvas_linewidth.html

在这个例子中,应该是 1px 的线看起来实际上是 2px 但颜色更浅。这在 Chrome 10 和 Firefox 4 中都存在。

我希望最左边那条线的宽度与该页标题下划线的宽度相同。

是否有另一种方法可以画一条线来获得我正在寻找的结果。

最佳答案

注意部分

However, the leftmost and all other odd-integer-width thickness lines do not appear crisp, because of the path's positioning.

Obtaining crisp lines requires understanding how paths are stroked. In the images below, the grid represents the canvas coordinate grid. The squares between gridlines are actual on-screen pixels. In the first grid image below, a rectangle from (2,1) to (5,5) is filled. The entire area between them (light red) falls on pixel boundaries, so the resulting filled rectangle will have crisp edges.

因此,如果您以半像素绘制(当绘制奇数像素宽度的线时),那么实际绘制的边缘将落在绝对像素上并且看起来很好..

例子在 http://jsfiddle.net/Wav5v/


或者您可以使用 fillRect(x,y,width,height)宽度为 1..

function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
  for (i=0;i<10;i++){
    ctx.fillRect(5 + i*14, 5, 1+i, 140); 
  }
}

例子在 http://jsfiddle.net/Wav5v/1/

关于html - Canvas 元素中的线条粗细,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679295/

相关文章:

html - 向右滚动时左侧粘性元素消失

iphone - 编码不存在于 HTTP header 中,如何在 HTML header 中找到它? (iPhone)

javascript - 如何让 Canvas 元素到达 Canvas 中心

javascript - 裁剪图像Processing.js

php - 写了一个代码来显示随机照片,但在给定时间只需要显示四个 img

javascript - 如何在包含 &lt;script&gt; 标签时使用 $sce.trustAsHtml

javascript - 以编程方式附加内容而不丢失 html 标签

javascript - 如何更改此 d3.js 图表

javascript - 将 Canvas 另存为 jpg 到桌面

javascript - 使用kinetic js在 Canvas 中显示多个对象