Skip to content

Instantly share code, notes, and snippets.

@etaque
Created January 7, 2016 14:48
Show Gist options
  • Save etaque/949a7384e006e2c0fa8d to your computer and use it in GitHub Desktop.
Save etaque/949a7384e006e2c0fa8d to your computer and use it in GitHub Desktop.
Send window mouse drag events to Elm port
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