d3强制布局在拖动时捕捉到网格

时间:2015-08-03 01:07:36

标签: javascript d3.js snapping

在d3中完成拖动后如何捕捉到网格?

我尝试向dragend事件添加一个事件监听器并对值进行四舍五入,但它似乎不起作用:

force.drag()
    .on('dragend', function(d) {
        d.fixed = false;
        d.x = Math.round(d.x / 10) * 10;
        d.y = Math.round(d.y / 10) * 10;
    });

http://jsfiddle.net/zc89zj9e/

1 个答案:

答案 0 :(得分:3)

你的jsfiddle有2个问题。首先,节点在被拖动后变得不可分割 - 这是因为您在dragend上设置了d.fixed = false。这可以防止进一步的位置变化。解决方案是在dragstart上设置d.x

其次,除了d.yd.px之外,您需要设置d.pyfunction dragstarted(d) { // ... d.fixed = false; } function dragended(d) { d.fixed = true; d.x = d.px = Math.round(d.x / 100) * 100; d.y = d.py = Math.round(d.y / 100) * 100; } (强制布局在内部使用)以使位置更改生效

public IWebElement WaitForElement(IWebDriver driver, string value)
{
    By xPath = By.XPath("//input[@type='" + value + "']");
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    //Explicit wait should be enough in this case and do a DOM polling 
    //regularly to see if the element exist and until it finds it for 
    //most 10 second
    IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
    {
        return d.FindElement(xPath);
    });

    return myDynamicElement;
}

完整演示here