我想保存 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)。有什么想法吗?
最佳答案
好吧,由于没有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({.....});
}
关于javascript - 使用偏移 KineticJS HTML5 从舞台保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13493099/