未捕获的TypeError:无法执行' getImageData' on' CanvasRenderingContext2D':提供的double值是非限定的

时间:2016-04-25 04:28:44

标签: javascript html canvas

我写了一些代码来从网站上的画布中获取数据:

var checkPoints = ["+0,+0","+10,+10","+10,+0","+0,+10","-10,-10","-10,+0","+0,-10","+20,+20","+20,+0","+0,+20","-20,-20","-20,+0","+0,-20","+30,+30","+30,+0","+0,+30","-30,-30","-30,+0","+0,-30","+40,+40","+40,+0","+0,+40","-40,-40","-40,+0","+0,-40","+40,+40","+40,+0","+0,+40","-40,-40","-40,+0","+0,-40"];
var changePoints = ["600,0","-100,400","-100,300","600,580","-100,1000","1000,300","0,0","600,0","-100,400","-100,300","600,580","-100,1000","1000,300","0,0","600,0","-100,400","-100,300","600,580","-100,1000","1000,300","0,0","600,0","-100,400","-100,300","600,580","-100,1000","1000,300","0,0","600,0","-100,400","-100,300","600,580","-100,1000","1000,300","0,0"];
var checkRGB = [];
var oldCheckRGB = [];
for (i=0; i<checkPoints.length; i++) {
checkRGB.push("");
oldCheckRGB.push("");
}
var mousePos = myCanvas.relMouseCoords(e);
x = mousePos.x;
y = mousePos.y;
c = myCanvas.getContext("2d");
for (i=0; i<checkPoints.length; i++) {
    checkX = x+checkPoints[i].split(",")[0];
    checkY = y+checkPoints[i].split(",")[1];
    console.log("CheckX: "+checkX+", CheckY: "+checkY);
    checkRGB[i] = c.getImageData(x+checkX, y+checkY, ID("myCanvas").width, ID("myCanvas").height).data;
    if (checkRGB[i][0]-oldCheckRGB[i][0] > minimumColorChange || checkRGB[i][1]-oldCheckRGB[i][1] > minimumColorChange || checkRGB[i][2]-oldCheckRGB[i][2] > minimumColorChange) {
        xm = changePoints[i].split(",")[0];
        ym = changePoints[i].split(",")[1];
        console.log("XM set: "+xm+", YM set: "+ym);
    }
}

(每次移动鼠标时都会调用此代码) 我一直收到错误: canvasTest.js:17 Uncaught TypeError:无法执行&#39; getImageData&#39; on&#39; CanvasRenderingContext2D&#39;:提供的double值是非限制性的。

我已经对堆栈溢出,谷歌等进行了一些搜索,并且没有找到关于此错误或如何修复它的确定结果。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您正尝试在字符串数组中添加一个值,而不是任何强制转换:

 checkX = x+checkPoints[i].split(",")[0];
 checkY = y+checkPoints[i].split(",")[1];

也许你应该使用:

 checkX = x + parseFloat(checkPoints[i].split(",")[0]);
 checkY = y + parseFloat(checkPoints[i].split(",")[1]);