Canvas strokeStyle not reliably changing?

丶灬走出姿态 提交于 2020-01-24 08:18:10

问题


Here's my code. For some reason it draws the lines in mostly grey.

It would appear that some of the lines are being drawn with two stroke styles on top of each other, even though my draw calls don't actually overlap. Some of the lines are white with inner grey. My white lines are thicker than my grey ones so obviously it's drawing two lines. Are canvas draw calls asynchronous or something?

Any idea why?

        for (var i=0; i<minor_lanes.length; i++) {
            connect(minor_lanes[i], "#333", 3);
        }

        for (var i=0; i<major_lanes.length; i++) {
            connect(major_lanes[i], "#fff", 4);
        }

        for (var i=0; i<limited_lanes.length; i++) {
            connect(limited_lanes[i], "#FFFF99", 2);
        }

        function connect(id, color, width) {
            if (!id) {
                return;
            }
            ctx.lineWidth = width;
            ctx.strokeStyle = color;
            $('#' + id).each(function() {
            var laneX = parseInt($(this).css('left')) + $(this).width()/2;
            var laneY = parseInt($(this).css('top')) + $(this).height()/2;
            ctx.moveTo(x,y);
            ctx.lineTo(laneX, laneY);
            ctx.stroke();
            });
        }

回答1:


I think you forget

ctx.beginPath();
// draw path
// stroke
ctx.closePath();


来源:https://stackoverflow.com/questions/7683662/canvas-strokestyle-not-reliably-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!