html 5 canvas LineTo()线色问题

sho*_*ive 0 javascript html5 canvas

我在HMTL 5 2D画布上绘制了5条水平线:

var canvas_ctx = my_canvas.getContext("2d");
    canvas_ctx.lineWidth = 0.5;
    canvas_ctx.strokeStyle = "black";

    {
        let line_x = 0;
        let line_length = canvas_ctx.width;
        let offset = 5;
        let numLines = 5;
        let numYincrement = 10;
        for (let i=0;i<numLines * numYincrement;i+=numYincrement) {
            //canvas_ctx.beginPath();
            canvas_ctx.moveTo(line_x,i + offset);
            canvas_ctx.lineTo(line_length,i + offset);
            canvas_ctx.stroke();
            //canvas_ctx.closePath();
        }
    }
Run Code Online (Sandbox Code Playgroud)

理想情况下,这应导致出现5条黑线。取而代之的是,线条的颜色似乎随着每条新线条而逐渐褪色(好像是渐变!),因此第5行是灰色。如果我取消注释canvas_ctx.beginPath();和canvas_ctx.closePath();,则所有行变为灰色。为什么会这样?

Kai*_*ido 5

笔划确实从坐标的两侧重叠。

var ctx = c.getContext('2d');
ctx.strokeStyle="red";
// draw big
ctx.scale(30, 30);
ctx.beginPath();
ctx.moveTo(5, 0);
ctx.lineTo(5, 10);
ctx.stroke();

drawPixelGrid();


function drawPixelGrid() {
  // simply renders where the pixel bounds are
  ctx.beginPath();
  // remove the zoom
  ctx.setTransform(1,0,0,1,0,0);
  ctx.strokeStyle = 'gray';
  ctx.lineWidth = 2; // avoid the problem we are demonstrating by using a perfect lineWidth ;-)

  for(let y=0; y<=300; y+=30) {
    ctx.moveTo(0, y);
    ctx.lineTo(300, y);
    for(let x=0; x<=300; x+=30) {
      ctx.moveTo(x, 0);
      ctx.lineTo(x, 300);
    }
  }
  ctx.stroke();
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="c" height=300></canvas>
Run Code Online (Sandbox Code Playgroud)

但是很明显,像素不能同时设置为两种颜色。因此,浏览器会应用抗锯齿功能,这会将您的像素颜色淡化为其他颜色,这是将背景色和前景色混合在一起的结果。因此,对于白色或透明背景上的黑色笔触,这会导致渲染实际的灰色像素。在这里,我将继续以红色为例:

var ctx = c.getContext('2d');
ctx.strokeStyle="red";
// first draw as on a 10*10 canvas
ctx.beginPath();
ctx.moveTo(5, 0);
ctx.lineTo(5, 10);
ctx.stroke();

// zoom it
ctx.imageSmoothingEnabled = 0;
ctx.globalCompositeOperation = 'copy';
ctx.drawImage(c, 0,0,9000,9000);

drawPixelGrid();

// this is not red...

function drawPixelGrid() {
  ctx.globalCompositeOperation = 'source-over';
  ctx.beginPath();
  ctx.setTransform(1,0,0,1,0,0);
  ctx.strokeStyle = 'gray';
  ctx.lineWidth = 2;

  for(let y=0; y<=300; y+=30) {
    ctx.moveTo(0, y);
    ctx.lineTo(300, y);
    for(let x=0; x<=300; x+=30) {
      ctx.moveTo(x, 0);
      ctx.lineTo(x, 300);
    }
  }
  ctx.stroke();
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="c" height=300></canvas>
Run Code Online (Sandbox Code Playgroud)

避免这种情况的一种方法通常是在坐标上应用偏移,以使线条在像素边界上正确延伸。例如,对于1px的lineWidth,您将应用0.5的偏移量:

var ctx = c.getContext('2d');
ctx.strokeStyle="red";
// first draw as on a 10*10 canvas
ctx.beginPath();
ctx.moveTo(5.5, 0); // offset +0.5px
ctx.lineTo(5.5, 10);
ctx.stroke();

// zoom it
ctx.imageSmoothingEnabled = 0;
ctx.globalCompositeOperation = 'copy';
ctx.drawImage(c, 0,0,9000,9000);

drawPixelGrid();
// now we've got a real red

function drawPixelGrid() {
  ctx.globalCompositeOperation = 'source-over';
  ctx.beginPath();
  ctx.setTransform(1,0,0,1,0,0);
  ctx.strokeStyle = 'gray';
  ctx.lineWidth = 2;

  for(let y=0; y<=300; y+=30) {
    ctx.moveTo(0, y);
    ctx.lineTo(300, y);
    for(let x=0; x<=300; x+=30) {
      ctx.moveTo(x, 0);
      ctx.lineTo(x, 300);
    }
  }
  ctx.stroke();
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="c" height=300></canvas>
Run Code Online (Sandbox Code Playgroud)

但是在您的情况下,您将以0.5px的lineWidth进行绘制,因此没有偏移量可以消除这种抗锯齿。

因此,如果您想要完美的颜色,请选择正确的lineWidth。