Created
January 30, 2016 08:03
-
-
Save etaque/586676245bedc2e6ff48 to your computer and use it in GitHub Desktop.
Mouse drag and drop for Elm ports
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
const sendWindowMouseDrags = 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; | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment