Created
January 7, 2016 14:48
-
-
Save etaque/949a7384e006e2c0fa8d to your computer and use it in GitHub Desktop.
Send window mouse drag events to Elm port
This file contains hidden or 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
const sendMouseDrags = port => { | |
let mouseDown = false; | |
let mousePosition = { x: null, y: null }; | |
const getPoint = e => ({ x: e.pageX, y: e.pageY }); | |
document.addEventListener('mousedown', e => { | |
mousePosition = getPoint(e); | |
mouseDown = true; | |
port.send([mousePosition, null]); | |
}); | |
document.addEventListener('mouseup', e => { | |
mousePosition = getPoint(e); | |
mouseDown = false; | |
port.send([null, mousePosition]); | |
}); | |
document.addEventListener('mousemove', e => { | |
const newPosition = getPoint(e); | |
if (mousePosition.x && mousePosition.y && mouseDown) { | |
port.send([mousePosition, newPosition]); | |
} | |
mousePosition = newPosition; | |
}); | |
}; | |
module.exports = sendMouseDrags; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment