A Raf based timer. This class allows you to run RAF for only a set duration and then stop.
This can be useful to do things like one time animations.
Here is an example of running the rafTimer for 300ms and lerping the x value of the box.
// Box x starting value.box.x = 0;let rafTimer = new RafTimer((progress)=> { // Called on each raf cycle from when start is called // for 300ms. // progress is passed as a value between 0 and 1. // console.log('progress') // Will move the box from 0 (starting) to 500 // based on the progress. box.x = mathf.lerp(box.x, 500, progress); // or go fancy and add easing to the progress! // box.x = mathf.ease(box.x, 500, progress, ease.easeInOutSine);})rafTimer.onComplete(()=> { // Do something on completion.})// Set the duration of the rafTimer 300 and start it.rafTimer.setDuration(300);rafTimer.play();// If you want, slow down the FPS.rafTimer.setFps(10);// Pause if you want.window.setTimeout(()=> { rafTimer.pause(); // Start playing again from pause point playButton.addEventListner('click', ()=> { rafTimer.play(); }, { once: true}); // Reset to beginning resetButton.addEventListner('click', ()=> { rafTimer.reset(); rafTimer.play(); }, { once: true});})TODO (uxder); Update this to extend emit emitter. Copy
// Box x starting value.box.x = 0;let rafTimer = new RafTimer((progress)=> { // Called on each raf cycle from when start is called // for 300ms. // progress is passed as a value between 0 and 1. // console.log('progress') // Will move the box from 0 (starting) to 500 // based on the progress. box.x = mathf.lerp(box.x, 500, progress); // or go fancy and add easing to the progress! // box.x = mathf.ease(box.x, 500, progress, ease.easeInOutSine);})rafTimer.onComplete(()=> { // Do something on completion.})// Set the duration of the rafTimer 300 and start it.rafTimer.setDuration(300);rafTimer.play();// If you want, slow down the FPS.rafTimer.setFps(10);// Pause if you want.window.setTimeout(()=> { rafTimer.pause(); // Start playing again from pause point playButton.addEventListner('click', ()=> { rafTimer.play(); }, { once: true}); // Reset to beginning resetButton.addEventListner('click', ()=> { rafTimer.reset(); rafTimer.play(); }, { once: true});})TODO (uxder); Update this to extend emit emitter.
Pauses the raf Timer.
Start playing the rafTimer.
Resets the RAF timer.
Set the duration of the raf loop in ms.
The duration in ms.
Sets the fps of raf loop..
A Raf based timer. This class allows you to run RAF for only a set duration and then stop.
This can be useful to do things like one time animations.
Here is an example of running the rafTimer for 300ms and lerping the x value of the box.