1

I want to save part of a KineticJS stage. This code is working perfect:

stage.toDataURL({
        width: 350,
        height: 350,
        mimeType: "image/jpeg",
        callback: function(dataUrl) {
          /*
           * here you can do anything you like with the data url.
           * In this tutorial we'll just open the url with the browser
           * so that you can see the result as an image
           */
          window.open(dataUrl);
        }
      });
    }, false);

But what i want is to add an offset to that, so image would start and coordinates (75,75) of the stage area. Any idea?

4

1 回答 1

1

好吧,由于没有crop() 方法,因此您必须恢复为双向移动舞台75 上的所有对象,幸运的是,这并不是很困难。

就像是:

 var layersList = stage.getChildren();
 for (var layerNum in layersList){   //loop through all layers
     var childList = layerList.getChildren();  //get all children of the layer
     for(var childNum in childList){
          childList[childNum].move(-75,-75);
     }
 }
 stage.draw();
 stage.toDataURL({.....});

您可以通过使用相同的代码并执行 .move(75,75); 来撤消此操作。将每个项目放回原来的位置

或者,如果您想通过函数定义偏移量,只需执行以下操作:

 function moveStage(offsetX, offsetY){
    var layersList = stage.getChildren();
    for (var layerNum in layersList){   //loop through all layers
        var childList = layerList.getChildren();  //get all children of the layer
        for(var childNum in childList){
             childList[childNum].move(-offsetX,-offsetY);
        }
    }

 stage.draw();
 stage.toDataURL({.....});
 }
于 2013-01-10T14:53:13.537 回答