简体   繁体   English

从ZPD插件将Raphael对象存储在变量中

[英]Store Raphael object in variable from ZPD plugin

I have previously worked with Raphael, but I have returned to my project and decided to totally re-write it to improve performance and general code quality. 我以前曾与Raphael一起工作过,但是我返回了我的项目并决定完全重写它以提高性能和一般代码质量。 Anyway, the application uses Raphael to create Mind Maps (If you have read my previous questions you will have more of an idea of the overall application). 无论如何,该应用程序都使用Raphael来创建思维导图(如果您已经阅读了之前的问题,那么您将对整个应用程序有更多的了解)。

So I create nodes on the canvas using paper.circle() etc and to avoid the unnecessary work of me using .drag() I decided to use the Raphael ZPD plugin . 因此,我使用paper.circle()等在画布上创建节点,并且为了避免使用.drag()进行不必要的工作,我决定使用Raphael ZPD插件 The plugin is far too complicated for me to fully understand, but I intend to edit it very slightly. 该插件太复杂了,我无法完全理解,但是我打算对其进行非常小的编辑。 I would like to store the active node in a variable _node. 我想将活动节点存储在变量_node中。 To do this I thought it would be as simple as getting the evt.target and assigning it, but apparently it's not. 为此,我认为这就像获取evt.target并分配它一样简单,但显然并非如此。

Here is part of the plugin (The bit where I think the assignment should go): 这是插件的一部分(我认为应该去的地方):

/**
 * Handle mouse button release event.
 */
me.handleMouseUp = function(evt) {
    if (evt.preventDefault)
        evt.preventDefault();

    evt.returnValue = false;

    var svgDoc = evt.target.ownerDocument;

    if ((me.state == 'pan' && me.opts.pan) || (me.state == 'move' && me.opts.drag)) {
        // Quit pan mode
        me.state = '';
    }
};

When using alert(evt.target); 当使用alert(evt.target); (when clicking a circle) I am given: (单击圆圈时)会显示:

[object SVGCircleElement] [对象SVGCircleElement]

which implies that evt.target is getting the individual element (as it changes for other shapes) but then trying to manipulate _node after assignment fails. 这意味着evt.target正在获取单个元素(随着其他形状的变化),但是在分配失败后尝试操作_node I get an error message of: 我收到以下错误消息:

_node.attr is not a function _node.attr不是函数

(I attempted to change the attr() as this was the simplest function). (我试图更改attr(),因为这是最简单的函数)。

I have tried many variations using node, evt.target, evt.currentTarget etc, but I'm sure I am just missing something stupid with evt.target because it produces the correct output on alert. 我已经尝试使用node,evt.target,evt.currentTarget等进行了许多变体,但是我确定我只是缺少evt.target一些愚蠢之处,因为它会在警报时产生正确的输出。

I understand that me. 我了解me. is defined elsewhere in the code so please view the link above if you need to (seems better than posting the full plugin here). 是在代码的其他地方定义的,因此如果需要,请查看上面的链接(似乎比在此处发布完整的插件更好)。

I'd appreciate any help that you can give. 我会很感激您能提供的任何帮助。

Thank you. 谢谢。

UPDATE: I'm now 100% certain that it is evt.target that I should be storing in _node but it would seem that there is no direct link between SVG, DOM and Raphael. 更新:我现在100%确定应该将evt.target存储在_node但是在SVG,DOM和Raphael之间似乎没有直接链接。 Any ideas on how I could link the SVG element back to Raphael? 关于如何将SVG元素链接回Raphael的任何想法?

I started using this plugin last week, and have run into the same issue. 我上周开始使用此插件,并且遇到了同样的问题。

I'm able to link the SVG shape back to the raphael handle to the shape. 我能够将SVG形状链接回该形状的拉斐尔手柄。

Basically, you first have to maintain a pointer to the raphael paper object that's tied to the zpd plugin object. 基本上,您首先必须维护一个指向与zpd插件对象绑定的raphael纸对象的指针。 I do this by creating a raphPaper declaration after the var opts = {...} line 我通过在var opts = {...}行之后创建raphPaper声明来完成此操作

var raphPaper = null;

So that raphPaper object will be available like the opts object is. 这样raphPaper对象将像opts对象一样可用。

When raphael creates a shape, it assigns two variables (that I've found): .raphael and .raphaelid .raphael is a boolean telling us whether or not the SVG shape was created by raphael, and .raphaelid gives us the raphael internal id for the SVG shape. 当raphael创建形状时,它会分配两个变量(我已经找到):.raphael和.raphaelid .raphael是一个布尔值,告诉我们SVG形状是否由raphael创建,而.raphaelid为我们提供了raphael内部ID用于SVG形状。 So, to get the raphael element, we just use getById on our paper object (raphPaper). 因此,要获取raphael元素,我们只需在纸张对象(raphPaper)上使用getById。

if(evt.target.raphael){
    var raphShp = raphPaper.getById(evt.target.raphaelid);
}

So for your problem, you just have to assign raphShp to _node or whatever variable you want to access. 因此,对于您的问题,只需将raphShp分配给_node或您要访问的任何变量。

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

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