如何在Graphiti.js中的矩形上添加click事件

时间:2012-07-09 10:10:32

标签: javascript graphiti-js

我想在graphiti中的矩形上注册click事件。我试过这个

var innerrect = new graphiti.shape.basic.Rectangle(100, 20);
        innerrect.onClick = function(){
            alert("Hi");
        }
rect.addFigure(innerrect , new graphiti.layout.locator.BottomLocator(rect));
canvas.addFigure(rect, 100, 100);   

但不会工作。请跟我说清楚吗?

1 个答案:

答案 0 :(得分:0)

像这样创建自己继承自Rectangle的形状。 这看起来有点不舒服的第一时刻,但你写的是正常的 你自己的形状有很多自定义代码。在这种情况下,为新课程创作是一次性的努力。

请记住,这些示例是一个很好的起点。还有一个带有click事件监听器的示例。

MyFigure = graphiti.shape.basic.Rectangle.extend({

NAME : "MyFigure",

init : function()
{
    this._super();

    this.createPort("output");
    this.setDimension(30,30);
    this.setResizeable(false);

    this.value=false;

    this.colors={};
    this.colors[true]="#00f000";
    this.colors[false]="#f00000";

    this.setBackgroundColor(this.colors[this.value]);
},

/**
 * @method
 * Change the color and the internal value of the figure.
 * Post the new value to related input ports.
 * 
 */
onClick: function(){
    this.value = !this.value;
    this.setBackgroundColor(this.colors[this.value]);

    var connections = this.getOutputPort(0).getConnections();
    connections.each($.proxy(function(i, conn){
        var targetPort = conn.getTarget();
        targetPort.setValue(this.value);
        conn.setColor(this.getBackgroundColor());
    },this));
}

});
相关问题