【问题标题】:how to draw curve through N points using javascript HTML5 canvas?如何使用javascript HTML5画布通过N个点绘制曲线?
【发布时间】:2013-08-06 17:17:13
【问题描述】:

对于绘图应用程序,我将鼠标移动坐标保存到一个数组中,然后使用 lineTo 绘制它们。结果线不平滑。如何在所有收集的点之间生成一条曲线?

我用谷歌搜索了,但我只找到了 3 个用于绘制线条的函数:对于 2 个样本点,只需使用 lineTo。 3个样本点quadraticCurveTo,4个样本点bezierCurveTo。

(我尝试为数组中的每 4 个点绘制一个 bezierCurveTo,但这会导致每 4 个采样点发生扭结,而不是连续平滑曲线。)

如何编写一个函数来绘制具有 5 个及以上样本点的平滑曲线?

【问题讨论】:

  • 肯有一个不错的答案;如果您想了解有关具有此属性的曲线(通过点的样条)的更多信息,您可以查找术语“有限差分曲线”、“基数样条”、“Catmull-Rom 样条”和“Kochanek–Bartels 样条”。

标签: javascript html canvas bezier spline


【解决方案1】:

您可以使用基数样条来执行此操作:

这个函数是这样的,点数组排序为[x1, y1, x2, y2, ... xn, yn],[0.0, 1.0] 之间的张力和可选的段数,它决定了每个点之间的分辨率。

Here's an online demo of this in action

UPDATE 发布了我的主要实现的错误版本,这是正确的 -

结果将是一个新数组,其中包含您迭代的平滑线 -

function getCurvePoints(ptsa, tension, numOfSegments) {

    // use input value if provided, or use a default value   
    tension         =   (tension != 'undefined') ? tension : 0.5;
    numOfSegments   =   numOfSegments   ? numOfSegments : 16;

    var _pts = [], res = [],    // clone array
        x, y,                   // our x,y coords
        t1x, t2x, t1y, t2y,     // tension vectors
        c1, c2, c3, c4,         // cardinal points
        st, t, i;               // steps based on num. of segments

    // clone array so we don't change the original
    _pts = ptsa.slice(0);

    _pts.unshift(pts[1]);           //copy 1. point and insert at beginning
    _pts.unshift(pts[0]);
    _pts.push(pts[pts.length - 2]); //copy last point and append
    _pts.push(pts[pts.length - 1]);

    // ok, lets start..

    // 1. loop goes through point array
    // 2. loop goes through each segment between the two points + one point before and after
    for (i=2; i < (_pts.length - 4); i+=2) {

        // calc tension vectors
        t1x = (_pts[i+2] - _pts[i-2]) * tension;
        t2x = (_pts[i+4] - _pts[i]) * tension;

        t1y = (_pts[i+3] - _pts[i-1]) * tension;
        t2y = (_pts[i+5] - _pts[i+1]) * tension;

        for (t=0; t <= numOfSegments; t++) {

            // calc step
            st = t / numOfSegments;

            // calc cardinals
            c1 =   2 * Math.pow(st, 3)  - 3 * Math.pow(st, 2) + 1; 
            c2 = -(2 * Math.pow(st, 3)) + 3 * Math.pow(st, 2); 
            c3 =       Math.pow(st, 3)  - 2 * Math.pow(st, 2) + st; 
            c4 =       Math.pow(st, 3)  -     Math.pow(st, 2);

            // calc x and y cords with common control vectors
            x = c1 * _pts[i]    + c2 * _pts[i+2] + c3 * t1x + c4 * t2x;
            y = c1 * _pts[i+1]  + c2 * _pts[i+3] + c3 * t1y + c4 * t2y;

            //store points in array
            res.push(x);
            res.push(y);

        }
    }

    return res;
}

【讨论】:

  • 不需要“段数”参数,样条曲线是一条连续曲线,并且以低复杂度运行点生成只是纯粹的数字运算。 JS 在这方面表现不错。
  • @Mike'Pomax'Kamermans 您需要知道要在每个初始点之间绘制多少点,因此您需要一个分段值。
  • 不一定。而不是“多少段”,根据样条切线更容易获得下一个坐标。强切线意味着你的下一个点应该是一个小的间隔,弱切线意味着你可以使用更大的间隔。与从需要 N 段开始,然后使用固定间隔轮询来生成它们相比,这会生成更好的曲线近似
猜你喜欢
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多