使用偏移量KineticJS HTML5从舞台保存图像

时间:2012-11-21 12:19:21

标签: javascript html5 canvas kineticjs stage

我想保存KineticJS舞台的一部分。这段代码非常完美:

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

但我想要的是添加一个偏移,所以图像将开始和舞台区域的坐标(75,75)。有什么想法吗?

1 个答案:

答案 0 :(得分: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({.....});
 }