20 lines
		
	
	
		
			703 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			20 lines
		
	
	
		
			703 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| function decay({ velocity = 0, from = 0, power = 0.8, timeConstant = 350, restDelta = 0.5, modifyTarget, }) {
 | |
|     const state = { done: false, value: from };
 | |
|     let amplitude = power * velocity;
 | |
|     const ideal = from + amplitude;
 | |
|     const target = modifyTarget === undefined ? ideal : modifyTarget(ideal);
 | |
|     if (target !== ideal)
 | |
|         amplitude = target - from;
 | |
|     return {
 | |
|         next: (t) => {
 | |
|             const delta = -amplitude * Math.exp(-t / timeConstant);
 | |
|             state.done = !(delta > restDelta || delta < -restDelta);
 | |
|             state.value = state.done ? target : target + delta;
 | |
|             return state;
 | |
|         },
 | |
|         flipTarget: () => { },
 | |
|     };
 | |
| }
 | |
| 
 | |
| export { decay };
 | 
