javascript - KineticJS/Javascript动态创建和即时拖动

标签 javascript html events logic kineticjs

我想要做的是在 mousedown 上动态创建一个形状,然后立即让它(形状)跟随鼠标光标,直到 mouseup 将其设置到位。 这是我到目前为止所拥有的,但它对我不起作用。

addNegativeButton.on('mousedown', function(){
     var userPos = stage.getUserPosition();
     shapesLayer.add(new Kinetic.Image({
          image: imageObj,
          x: userPos.x,
          y: userPos.y,
          height: 25,
          width: 25,
          rotation: 1,                      
          draggable: true,
          offset: 12
}));

var last = shapesLayer.getChildren()[shapesLayer.getChildren().length -1];
stage.on("mouseup", function(){
    last.setAbsolutePosition(stage.getUserPosition());  
    stage.off("mouseup");
});

重申一下: 我有一个“addNegativeButton”,单击它会创建一个形状。 但我希望它跟随鼠标光标,直到释放单击。

http://jsfiddle.net/CPrEe/37/ 有什么建议吗?

最佳答案

事实证明它相当简单;)
添加元素/形状后,您所要做的就是使用模拟功能来模拟鼠标按下并拖动......

var stage = new Kinetic.Stage({
    container: 'container',
    width: 578,
    height: 200
});

var layer = new Kinetic.Layer();

var rect = new Kinetic.Rect({
    x: 20,
    y: 20,
    width: 100,
    height: 50,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 4
});
//What I really want here is to start dragging the circle until the 
//user releases the mouse, which would then place the circle.
rect.on('mousedown', function(evt) {

    var userPos = stage.getUserPosition();
    var latestElement = new Kinetic.Circle({
        x: userPos.x,
        y: userPos.y,
        radius: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4,
        draggable: true
    })

    layer.add(latestElement);

    // Here's the bit you want.  After adding the circle (with draggable:true) simulate the mousedown event on it which will initiate the drag
    latestElement.simulate('mousedown');

    layer.draw();
});

layer.add(rect);

stage.add(layer);​

http://jsfiddle.net/CPrEe/38/
http://kineticjs.com/docs/symbols/Kinetic.Node.php#simulate

关于javascript - KineticJS/Javascript动态创建和即时拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13897131/

相关文章:

javascript - 哪些事件处理程序附加到 DOM 节点 - 如何查找?

javascript - 将图像从 cordova imagepicker 转换为 base64

javascript - 为什么 offsetParent 忽略我的文章标签?

css - Div 布局随着移动浏览器的变化而变化

javascript - JQuery .removeAttr ('required' ) 不适用于输入单选组

javascript - 如何正确处理 JavaScript 事件中的关闭?

javascript - 使用 keydown() 仅发送一个 Ajax post 请求 - jQuery

javascript - 单击子元素时如何将类添加到父元素

javascript - 如何获取systemuser实体的值?

javascript - 无法使用 form2js.js 将表单数据转换为 JSON 对象