Javascript JS-将二维向量转化为曲线?
我在画布上有两个点(x/y),可以使用requestanimationframe或setinterval在它们之间直线移动图像 然而,我却想,不知怎的?!,在弯曲动画中移动对象,具体取决于速度x/y和s(步长)的矢量组合 我创建了这个JSBin: -(单击init) 有没有可能把这个“向量”变成某种曲线来画出平滑的运动? 如果不是,我需要什么其他值来将其转化为曲线运动Javascript JS-将二维向量转化为曲线?,javascript,vector,curve,Javascript,Vector,Curve,我在画布上有两个点(x/y),可以使用requestanimationframe或setinterval在它们之间直线移动图像 然而,我却想,不知怎的?!,在弯曲动画中移动对象,具体取决于速度x/y和s(步长)的矢量组合 我创建了这个JSBin: -(单击init) 有没有可能把这个“向量”变成某种曲线来画出平滑的运动? 如果不是,我需要什么其他值来将其转化为曲线运动 //start at 50,50 //move to 150,125 // Vector is 100/75 or v
//start at 50,50
//move to 150,125
// Vector is 100/75 or v +1/+0.75 at 100 steps
function Move(ox, oy, x, y){
this.ox = ox;
this.oy = oy;
this.x = x
this.y = y
this.p;
this.v = {x: x - ox, y: y - oy};
this.setup = function(){
var p = {};
var v = this.v;
if (this.x > this.y){
p.s = v.y;
p.y = 1;
p.x = v.x/v.y;
}
else {
p.s = v.x;
p.x = 1;
p.y = v.y/v.x;
}
this.p = p;
}
this.setup();
}
function Ship(x, y){
this.x = x;
this.y = y;
this.moves = [];
this.draw = function(){
this.drawSelf();
this.drawTarget();
}
this.create = function(){
var move = new Move(this.x, this.y, 150, 125);
this.moves.push(move);
}
this.update = function(){
var m = this.moves[0];
var self = this;
anim = setInterval(function(){
ctx.clearRect(0, 0, res, res);
self.x += m.p.x;
self.y += m.p.y;
m.p.s--;
self.draw();
if (m.p.s == 0){
clearInterval(anim);
}
}, 30);
}
this.create();
this.draw();
this.update();
}
function init(){
var ship = new Ship(50, 50);
}
关于这个主题有一些有用的信息 基本上,您需要一个或多个控制点来将向量“弯曲”为曲线 对于单个控制点,可通过以下公式实现:
[x,y]=(1-t)²P0+2(1-t)tP1+t2²P2
当
t=0
时,右侧等于第一个控制点–线段的起点。当t=1
时,我们得到点P1、第二个控制点和线段末端。您需要3个或更多点来定义曲线。谷歌“样条曲线”。如果你有一个速度,你可以用它来定义第三个点,从起始点向初始速度方向向前投射。