Last active
December 10, 2019 06:22
-
-
Save rap0so/f8dd897a8e8018aeeec730e9a6419fe2 to your computer and use it in GitHub Desktop.
Method to get shake moves
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import throttle from 'throttle'; | |
const totalMoves = []; | |
let totalShakes = 0; | |
const mousePos = event => { | |
totalMoves.push({ | |
x: event.clientX, | |
y: event.clientY | |
}); | |
}; | |
const throttledMousePos = throttle(mousePos, 200); | |
const testPivot = (list, pivot) => { | |
const passed = list.filter(number => { | |
const diff = number - pivot; | |
return diff > 0 && diff <= 150; | |
}); | |
return passed.length >= 3; | |
}; | |
const _setShake = () => | |
document.addEventListener('mousemove', throttledMousePos); | |
const getAllShakes = () => { | |
if (totalMoves.length < 4) return 0; | |
const pairNumbers = totalMoves.filter((element, idx) => idx % 2 === 0); | |
const pivotX = | |
pairNumbers.reduce((acc, current) => acc + current.x, 0) / | |
pairNumbers.length; | |
const pivotY = | |
pairNumbers.reduce((acc, current) => acc + current.y, 0) / | |
pairNumbers.length; | |
const passOnPivotX = testPivot(pairNumbers.map(num => num.x), pivotX); | |
const passOnPivotY = testPivot(pairNumbers.map(num => num.y), pivotY); | |
if (passOnPivotX && passOnPivotY) { | |
totalShakes += 1; | |
} | |
return totalShakes; | |
}; | |
const getShake = () => | |
new Promise(resolve => | |
resolve({ | |
shake_clicks: getAllShakes() | |
}) | |
); | |
export { _setShake, getShake }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment