【发布时间】:2015-09-18 16:52:54
【问题描述】:
我有一个函数可以将相机移动到用户在画布上单击的位置。但目前这种运动是线性的。我希望它使用缓入/缓出过渡,但我很难理解如何使用我当前的功能来实现它。
这是我的代码:
animate.mouseEvent = function(e,el){
var mousePos = mouse.relativePosition(e,el);
var parent = el;
if(e.button == 0){
function update(){
if(centerX == mousePos.x && centerY == mousePos.y){
clearInterval(parent.timer);
//center X / Y is canvas width & height halved
}
var difx = centerX - mousePos.x,
dify = centerY - mousePos.y,
distance = Math.sqrt(difx*difx + dify*dify),
normalX = difx/distance,
normalY = dify/distance,
speed = 1, //currently linear [replace with ease in/out]
x = normalX * speed,
y = normalY * speed;
updateOffsets(x,y);
mousePos.x += x;
mousePos.y += y;
}
parent.timer = setInterval(update,1);
}
}
animate.updateOffsets = function(x,y){
canvas.offsetX -= x;
canvas.offsetY -= y;
}
所以我想知道如何从我当前的线性方法中实现缓入/缓出。使用以下函数:
Math.easeInOutQuad = function (t, b, c, d) {
t /= d/2;
if (t < 1) return c/2*t*t + b;
t--;
return -c/2 * (t*(t-2) - 1) + b;
};
我不完全知道如何实现它,也不知道它究竟会返回什么来计算新的偏移量。希望有人能解释一下。
【问题讨论】:
标签: javascript