【问题标题】:How to add click event on rectangle in Graphiti.js如何在 Graphiti.js 中的矩形上添加点击事件
【发布时间】:2012-07-09 10:10:31
【问题描述】:

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

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);   

但不会工作。请让我知道吗?

【问题讨论】:

    标签: javascript graphiti-js


    【解决方案1】:

    像这样创建自己的继承自 Rectangle 的形状。 乍一看,这看起来有点不舒服,但通常你会写 带有大量自定义代码的您自己的形状。在这种情况下,创建一个新类是一次性的。

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

    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));
    }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2015-04-06
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多