html 5 canvas LineTo()线颜色问题

时间:2018-04-09 13:04:05

标签: javascript html5 canvas

我正在为HMTL 5 2D画布绘制五条水平线:

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();
        }
    }

理想情况下,这应该会产生5条黑线。相反,线条的颜色似乎随每条新线条淡化(好像它是一个渐变!),因此第5行是灰色的。如果我取消注释canvas_ctx.beginPath();canvas_ctx.closePath();,则所有行都会变为灰色。为什么会发生这种情况?

1 个答案:

答案 0 :(得分:4)

笔划从坐标的两边重叠。

&#13;
&#13;
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();
}
&#13;
<canvas id="c" height=300></canvas>
&#13;
&#13;
&#13;

但显然,像素不能同时设置为两种颜色。因此浏览器会应用antialiasing,这会将您的像素颜色淡化为其他颜色,这是混合背景和前景色的结果。 因此,对于白色或透明背景上的黑色笔划,这会导致呈现实际的灰色像素。在这里,我将继续使用红色作为例子:

&#13;
&#13;
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();
}
&#13;
<canvas id="c" height=300></canvas>
&#13;
&#13;
&#13;

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

&#13;
&#13;
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();
}
&#13;
<canvas id="c" height=300></canvas>
&#13;
&#13;
&#13;

但是在你的情况下,你绘制的是0.5px lineWidth,所以没有偏移量可以摆脱这种抗锯齿。

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