为什么画布中的前一个圆圈的属性会被更新的属性替换?

时间:2015-01-22 10:49:49

标签: javascript html html5 canvas

我有画布,我想在其中画一个内圈,在它上面画一个外圈。首先在圆圈之间显示一条连线。其次,我想要外圈的lineWidth为5,它也是外圈为5。 怎么阻止这个?

我的代码,

    <script>
        function drawCircle() {
            var circleCanvas = document.getElementById("myCircleCanvas");
            var circle = circleCanvas.getContext("2d");
            circle.beginPath();
            var x = 95;
            var y = 75;
            var radius = 60;

            circle.arc(x, y, radius, 0, 2 * Math.PI);
            circle.stroke();

            circle.fillStyle = '#505050';
            circle.fill();
            circle.fillStyle = '#fff';

            var font = "bold " + radius / 3 + "px serif";
            circle.font = font;
            circle.fillText("AVAYA", x - x / 3, y - y / 5);

            circle.fillStyle = '#000';
            circle.fillText("CIE", x - x / 3 + 15, y - y / 5 + 25);

            circle.arc(x, y, radius + 10, 0, 2 * Math.PI);
            circle.lineWidth = 5;//just want to increase outer circle
            circle.stroke(); //why drawing extra line
        }
    </script>
<canvas id="myCircleCanvas">Your browser does not support the HTML5 canvas tag.
        </canvas>

1 个答案:

答案 0 :(得分:0)

如果您希望独立处理这两个圆,请在绘制第二个圆之前关闭您开始的路径并开始一个新圆。例如:

function drawCircle() {
    var circleCanvas = document.getElementById("myCircleCanvas");
    var circle = circleCanvas.getContext("2d");
    circle.beginPath();
    var x = 95;
    var y = 75;
    var radius = 60;

    circle.arc(x, y, radius, 0, 2 * Math.PI);
    circle.stroke();

    circle.fillStyle = '#505050';
    circle.fill();
    circle.fillStyle = '#fff';

    var font = "bold " + radius / 3 + "px serif";
    circle.font = font;
    circle.fillText("AVAYA", x - x / 3, y - y / 5);

    circle.fillStyle = '#000';
    circle.fillText("CIE", x - x / 3 + 15, y - y / 5 + 25);

    circle.closePath();
    circle.beginPath();

    circle.arc(x, y, radius + 10, 0, 2 * Math.PI);
    circle.lineWidth = 5;//just want to increase outer circle
    circle.stroke(); //why drawing extra line

    circle.closePath();
}

http://jsfiddle.net/mudbo48j/1