1

I try load my simple stage from json file, I load images but it's all in one layer, and image is not in group, so resize not working, i don't know how i should to load all elements with layers and group;

This is my function to load(it's only image for now)

   function load(loadStage) {
    counter = 2;
    activeStage = stage;
    stage = Kinetic.Node.create(loadStage,'right');
    var background = stage.get('.background');
    var backgroundImage = stage.get('.backgroundImage');
    var clipartsImage = stage.get('.image');
    var text = stage.get('.text');
    console.log(stage.getChildren());


   for(i=0;i<clipartsImage.length;i++){ 
     counter++;
      (function() { 

          var image = clipartsImage[i];
          groups[counter] = image.parent.get;
          //console.log(image);
          var imageObj = new Image();
          imageObj.onload = function() {
              imageObj.src = image.attrs.src;
              console.log(image);
              image.setImage(imageObj);

              image.on('mouseover', function() {
                  document.body.style.cursor = 'pointer';
              });
              image.on('mouseout', function() {
                  document.body.style.cursor = 'default';
              });

              image.on('click', function(evt) {
                  this.children[0].children[3].show();
                  var shape = evt.targetNode;
                  this.moveToTop();
                  active = layers[shape.getId()];
                  objectId = shape.getId();
              });
              image.getLayer().draw;
          };
          imageObj.src = image.attrs.src;
      })();

      stage.draw();
    }
    stage.draw();
}

And this is my json file, http://wklej.org/id/1105520/ this is middle stage. I'll keep trying, but if someone has had a similar problem and solved it, I would be grateful for your help :)

4

1 回答 1

0

1)

你的 JSON 文件是怎么回事?base64 字符串已经炸成了一百万行......

对于初学者来说,KineticJS.Layer不能有一个子层。参考:

https://github.com/ericdrowell/KineticJS/wiki/Change-Log#455-july-28-2013

新的 add() 验证。如果将无效的子级添加到父级(例如,将层添加到另一个层时),则会引发错误。

https://github.com/ericdrowell/KineticJS/wiki

未为分组图层的子级触发 KineticJS 事件

在您的 JSON 文件中,您有大量的层相互分层,这肯定会导致问题。实际上,您拥有的每个组看起来都有一个图层!这是完全错误的 KineticJS 结构。

您应该对如何以及何时使用图层和组进行一些研究,这里有一些参考资料:

KineticJs中组和层之间有什么区别

https://github.com/ericdrowell/KineticJS/wiki(同样是 KineticJS 的数据结构层次结构)

2)

无需调用stage.draw()两次。

3)

JSON 是一种数据格式,它只存储对象属性。这意味着它存储对附加到对象的事件的任何引用。

这意味着当您加载 JSON 字符串时,您必须updateAnchor再次将该函数重新绑定到所有锚点。(类似于您如何将click功能重新绑定到您的图像 onload)

在使用 JSON 文件创建阶段后,您必须在加载函数中执行类似的操作:

var children = layer.getChildren(); //Use the layer that has all your groups with images/anchors. *REMEMBER you can't layers within layers!

for (var i=0; i<children.length; i++) {
  //If name starts with 'anchor'
  if (children.getName().indexOf('anchor') !== -1) {
    children[i].on('dragmove', function(evt) {
      updateAnchor(this);
      layer.draw();
    });
  }
}
于 2013-08-13T19:01:22.880 回答