Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用offset KineticJS HTML5从后台保存图像_Javascript_Html_Canvas_Kineticjs_Stage - Fatal编程技术网

Javascript 使用offset KineticJS HTML5从后台保存图像

Javascript 使用offset KineticJS HTML5从后台保存图像,javascript,html,canvas,kineticjs,stage,Javascript,Html,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. *

我想保存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()方法,所以您必须恢复到在stage 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({.....});
 }

因为没有crop()方法,所以您必须恢复到沿两个方向移动stage 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({.....});
 }