【问题标题】:How to properly change the vertices position programmatically for a link in JointJS如何以编程方式正确更改 JointJS 中链接的顶点位置
【发布时间】:2015-10-01 09:52:23
【问题描述】:

我正在使用以下代码更改链接的顶点位置:

var vertices = link.get('vertices');
for(var j=0;j < vertices.length; j++){
   var vertex = vertices[j];
   vertex.x += differenceX;
   vertex.y += differenceY;
}

然而,即使链接顶点似乎被很好地翻译了,当将鼠标悬停在链接上时,链接工具会出现在之前的位置,如下面的屏幕截图所示:

我已经尝试了不同的方法,包括调用

  • paper.render()
  • linkview.update()

不幸的是,它们似乎都不起作用......

【问题讨论】:

    标签: css jointjs


    【解决方案1】:

    您应该使用Backbone.Model.prototype.set 来更新属性,而不是直接修改它们。否则不会在模型上触发 change 事件,并且视图不会自行更新。

    var oldVertices = link.get('vertices');
    var newVertices = [];
    
    for(var j=0; j < oldVertices.length; j++){
       var oldVertex = oldVertices[j];
       // Create a new object, so you are not modifying
       // the previous model's attributes.
       // Backbone Model would not trigger a change event if the previous
       // and the new value are `deep` equal.
       var newVertex = { x: oldVertex.x, y: oldVertex.y }
       newVertex.x += differenceX;
       newVertex.y += differenceY;
       newVertices.push(newVertex);
    }
    
    link.set('vertices', newVertices);
    

    【讨论】:

    • 酷它几乎适用于这个新版本。顶点在新位置正确显示,但是箭头稍微偏离了位置......有什么想法为什么会发生这种情况?
    • mmm 我不确定我能否轻松做到这一点,因为应用程序有很多依赖项。我会看看我能做什么,然后告诉你;)
    • 感谢@Roman,它运行良好。使用 ES6,循环可以简单地是:for(let v of oldVertices){ newVertices.push(g.point(v.x + diffX, v.y + diffY)) }
    猜你喜欢
    • 2011-06-23
    • 1970-01-01
    • 2010-12-04
    • 2019-07-16
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多