0

I have several layers of KineticJS canvases which are all on top of each other. I'd like to be able to export these as a PNG. I can export each of the layers by using the .toDataURL() function, but I'd like them all as one image.

Is there a way to merge all the base64 strings?

4

2 回答 2

3

@apsillers 有正确的想法——将每个图像相互叠加并导出最终合成。

KineticJS 有一个导出所有图层组合的快捷方式:stage.toDataURL().

stage.toDataURL将其所有层的组合导出到 dataUrl。

警告:与所有画布图像导出一样,您必须确保所有图像都符合 CORS。

这是示例代码:

var stage = new Kinetic.Stage({
    container: 'container',
    width: 400,
    height: 400
});
var layer1 = new Kinetic.Layer();
stage.add(layer1);
var layer2 = new Kinetic.Layer();
stage.add(layer2);


var img1=new Image();
img1.onload = function() {
    var image1 = new Kinetic.Image({
      x: 0,
      y: 0,
      image: img1,
      width: 100,
      height: 100
    });
    layer1.add(image1);
    layer1.draw();
}
img1.src="yourCORScompliantImage1.png";

var img2=new Image();
img2.onload = function() {
    var image2 = new Kinetic.Image({
      x: 0,
      y: 0,
      image: img2,
      width: 100,
      height: 100
    });
    layer2.add(image2);
    layer2.draw();
}
img2.src="yourCORScompliantImage2.png";

$("#export").click(function(){
        stage.toDataURL({
          callback: function(dataUrl) {
              window.open(dataUrl);
          }
        });
});
于 2013-10-23T16:40:12.930 回答
1

you can make svg containing

<image width="100" height="100" xlink:href="data:image1/png;base64,...">
<image width="100" height="100" xlink:href="data:image2/png;base64,...">
<image width="100" height="100" xlink:href="data:image3/png;base64,...">
<image width="100" height="100" xlink:href="data:image4/png;base64,...">

will be 4 layered svg

于 2013-10-23T13:08:20.863 回答