Debian Codegraph @

src/inputjs/get-center.js

raw
1 import { round } from '../utils/utils-consts';
2
3 /**
4 * @private
5 * get the center of all the pointers
6 * @param {Array} pointers
7 * @return {Object} center contains `x` and `y` properties
8 */
9 export default function getCenter(pointers) {
10 let pointersLength = pointers.length;
11
12 // no need to loop when only one touch
13 if (pointersLength === 1) {
14 return {
15 x: round(pointers[0].clientX),
16 y: round(pointers[0].clientY)
17 };
18 }
19
20 let x = 0;
21 let y = 0;
22 let i = 0;
23 while (i < pointersLength) {
24 x += pointers[i].clientX;
25 y += pointers[i].clientY;
26 i++;
27 }
28
29 return {
30 x: round(x / pointersLength),
31 y: round(y / pointersLength)
32 };
33 }