我编写了一个函数来计算 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);
}