javascript - HTML5 Canvas 描边重叠

标签 javascript html canvas overlap stroke

这里我创建了一个线条路径和带有路径的笔划,但是与我需要的笔划没有重叠:

代码和演示:http://jsbin.com/yepigu/12/edit?output

ctx.strokeStyle='yellowgreen';
  drawPolyline(pts);

一切都很好,但正如你所看到的,我看不到 y 笔划重叠

如何更改此示例以显示与笔划的重叠?

最佳答案

您可以绘制红色轮廓、粉红色填充、蓝色描边的折线,以显示每个线段中的重叠部分。

enter image description here

关键是单独绘制每个线段,而不是在单个路径中绘制它。另外,从最粗到最细的位置绘制线条的红色、粉色和蓝色部分。

这里是示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;

var pts=[{x:50,y:50},{x:200,y:200},{x:50,y:200},{x:200,y:50}];

ctx.lineCap='round';
ctx.lineJoin='round';

drawPolyline(pts);


function drawPolyline(pts){
  for(var i=1;i<pts.length;i++){
    //
    ctx.lineWidth=25;
    ctx.strokeStyle='red';
    ctx.beginPath();
    ctx.moveTo(pts[i-1].x,pts[i-1].y);
    ctx.lineTo(pts[i].x,pts[i].y);
    ctx.stroke();
    //
    ctx.lineWidth=22;
    ctx.strokeStyle='pink';
    ctx.beginPath();
    ctx.moveTo(pts[i-1].x,pts[i-1].y);
    ctx.lineTo(pts[i].x,pts[i].y);
    ctx.stroke();
    //
    ctx.lineWidth=3;
    ctx.strokeStyle='blue';
    ctx.beginPath();
    ctx.moveTo(pts[i-1].x,pts[i-1].y);
    ctx.lineTo(pts[i].x,pts[i].y);
    ctx.stroke();
  }
}
body{ background-color: ivory; padding:10px; }
#canvas{border:1px solid red;}
<canvas id="canvas" width=600 height=600></canvas>

关于javascript - HTML5 Canvas 描边重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28072249/

相关文章:

javascript - 白色背景打破了我的 Canvas

javascript - 我的 Tile Stamper 不工作

api - Facebook API、iFrame 中的 Canvas 应用、Javascript API、IE8 中的损坏?

javascript - Google+ 参数无法正常工作

javascript - 需要有关灰尘模板的建议

javascript - 如何对 HTML 表格执行实时搜索和过滤

Javascript 递归调用产生幽灵结果

javascript - 我怎样才能制作一个互动的自动收报机?

javascript - 使用javascript或jquery手动触发输入框中的验证错误

javascript - 将数字相加