我正在循环遍历canvas元素的imageData像素数组。我想循环遍历第一行(顶部)和最后一行(底部)像素。我该怎么做?
这就是我在整个像素阵列中循环的方式:
//Var declarations, etc.
imageData = context.getImageData(0, 0, cols, rows);
for (var i = 0, max = imageData.data.length; i < max; i+=4) {
//This is looping through the entire array.
}
答案 0 :(得分:5)
一种方法是单独获取顶行和底行,然后遍历它们:
imageDataTop = context.getImageData(0,0,cols,1);
imageDataBottom = context.getImageData(0,rows-1,cols,1);
此处有更多信息:https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData