简体   繁体   English

如何使用konva.image多次裁剪相同的图像

[英]how to crop same image multiple times with konva.image

i am trying to get cells from a photo with a table. 我试图从一张桌子的照片中获取单元格。 i have the coordonates of the cells in the image. 我有图像中的细胞的coordonates。 now i want to view my cells using a konvajs library. 现在我想使用konvajs库查看我的单元格。 the problem is that the table is 30x30. 问题是表格是30x30。 so i have 900 cells. 所以我有900个牢房 when using kanva.image 900 times the browser stop working, because it tries to load 900 time the same image. 当使用kanva.image 900倍时,浏览器将停止工作,因为它尝试加载900倍的相同图像。 i want to load one time the image and use it for croping 900 time. 我想加载一张图像并将其用于裁剪900次。 here are my code: 这是我的代码:

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]); 
    }

I think you should move your add_i into clone.onload like this: 我认为您应该将add_i移到clone.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]);
}

cause the add_i is invoked before the onload callback because of that Konva.Image ie cloneI does not have native image instance when it's used. 导致add_i是之前被调用onload因为回调Konva.Image即cloneI时,它的使用不具有本机映像实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM