Skip to content

Instantly share code, notes, and snippets.

@etaque
Created January 30, 2016 08:03
Show Gist options
  • Save etaque/586676245bedc2e6ff48 to your computer and use it in GitHub Desktop.
Save etaque/586676245bedc2e6ff48 to your computer and use it in GitHub Desktop.
Mouse drag and drop for Elm ports
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