代码之家  ›  专栏  ›  技术社区  ›  Bob

如何在圆内画笔画?

  •  1
  • Bob  · 技术社区  · 11 年前

    有没有一种简单的方法可以在一个圆圈内画笔画(不需要画两个圆圈和类似的变通方法)?如果我这样做:

    context.beginPath();
    context.arc(200, 200, 93, Math.PI / 2, Math.PI, true);
    context.fillStyle = '#FF6A6A';
    context.fill();
    context.lineWidth = 20;
    context.strokeStyle = '#FF0000';
    context.stroke();
    

    我明白了:

    enter image description here

    笔划部分绘制在图形外部(用绿色圆圈标记),而我需要它在内部。

    1 回复  |  直到 11 年前
        1
  •  3
  •   ericjbasti    11 年前

    应更改半径以补偿线宽:

    context.beginPath();
    context.arc(200, 200, 93, Math.PI / 2, Math.PI, true);
    context.fillStyle = '#FF6A6A';
    context.fill();
    context.lineWidth = 20;
    context.strokeStyle = '#FF0000';
    context.beginPath();
    
    // the radius of 93 - half the line width
    context.arc(200, 200, 93-10, Math.PI / 2, Math.PI, true);
    
    context.stroke();
    

    JS箱: http://jsbin.com/qamuwumiri/edit?html,js,output