I have a draggable instance that snaps to y position based on an array.
I would like to send out a global event onDragEnd as to what the final snapped position would be.
I 'm using this approach, which currently is a little buggy, but I can probably make it more robust.
Is there a more elegant method?
const closest = (array, num) => {
let i = 0;
let minDiff = 1000;
let ans;
for (i in array) {
let m = Math.abs(num - array);
if (m < minDiff) {
minDiff = m;
ans = array;
}
}
return ans;
}
onDragCompleteY(e) {
let y = e.layerY;
let arr = this.generateSnapDistances();
let finalNum = closest(arr, y);
let SNAPPED_INDEX = arr.indexOf(finalNum);
}