【问题标题】:Save image from Stage with offset KineticJS HTML5使用偏移 KineticJS HTML5 从舞台保存图像
【发布时间】:2012-11-09 16:40:13
【问题描述】:

我想保存 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) 舞台区域。有什么想法吗?

【问题讨论】:

    标签: javascript html canvas kineticjs stage


    【解决方案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({.....});
     }
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 2014-05-19
      • 2014-06-01
      • 2012-10-30
      • 2014-03-02
      • 2013-04-04
      相关资源
      最近更新 更多