0

我正在尝试从带有表格的照片中获取单元格。我有图像中细胞的坐标。现在我想使用 konvajs 库查看我的单元格。问题是桌子是30x30。所以我有900个细胞。当使用 kanva.image 900 次时,浏览器停止工作,因为它尝试加载 900 次相同的图像。我想加载一次图像并将其用于裁剪 900 次。这是我的代码:

function add_i(layer,cell,row,weight,k,cloneI){         

        layer.add(cloneI);

        cloneI.crop({
              x: parseInt(cell.x),
              y: parseInt(row.y),
              width: cell.width,
              height: row.height
            });
            cloneI.width(cell.width);
            cloneI.height(row.height);
            cloneI.y(row.y);
            cloneI.x(cell.x);

    }
    layerP.push( new Konva.Layer());
    if(weight.stage == 'pred'){
      var cloneI = new Konva.Image({
            id:'img_'+k,
            draggable: true
      });
      var clone = new Image();

      clone.onload = function() {
            cloneI.image(clone);               

            layerP[0].draw();
      };


      for (var i in weight.predictions){
        var row = weight.predictions[i];
        for (var j in row.cells){
          var cell = row.cells[j];  
          add_i(layerP[0],cell,row,weight,k,cloneI.clone());              

          k+=1;
        }
        if(i==4 && false)
          break;
      }
      clone.src = weight.path_i;
      stage.add(layerP[0]); 
    }
4

1 回答 1

2

我认为你应该像这样移动你add_iclone.onload

function add_i(layer, cell, row, weight, k, cloneI) {

  layer.add(cloneI);

  cloneI.crop({
    x: parseInt(cell.x),
    y: parseInt(row.y),
    width: cell.width,
    height: row.height
  });
  cloneI.width(cell.width);
  cloneI.height(row.height);
  cloneI.y(row.y);
  cloneI.x(cell.x);

}
layerP.push(new Konva.Layer());
if (weight.stage == 'pred') {
  var clone = new Image();

  clone.onload = function() {
    var cloneI = new Konva.Image({
      id: 'img_' + k,
      draggable: true,
      image: clone
    });

    for (var i in weight.predictions) {
      var row = weight.predictions[i];
      for (var j in row.cells) {
        var cell = row.cells[j];
        add_i(layerP[0], cell, row, weight, k, cloneI.clone());

        k += 1;
      }
      if (i == 4 && false)
        break;
    }
    layerP[0].draw();
  };
  clone.src = weight.path_i;
  stage.add(layerP[0]);
}

导致在回调add_i之前调用,onload因为Konva.Image即cloneI在使用时没有本机图像实例。

于 2017-04-17T05:00:38.297 回答