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

标签 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.
           * 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/

相关文章:

javascript - 参数 '[controllerName]' 不是 NaNunction,在转换为 TypeScript 时未定义

javascript - 如何使用knockout.js通过点击添加一项或删除一项

javascript - Canvas 部分圆

javascript - 将 Canvas 图像作为文件流 HTML5 发送

javascript - HTML5 Canvas : adding collision to constantly drawn images

javascript - Rails 窗体 : after click - disable link

javascript - 选择输入框时,屏幕键盘会调整应用程序大小 Apache Cordova Visual Studio?

html - 显示内联 block 和图像

html - 如何缩小形成 V 形的两个旋转矩形之间的间隙?

javascript - 需要以何种方式将事件设置为*捕获*?