| 1 |
import { INPUT_CANCEL,COMPUTE_INTERVAL } from './input-consts'; |
| 2 |
import { abs } from '../utils/utils-consts'; |
| 3 |
import getVelocity from './get-velocity'; |
| 4 |
import getDirection from './get-direction'; |
| 5 |
|
| 6 |
/** |
| 7 |
* @private |
| 8 |
* velocity is calculated every x ms |
| 9 |
* @param {Object} session |
| 10 |
* @param {Object} input |
| 11 |
*/ |
| 12 |
export default function computeIntervalInputData(session, input) { |
| 13 |
let last = session.lastInterval || input; |
| 14 |
let deltaTime = input.timeStamp - last.timeStamp; |
| 15 |
let velocity; |
| 16 |
let velocityX; |
| 17 |
let velocityY; |
| 18 |
let direction; |
| 19 |
|
| 20 |
if (input.eventType !== INPUT_CANCEL && (deltaTime > COMPUTE_INTERVAL || last.velocity === undefined)) { |
| 21 |
let deltaX = input.deltaX - last.deltaX; |
| 22 |
let deltaY = input.deltaY - last.deltaY; |
| 23 |
|
| 24 |
let v = getVelocity(deltaTime, deltaX, deltaY); |
| 25 |
velocityX = v.x; |
| 26 |
velocityY = v.y; |
| 27 |
velocity = (abs(v.x) > abs(v.y)) ? v.x : v.y; |
| 28 |
direction = getDirection(deltaX, deltaY); |
| 29 |
|
| 30 |
session.lastInterval = input; |
| 31 |
} else { |
| 32 |
// use latest velocity info if it doesn't overtake a minimum period |
| 33 |
velocity = last.velocity; |
| 34 |
velocityX = last.velocityX; |
| 35 |
velocityY = last.velocityY; |
| 36 |
direction = last.direction; |
| 37 |
} |
| 38 |
|
| 39 |
input.velocity = velocity; |
| 40 |
input.velocityX = velocityX; |
| 41 |
input.velocityY = velocityY; |
| 42 |
input.direction = direction; |
| 43 |
} |