Skip to content

Instantly share code, notes, and snippets.

@GoodNovember
Created August 13, 2017 01:21
Show Gist options
  • Save GoodNovember/254e29da2f88295f2c79a7f39237d245 to your computer and use it in GitHub Desktop.
Save GoodNovember/254e29da2f88295f2c79a7f39237d245 to your computer and use it in GitHub Desktop.
Event Buttons Parser, allows you to check which buttons are pressed in a more english friendly way.
function CreateEventButtonsParser(buttonNamesArray){
var buttonCount = buttonNamesArray.length
var variationsNeeded = Math.pow(2, buttonCount)
var seq = makeSequence(0, variationsNeeded, 1)
var vals = seq.map(function(value){ return getBinary(buttonCount, value) })
var map = vals.reduce(function(acc, binaryString, index){
acc[index] = parseBinaryString(buttonNamesArray, binaryString)
return acc
}, {})
function makeSequence(from, to, by){
var output = []
var start = from;
if(start < to){
for(start = from; start < to; start += by){
output.push(start)
}
}else if(start > to){
for(start = from; start > to; start += by){
output.push(start)
}
}
return output
}
function getBinary(digits, inputInt){
var raw = (inputInt).toString(2).split("").reverse().join("")
return padString(digits, "0", raw)
}
function padString(ideal, padChar, test){
var output = test
while(output.length < ideal){
output = output + padChar
}
return output
}
function parseBinaryString(stringNames, input){
var output = {}
var hasAny = false
var count = 0;
for(var i = 0; i < input.length; i++){
var name = stringNames[i]
var test = input[i] === "1"
output[name] = test
if(test){
count++;
}
if(!hasAny){
hasAny = test
}
}
output.any = hasAny
output.none = !hasAny
output.count = count
return output
}
return function(eventButtonsValue){
return map[eventButtonsValue]
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>scratchpad</title>
</head>
<body>
<div id="thingy">Make Mouse Target Element</div>
<script src="./buttonParser.js"></script>
<script>
var state = {}
// call them what you like, but they must be in this order:
var allPossibleButtons = ["left", "right", "middle", "fourth", "fifth"]
var buttonsICareAbout = ["Left", "RIGHT", "middle"]
// notice, how you spell them here, matches how they will be in the resulting object
var buttonsParserFn = CreateEventButtonsParser(buttonsICareAbout)
state.buttons = buttonsParserFn(0) // inital values
var elm = document.getElementById("thingy")
elm.addEventListener("mousedown", updateButtons)
elm.addEventListener("mouseup", updateButtons)
elm.addEventListener("mouseenter", updateButtons)
elm.addEventListener("mouseleave", updateButtons)
elm.addEventListener("mousemove", updateButtons)
function updateButtons(event){
state.buttons = buttonsParserFn(event.buttons)
}
function heartbeat(timestamp){
if(state.buttons.Left){
console.log("Left button is pressed!")
}
if(state.buttons.RIGHT){
console.log("Right button is pressed!")
}
// the following properties are automatically part of the state returned
if(state.buttons.count == 2){
// note, this will reach a maximum of the length of button names you provide in the array.
console.log("THERE ARE TWO BUTTONS PRESSED!!")
}
if(state.buttons.any){ // inlc
console.log("Any button is pressed!")
}
if(state.buttons.none){
// console.log("No button is pressed!")
}
requestAnimationFrame(heartbeat)
}
requestAnimationFrame(heartbeat)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment