Debian Codegraph @

src/inputjs/compute-input-data.js

raw
1 import { now } from '../utils/utils-consts';
2 import { abs } from '../utils/utils-consts';
3 import hasParent from '../utils/has-parent';
4 import simpleCloneInputData from './simple-clone-input-data';
5 import getCenter from './get-center';
6 import getDistance from './get-distance';
7 import getAngle from './get-angle';
8 import getDirection from './get-direction';
9 import computeDeltaXY from './compute-delta-xy';
10 import getVelocity from './get-velocity';
11 import getScale from './get-scale';
12 import getRotation from './get-rotation';
13 import computeIntervalInputData from './compute-interval-input-data';
14
15 /**
16 * @private
17 * extend the data with some usable properties like scale, rotate, velocity etc
18 * @param {Object} manager
19 * @param {Object} input
20 */
21 export default function computeInputData(manager, input) {
22 let { session } = manager;
23 let { pointers } = input;
24 let { length:pointersLength } = pointers;
25
26 // store the first input to calculate the distance and direction
27 if (!session.firstInput) {
28 session.firstInput = simpleCloneInputData(input);
29 }
30
31 // to compute scale and rotation we need to store the multiple touches
32 if (pointersLength > 1 && !session.firstMultiple) {
33 session.firstMultiple = simpleCloneInputData(input);
34 } else if (pointersLength === 1) {
35 session.firstMultiple = false;
36 }
37
38 let { firstInput, firstMultiple } = session;
39 let offsetCenter = firstMultiple ? firstMultiple.center : firstInput.center;
40
41 let center = input.center = getCenter(pointers);
42 input.timeStamp = now();
43 input.deltaTime = input.timeStamp - firstInput.timeStamp;
44
45 input.angle = getAngle(offsetCenter, center);
46 input.distance = getDistance(offsetCenter, center);
47
48 computeDeltaXY(session, input);
49 input.offsetDirection = getDirection(input.deltaX, input.deltaY);
50
51 let overallVelocity = getVelocity(input.deltaTime, input.deltaX, input.deltaY);
52 input.overallVelocityX = overallVelocity.x;
53 input.overallVelocityY = overallVelocity.y;
54 input.overallVelocity = (abs(overallVelocity.x) > abs(overallVelocity.y)) ? overallVelocity.x : overallVelocity.y;
55
56 input.scale = firstMultiple ? getScale(firstMultiple.pointers, pointers) : 1;
57 input.rotation = firstMultiple ? getRotation(firstMultiple.pointers, pointers) : 0;
58
59 input.maxPointers = !session.prevInput ? input.pointers.length : ((input.pointers.length >
60 session.prevInput.maxPointers) ? input.pointers.length : session.prevInput.maxPointers);
61
62 computeIntervalInputData(session, input);
63
64 // find the correct target
65 let target = manager.element;
66 const srcEvent = input.srcEvent;
67 let srcEventTarget;
68
69 if (srcEvent.composedPath) {
70 srcEventTarget = srcEvent.composedPath()[0];
71 } else if (srcEvent.path) {
72 srcEventTarget = srcEvent.path[0];
73 } else {
74 srcEventTarget = srcEvent.target;
75 }
76
77 if (hasParent(srcEventTarget, target)) {
78 target = srcEventTarget;
79 }
80 input.target = target;
81 }