【问题标题】:KineticJS drag and drop from one layer to anotherKineticJS 从一层拖放到另一层
【发布时间】:2014-03-29 00:40:23
【问题描述】:

现在,我有一个舞台,两个图层和在 leftLayer 上绘制的图像。我通过添加属性draggable: true 使图像可拖动。我找到的所有拖放示例(动力学文档、教程、jsfiddles...)都在同一个区域内。

我怎样才能使在左侧图层上绘制的项目仅被删除在右侧图层中,否则,让它们像在 jquery 中一样恢复(如果它是可实现的)?

这是我正在编辑的the jsfiddle。救命!

【问题讨论】:

  • 我实际上需要的是如何验证dropzone是否是正确的层,通过使用dragendmouseup或其他绑定事件,然后allowDrop()。如何实现?

标签: drag-and-drop kineticjs


【解决方案1】:

您需要做的是克隆对象,触发 dragstart 事件,然后在 dragend 检查项目是否放置在正确的容器中,否则,将其动画回原始容器。

不带revert的例子:http://cs.neiu.edu/~tsam/physics/index.phtml(你可以用user:test登录,pass:test)

示例代码:

itemBeingCloned.on('mousedown touchstart', function(){ var userPos = stage.getPointerPosition();

    var cloneOfItem= new Kinetic.Image({
        image: imageObj, // image of object being cloned
        x: userPos.x,
        y: userPos.y,
        height: 25, // or set the height
        width: 25, // or set the width
        draggable: true,
        offset: 12,
        dragOnTop: false // you might actually allow this to be true
    });
    yourTemporaryLayer.add(cloneOfItem); // well, you need to store this in a layer temporarily, you should have a layer which takes up the whole stage, and two child layers under it, left and right 

    /* Lets define the behavior (events) of the item you want to copy */
    cloneOfItem.on('dragmove', function(){ 
        // in case you need to do something while moving the item
    });

    cloneOfItem.on('dragend', function(){ // once you are done dragging, check if placing somewhere, most of the logic will go here
        // check if on right side, 
            //if so, add to right layer, else
            //else animate back to original position, then destroy
    });

    cloneOfItem.on('dblclick dbltap', function(evt){ // in case you want to remove the item, I defined it as double-click event destroying the item
        this.remove(); // remove from layer
        this.destroy(); // destroy object
    });

    /* so when you do mousedown on the original item, it will create the item, and fire the previously defined events (above) */
    cloneOfItem.fire('mousedown');
    cloneOfItem.fire('touchstart');
    cloneOfItem.fire('dragstart');

    yourRightLayer.draw(); //redraw the layer just in case? maybe not needed

});

更新(更简单)

示例代码:

itemBeingCloned.on('mousedown touchstart', function(){
    var userPos = stage.getPointerPosition();

    var cloneOfItem= itemBeingCloned.clone();

    yourTemporaryLayer.add(cloneOfItem); // well, you need to store this in a layer temporarily, you should have a layer which takes up the whole stage, and two child layers under it, left and right 

    cloneOfItem.on('dragend', function(){ // once you are done dragging, check if placing somewhere, most of the logic will go here
        // check if on right side, 
            //if so, add to right layer, else
            //else animate back to original position, then destroy
    });

    /* so when you do mousedown on the original item, it will create the item, and fire the previously defined events (above) */
    cloneOfItem.fire('mousedown');
    cloneOfItem.fire('touchstart');
    cloneOfItem.fire('dragstart');

    yourRightLayer.draw(); //redraw the layer just in case? maybe not needed

});

更新:这是一个非常粗略的实现 http://jsfiddle.net/GLFxF/16/

【讨论】:

  • 非常感谢!!但矩形有一个奇怪的行为:/
  • 是的,您必须稍微刷新一下逻辑,以便它只克隆项目。你原来的“房间”也不应该是可拖动的。
  • 你想要的逻辑是在鼠标按下时克隆项目,然后立即能够拖动它
  • 不错!现在您只需要分组逻辑,以便将项目拖入组中。
  • 是的!好吧,这就是现在的问题,我尝试在没有 Kinetic.Group 的情况下使用 layer.find(".item") 就像这里的 jsfiddle.net/m1erickson/Zr6TE 一样。我做了一个小小提琴,但对我没有用:(jsfiddle.net/v778S/1我不知道出了什么问题(见房间对象的拖动)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 2015-03-24
  • 2012-12-21
  • 2013-01-07
相关资源
最近更新 更多