Skip to content

Instantly share code, notes, and snippets.

@zz85
Last active July 26, 2022 19:00
Show Gist options
  • Save zz85/443a9b370a61a82e55e8 to your computer and use it in GitHub Desktop.
Save zz85/443a9b370a61a82e55e8 to your computer and use it in GitHub Desktop.
Generic onDrag handler
/*
@author twitter.com/blurspline
*/
function handleDrag(element, ondown, onmove, onup, down_criteria) {
var pointer = null;
var bounds = element.getBoundingClientRect();
element.addEventListener('mousedown', onMouseDown);
function onMouseDown(e) {
if (down_criteria && !down_criteria(e)) return;
e.preventDefault();
handleStart(e);
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
ondown(pointer);
}
function onMouseMove(e) {
handleMove(e);
pointer.moved = true;
onmove(pointer);
}
function handleStart(e) {
bounds = element.getBoundingClientRect();
var currentx = e.clientX, currenty = e.clientY;
pointer = {
startx: currentx,
starty: currenty,
x: currentx,
y: currenty,
dx: 0,
dy: 0,
offsetx: currentx - bounds.left,
offsety: currenty - bounds.top,
moved: false
};
}
function handleMove(e) {
bounds = element.getBoundingClientRect();
var currentx = e.clientX,
currenty = e.clientY,
offsetx = currentx - bounds.left,
offsety = currenty - bounds.top;
pointer.x = currentx;
pointer.y = currenty;
pointer.dx = e.clientX - pointer.startx;
pointer.dy = e.clientY - pointer.starty;
pointer.offsetx = offsetx;
pointer.offsety = offsety;
}
function onMouseUp(e) {
handleMove(e);
onup(pointer);
pointer = null;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
element.addEventListener('touchstart', onTouchStart);
function onTouchStart(te) {
if (te.touches.length == 1) {
var e = te.touches[0];
if (down_criteria && !down_criteria(e)) return;
te.preventDefault();
handleStart(e);
ondown(pointer);
}
element.addEventListener('touchmove', onTouchMove);
element.addEventListener('touchend', onTouchEnd);
}
function onTouchMove(te) {
var e = te.touches[0];
onMouseMove(e);
}
function onTouchEnd(e) {
// var e = e.touches[0];
onMouseUp(e);
element.removeEventListener('touchmove', onTouchMove);
element.removeEventListener('touchend', onTouchEnd);
}
this.release = function() {
element.removeEventListener('mousedown', onMouseDown);
element.removeEventListener('touchstart', onTouchStart);
};
}
module.exports = handleDrag;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment