【问题标题】:How to pass an additional parameter to graffle.js如何将附加参数传递给 graffle.js
【发布时间】:2015-03-03 14:08:04
【问题描述】:

我正在尝试将一个附加参数传递给 graffle 的连接函数,以便我可以根据该新参数在行尾绘制带/不带箭头的连接。

要建立联系,我通常会这样做:

connections.push(paper.connection(obj1, obj2, "#000"));

现在我想以这种方式添加这个新参数:

connections.push(paper.connection(condCol, ret, "#000",true));

但参数始终是undefined

这就是我改变连接功能的方式:

Raphael.fn.connection = function (obj1, obj2, line, bg, addArrow) {

  //...

  if (line && line.line) {
      //...
    } else {
        // based on the 'addArrow' parameter I can figure out 
        // whether I should put an arrow at the end of the line or not
        var settings;
        if (addArrow) {
            //If addArrow was true, draw a arrow at the end of the line
            settings = { "stroke": color, fill: "none", 'stroke-width': 2, 'arrow-end': 'block-midium-midium' };
        }
        else {
             //If addArrow was false, just draw a line
            settings = { "stroke": color, fill: "none", 'stroke-width': 2 };
        }
        return {
            bg: bg && bg.split && this.path(path).attr({ stroke: bg.split("|")[0], fill: "none", "stroke-width": bg.split("|")[1] || 3 }),
            line: this.path(path).attr(settings), //use the settings
            from: obj1,
            to: obj2
        };
    }

}

你知道为什么这不起作用吗??

提前致谢。

【问题讨论】:

    标签: javascript svg raphael


    【解决方案1】:

    您的新connection() 函数采用五个参数。你只通过了四次。这就是 addArray 未定义的原因。您传递的true 值将进入bg 参数。尝试这样调用:

    connections.push(paper.connection(condCol, ret, "#000", null, true));
    

    【讨论】:

    • bg参数传递null或空字符串有什么区别?
    • 在这种情况下,这并不重要,因为代码测试 b 是否存在的方式。 JS 中的空字符串在转换为布尔值时被视为 false。空值也是如此。但其他代码可能正在以另一种方式进行测试。
    【解决方案2】:

    抱歉,我没有注意到这些是可选参数。 解决方案很简单。 我只是像这样更改方法的定义:

    Raphael.fn.connection = function (obj1, obj2, line, bg, addArrow) {
    
    }
    

    并调用函数,省略第四个参数:

    connections.push(paper.connection(condCol, ret, "#000","",true));
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多