首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Graphiti.js中添加矩形上的单击事件

如何在Graphiti.js中添加矩形上的单击事件
EN

Stack Overflow用户
提问于 2012-07-09 18:10:32
回答 1查看 176关注 0票数 0

我想在graphiti中注册矩形上的单击事件。我试过了

代码语言:javascript
复制
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);   

但行不通。请告诉我这件事好吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-09 18:15:38

创建你自己从矩形继承的形状,如下所示。这一开始看起来有点不舒服,但通常你会用很多自定义代码来编写自己的形状。在这种情况下,创建一个新类是一次性的工作。

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

代码语言:javascript
复制
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));
}

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11392956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档