從你的描述,在任何給定幀有6個狀態的:
- 電流線性插值
- 線性插值的時間跨度
- 電流方向
- 當前時間
- Start值的開始時間
- 結束值
從這些可以計算出所需的進步價值,說:
function progressValue(startTime, lerpSpanSeconds, dir,
currentTime X, Y, dir, currentTime) {
// lerp
return 0.42;
}
對於requestAnimationFrame,you need a simple callback to pass in。也就是說,函數必須知道它需要的所有東西,除了它在運行時可以獲得的東西。在這裏,它運行時只需要獲得當前時間,並從那裏開始工作。
function animableThing() {
var startTime = 7;
var lerpSpanSeconds = 3;
var dir = +1;
var X = 0;
var Y = 1;
var currentTime = GetCurrentUnicornTime();
var thingToAnimate = document.getElementById('myAnimableElement');
var progress = progressValue(startTime, lerpSpanSeconds, dir,
currentTime, X, Y, dir, currentTime);
// reverse when we hit the end
if(progress > Y) {
dir *= -1;
startTime = currentTime;
progress = Y;
}
DrawAnimationThing(thingToAnimate, progress);
// continue the animation
window.requestAnimationFrame(animableThing);
}
但是有一個問題;如果您希望能夠使用腳本中的值或來自屏幕的輸入或關於屏幕上元素的最新信息來設置動畫,那麼當您需要時,您需要能夠使animableThing
回調更清晰有新的價值。不料,母親:
function MotherOfAnimableThings(startTime, lerpSpanSeconds, dir, X, Y,
thingToAnimate)
{
// Passed in variables have scope outside the animableThing, these
// will be private to the animableThing function.
// Consider defaulting or validation here
// Construct a new function freshly each time the Mother is called,
// and return it to the caller. Note that we assign a variable here
// so that we can re-call RequestAnimationFrame to continue the loop
var callback = (function() {
var currentTime = GetCurrentUnicornTime();
var progress = progressValue(startTime, lerpSpanSeconds, dir,
currentTime, X, Y, dir, currentTime);
// reverse when we hit the end
if(progress > Y) {
dir *= -1;
startTime = currentTime;
progress = Y;
}
DrawAnimationThing(thingToAnimate, progress);
window.requestAnimationFrame(callback);
});
return callback;
}
我們可以走的更遠,並通過讓呼叫者使這個普通的其他類型的東西傳遞一個progressValue函數來調用,或事實上的回調,這樣你就可以採取任何元素,繪製函數和設置函數並創建一個動畫,但這是一個合理的起點。
有了上面的內容,我們只需要調用Mother來創建一個animableThing
函數並調用RequestAnimationFrame。從那以後,它在內部調用RequestAnimationFrame來繼續循環。
現在,已經做到這一點,你會希望把它停下來,所以加在它可以檢查回調變量,所以,你可以做
var animableThing = MotherOfAnimableThings(...);
window.requestAnimationFrame(animableThing);
// ... later
animableThing.stop = true; // it should stop on the next frame
您是否嘗試過的東西?你卡在哪裏? –
詳細說明@AramilRey提出的問題;你有什麼嘗試?你做了什麼研究?你能提供片段來證明你實際上試圖自己解決問題嗎? (一個簡單的谷歌搜索將爲你提供解決這個問題所需的知識。)你不應該依靠SO社區爲你做你的工作。 –
http://jsbin.com/picuwe/2/edit?js,output – punkbit