Canva Javascript

时间:2018-01-07 22:25:21

标签: javascript arrays canvas

我尝试在2维中转换1维像素数组,但循环在数组的所有单元格中创建了相同的对象。所有对象都具有相同的'i',它等于'data.length'。我不明白为什么!

// Initialisation of the array
let colonne = 0; let ligne= 0; let width = 0; let i = 0;let g = 0;
for(i = 0; i <= data.length; i += 4) {
    tableau[[colonne][ligne]] = new Pixel(data[i], data[i + 1], data[i + 2], data[i + 3], i);
    k++; width += 1; g += 4;
    if (width == image_width) { colonne++; ligne= 0; width = 0; }
}

// Trace of the segment
var dx, dy, cumul, x1, y1;
xi = 180; yi = 80; xf = 219; yf = 266;
x1 = xi ;
y1 = yi ;
dx = xf - xi ;
dy = yf - yi ;

data[tableau[[x1][y1]].id_pixel] = 255; 
data[tableau[[x1][y1]].id_pixel + 1] = 0;   
data[tableau[[x1][y1]].id_pixel + 2] = 0;

cumul = dx / 2 ;
for (x1 = xi + 1; x1 <= xf; x1++) {
  cumul += dy ;
  if (cumul >= dx) { cumul -= dx; y1 += 1 ; }
  data[tableau[[x1][y1]].id_pixel] = 255; 
  data[tableau[[x1][y1]].id_pixel + 1] = 0; 
  data[tableau[[x1][y1]].id_pixel + 2] = 0;
}

0 个答案:

没有答案