简体   繁体   English

如何在Paper.JS中手动调度事件?

[英]How to dispatch an event manually in Paper.JS?

What I am trying to achieve is to clone an item on the canvas when I click on it and drag the clone without releasing the mouse. 我要实现的目标是单击时在画布上克隆一个项目,然后拖动克隆而不释放鼠标。

             menuItem.onMouseDown = function(event){
                var clone = this.clone();
                clone.onMouseDrag = function(event){
                    this.position+=event.delta;
                    console.log(event);
                }
                var ev = new MouseEvent('mousedrag',
                            event.event);
                ev.event.type="mousemove";
                ev.delta={x:0,y:0};
                ev.target=clone;
                ev.point=event.point;
                clone.emit('mousedrag',ev);
            };

I tried this, I believe I need something like this. 我尝试过,我相信我需要这样的东西。 So when I click the menuItem I clone it, and set up the event for the clone, and then emit an event on it. 因此,当我单击menuItem时,将其克隆,并为克隆设置事件,然后在其上emit事件。 But the emitted event needs to be set up, and that is where my idea falls apart. 但是需要设置发出的事件,这就是我的主意破裂的地方。 Any thoughts on this one? 对这个有什么想法吗?

I would do things a bit differently; 我会做些不同的事情。 I wouldn't try to swap the handlers in the middle of the selection/dragging but would just logically swap the items temporarily: 我不会尝试在选择/拖动过程中交换处理程序,而只是在逻辑上临时交换项目:

menuItem = new Path.Circle(view.bounds.center, 25);
menuItem.fillColor = 'red';

var oldMenuItem = null;

menuItem.onMouseDown = function(e) {
    oldMenuItem = this.clone();
    oldMenuItem.fillColor = 'green';
}

menuItem.onMouseDrag = function(e) {
    this.position += e.delta;
}

menuItem.onMouseUp = function(e) {
    // swap menuItem and oldMenuItem to keep mouse handling on the
    // original item?
    var t = this.position;
    this.position = oldMenuItem.position;
    oldMenuItem.position = t;
}

Here's a sketch that implements something similar (I think) to what you're looking for. 这是一个草图 ,实现了与您正在寻找的东西类似的功能(我认为)。 Just pretend the red circle is the menu item. 只是假装红色圆圈是菜单项。

It is possible to construct a ToolEvent or MouseEvent (depending on the handler) but that is currently undocumented and would require a trip to the source code on github. 可以构造一个ToolEvent或MouseEvent(取决于处理程序),但是目前尚无记录,因此可能需要访问github上的源代码。 Edit: I got curious and took a trip to github. 编辑:我很好奇,并去了github。

The MouseEvent constructor code is in the events directory but the constructor is used and the .emit function is called from the CanvasView.js module (search for new MouseEvent ). MouseEvent构造函数代码位于events目录中,但使用构造函数,并从CanvasView.js模块调用.emit函数(搜索new MouseEvent )。 But in order to simulate what you want to you'll need to simulate the entire chain of events in order to keep the internal state consistent. 但是,为了模拟您想要的内容,您需要模拟整个事件链,以保持内部状态的一致性。 So you'd need to 1) emit a mouseup event on the original item, 2) emit a mousedown on the new item, and 3) emit mousemoves on the new item, and 3) detach the handler from the original item and attach a handler to the new item in between 1 & 2. (You might not need to emit the original mouseup if you detach the handler before.) If you've created a Tool you will need to create a ToolEvent rather than a MouseEvent . 因此,您需要1)在原始项目上发出mouseup事件,2)在新项目上发出mousedown,3)在新项目上发出mousemoves,3)从原始项目分离处理程序并附加一个1至2之间的新项的处理程序。(如果之前分离处理程序,则可能不需要发出原始的mouseup。)如果创建了Tool ,则需要创建ToolEvent而不是MouseEvent

Anyway, I can see why it's not documented - there is a lot to keep in mind. 无论如何,我可以看到为什么未记录在案-有很多需要牢记的地方。 I wanted to update this answer to reflect your original question even though the answer is still that it is probably best to find another way to perform this action. 我想更新此答案以反映您的原始问题,即使答案仍然是最好找到另一种方法来执行此操作。

In case someone really wants or needs to do this: 如果有人真的想要或需要这样做:

  1. Mouse events are constructed via the code in MouseEvent.js 鼠标事件是通过MouseEvent.js中的代码构造的
  2. The constructor is used, and the event emitted, in CanvasView.js CanvasView.js中使用构造函数并发出事件
  3. Event processing is driven in View.js 事件处理在View.js中驱动
  4. Tool events are constructed via code in ToolEvent.js 工具事件是通过ToolEvent.js中的代码构造的

OTOH, emitting a frame event is easy: OTOH,发出帧事件很容易:

view.emit('frame', {});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM