为什么赢了';我的javascript/画布多边形填充?

为什么赢了';我的javascript/画布多边形填充?,javascript,canvas,html5-canvas,Javascript,Canvas,Html5 Canvas,所以我创建了一个程序,可以从本地网络服务器获取美国的坐标列表。客户端可以通过发出一个简单的get请求来获取这些坐标。我试图单独填写各州,但这似乎不起作用。我的代码如下所示 ctx.fillStyle= "#f00"; //coords holds an array of JSON objects, which store an array of coordinates under the 'coords' tag. These coordinates are stored in an array

所以我创建了一个程序,可以从本地网络服务器获取美国的坐标列表。客户端可以通过发出一个简单的get请求来获取这些坐标。我试图单独填写各州,但这似乎不起作用。我的代码如下所示

ctx.fillStyle= "#f00";
//coords holds an array of JSON objects, which store an array of coordinates under the 'coords' tag. These coordinates are stored in an array, which holds [x,y].
for (var i=0; i<coords.length; i++){
            obj = coords[i]['coords'];
//Begin the path
            ctx.beginPath();
            for (var j=0; j<obj.length; j++){
//Draw each point on the path
                if (j!=obj.length-1){
                    ctx.moveTo(obj[j][0],obj[j][1])
                    ctx.lineTo(obj[j+1][0],obj[j+1][1])
                }
            }
//Close the path
            ctx.closePath();
            ctx.fill();
            ctx.stroke();
        }
ctx.fillStyle=“#f00”;
//coords保存一个JSON对象数组,它在“coords”标记下存储一个坐标数组。这些坐标存储在一个数组中,该数组保存[x,y]。

对于(var i=0;i每次执行moveTo时,都会“打断”曲线。我建议您首先只使用moveTo,然后只使用lineTo:

ctx.fillStyle= "#f00";
//coords holds an array of JSON objects, which store an array of coordinates under the 'coords' tag. These coordinates are stored in an array, which holds [x,y].
for (var i=0; i<coords.length; i++){
     obj = coords[i]['coords'];
     drawObj(obj);
}
ctx.fillStyle=“#f00”;
//coords保存一个JSON对象数组,该数组在“coords”标记下存储一个坐标数组。这些坐标存储在一个数组中,该数组保存[x,y]。

对于(var i=0;iYou's welcome.“you's breaking The curve”的真正含义是:“您在当前路径中创建了一个新的子路径”。但无论如何!
function drawObj(obj) {
         if (obj.length<2) return;
         ctx.beginPath();
         var currObj = obj[0];
         ctx.moveTo(currObj[0], currObj[1]);
         for (var j=1; j<obj.length; j++){
             currObj = =obj[j]
             ctx.lineTo(currObj[0],currObj[1])
         }
         ctx.closePath();
         ctx.fill();
         ctx.stroke();
}