【问题标题】:Snap.svg determine drag distance along a pathSnap.svg 确定沿路径的拖动距离
【发布时间】:2014-07-11 19:34:10
【问题描述】:

引用 here 并更新为与 Snap.svg here 一起使用,我想更好地了解所提供的 gradSearch 函数的实际工作原理(这有点超出我的想象),以及是否有任何好的替代方案采用这种方法?

gradSearch = function (l0, pt) {
    l0 = l0 + totLen;
    var l1 = l0,
        dist0 = dist(path.getPointAtLength(l0 % totLen), pt),
        dist1,
        searchDir;

    if (dist(path.getPointAtLength((l0 - searchDl) % totLen), pt) > 
       dist(path.getPointAtLength((l0 + searchDl) % totLen), pt)) {
        searchDir = searchDl;
    } else {
        searchDir = -searchDl;
    }

    l1 += searchDir;
    dist1 = dist(path.getPointAtLength(l1 % totLen), pt);
    while (dist1 < dist0) {
        dist0 = dist1;
        l1 += searchDir;
        dist1 = dist(path.getPointAtLength(l1 % totLen), pt);
    }
    l1 -= searchDir;
    return (l1 % totLen);
}

【问题讨论】:

  • 是的..这是东西,我也在寻找。该函数适用于 SVG 中的任何给定路径。我也想重用它,但是解释起来很困难。

标签: javascript html svg raphael snap.svg


【解决方案1】:

我花了一些时间来理解代码,但这就是我理解它的工作方式(试图简化解释):

首先将可拖动对象在路径上的位置记录为l0(注意这里是L0,第一次看代码容易和数字10混淆)。

从新点(鼠标拖到的位置)到路径上的位置的距离记录为dist0

if 语句然后确定要拖动的方向。它通过将searchDl 值添加到路径位置(即沿路径的长度)并减去相同的值并查看哪个更接近鼠标来实现位置。

一旦它知道要向哪个方向移动,它就会使用while 循环以searchDl 的大小不断添加/减去位置,直到路径上的点与鼠标位置之间的距离尽可能小尽其所能。

然后它返回路径上的新位置。

通过将searchDir 更改为更大的值,您可以以更大的增量移动,它可以以牺牲精度为代价更快地计算(如果我正确理解了代码)。

【讨论】:

    猜你喜欢
    • 2015-01-20
    • 2014-07-09
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多