在javascript中渲染b样条动画的问题

Issue with rendering b-spline animation in javascript

本文关键字:样条 动画 问题 javascript      更新时间:2023-09-26

我编写了一个函数,用于计算b样条曲线的点,当使用内置的setinterval()函数生成点时,在屏幕上绘制曲线。问题是,当这些点被绘制到屏幕上时,只有一部分或一小段曲线沿着插值曲线移动是可见的,但整个曲线在绘制曲线时并不持久。除此之外,一旦曲线到达某个点,它就会从屏幕上拖下来。该代码的设计方式似乎是,一旦t的值达到1,函数将结束(即间隔将被清除),但由于某种原因,函数将继续被调用,导致曲线继续在屏幕外动画化。

相反,我有另一个函数,它只是使用for循环来绘制具有相同点的曲线,但使用该函数可以绘制一条小曲线,清晰地显示在屏幕上。以下是两个功能的代码:

       //function that draws the spline one time
       function bspline(context, points) {
       context.beginPath();
       for (var t = 0; t < 1; t += 0.1) {
           var ax = (-points[0].x + 3 * points[1].x - 3 * points[2].x + points[3].x) / 6;
           var ay = (-points[0].y + 3 * points[1].y - 3 * points[2].y + points[3].y) / 6;
           var bx = (-points[0].x - 2 * points[1].x + points[2].x) / 2;
           var by = (-points[0].y - 2 * points[1].y + points[2].y) / 2;
           var cx = (-points[0].x + points[2].x) / 2;
           var cy = (-points[0].y + points[2].y) / 2;
           var dx = (points[0].x + 4 * points[1].x + points[2].x) / 6;
           var dy = (points[0].y + 4 * points[1].y + points[2].y) / 6;
           context.moveTo(
               ax*Math.pow(t,3) + bx*Math.pow(t,2) + cx*t + dx,
               ay*Math.pow(t,3) + by*Math.pow(t,2) + cy*t + dy
               );
           context.lineTo(
               ax*Math.pow(t+0.1, 3) + bx*Math.pow(t+0.1, 2) + cx*(t+0.1) + dx,
               ay*Math.pow(t+0.1,3) + by*Math.pow(t+0.1,2)  +  cy*(t+0.1) + dy
               );
           //m.translate(ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx, ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy,0);
       }
       context.stroke();
   }

var interval;
   //sets the interval for the spline to be animated over
   function drawSpline(context, points, newpts) {
       interval = setInterval(splineAnim(context, points, newpts), 1600.67);
       console.log("interval set");
   }
   var t = 0;
   //determines and draws the points of the spline
   function splineAnim(context, points, newpts) {
       // Draw curve segment
       var ax = (-points[0].x + 3 * points[1].x - 3 * points[2].x + points[3].x) / 6;
       var ay = (-points[0].y + 3 * points[1].y - 3 * points[2].y + points[3].y) / 6;
       var bx = (-points[0].x - 2 * points[1].x + points[2].x) / 2;
       var by = (-points[0].y - 2 * points[1].y + points[2].y) / 2;
       var cx = (-points[0].x + points[2].x) / 2;
       var cy = (-points[0].y + points[2].y) / 2;
       var dx = (points[0].x + 4 * points[1].x + points[2].x) / 6;
       var dy = (points[0].y + 4 * points[1].y + points[2].y) / 6;
       context.beginPath();
       context.moveTo(
           ax * Math.pow(t, 3) + bx * Math.pow(t, 2) + cx * t + dx,
           ay * Math.pow(t, 3) + by * Math.pow(t, 2) + cy * t + dy
           );
       var ax2 = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
       var ay2 = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
       context.lineTo(
           ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx,
           ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy
           );
       context.stroke();
       //m.translate(ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx, ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy, 0);
       //console.log("ax2: " + ax2 + ", ay2: " + ay2);
       var arr = [ax2, ay2];
       newpts.push(arr);
       t += 0.02;
       //Reached end of curve
       if (t > 1) clearInterval(interval);
   }

不是Java脚本编码器,但在许多语言中3表示整数

所以尝试将.0添加到所有浮动常量。。。我敢打赌,您的编译器/解释器会检测到整数并截断更改结果的子结果,因此曲线不匹配,有时即使不匹配,也可能看起来高于1.0范围。

此外,更好的for停止是:

for (t=0.0,e=1;e;t=0.1)
 {
 if (t>=1.0) { e=0; t=1.0; }
 // here do your stuff
 }

通过这种方式,您可以使用任何步骤,而不仅仅是精确的范围划分。此外,这还考虑了步长和累积舍入误差的不精确数字表示。

只有几个提示

  • 对小整数指数使用pow是一个巨大的过度使用和性能杀手
  • 你为什么要计算两次点?你可以移动到一次,然后只需排队。。。或者记住最后的x,y位置而不是计算

所以当把所有东西放在一起时(C++)

int e;
float t,tt,ttt,dt;
vec2 a0,a1,a2,a3; // coefficients 2D vectors
vec2 p0,p1,p2,p3; // your input control points 2D vectors
vec2 p;           // temp point
a0=                           (    p0);
a1=                  (3.0*p1)-(3.0*p0);
a2=         (3.0*p2)-(6.0*p1)+(3.0*p0);
a3=(    p3)-(3.0*p2)+(3.0*p1)-(    p0);
t=0.0; dt=0.1; tt=t*t; ttt=tt*t;
p=a0+(a1*t)+(a2*tt)+(a3*ttt);
MoveTo(p.x,p.y);
for (t+=dt,e=1;e;t=dt)
 {
 if (t>=1.0) { e=0; t=1.0; }
 // here do your stuff
 tt=t*t;
 ttt=tt*t;
 p=a0+(a1*t)+(a2*tt)+(a3*ttt);
 LineTo(p.x,p.y);
 }
  • vec2只是包含x,y成员的2D向量