Skip to content

Instantly share code, notes, and snippets.

@thgreasi
Created June 10, 2014 20:15
Show Gist options
  • Save thgreasi/724482849a0968b62b02 to your computer and use it in GitHub Desktop.
Save thgreasi/724482849a0968b62b02 to your computer and use it in GitHub Desktop.
;(function($, undefined) {
function findCenter(elem) {
var offset,
document = $(elem.ownerDocument);
elem = $(elem);
offset = elem.offset();
return {
x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
};
}
$.extend($.simulate.prototype, {
simulateDragPath: function() {
var i = 0,
target = this.target,
center = findCenter(target),
x = Math.floor(center.x),
y = Math.floor(center.y),
coord = {
clientX: x,
clientY: y
},
options = this.options,
pathi = 0,
pathlen = (options && options.length) || 0,
dx,
dy,
moves,
pathOptions;
this.simulateEvent(target, "mousedown", coord);
for (; pathi < pathlen; pathi++) {
pathOptions = options[pathi];
dx = pathOptions.dx || 0;
dy = pathOptions.dy || 0;
moves = pathOptions.moves || 3;
for (i = 0; i < moves; i++) {
x += dx / moves;
y += dy / moves;
coord = {
clientX: Math.round(x),
clientY: Math.round(y)
};
this.simulateEvent(document, "mousemove", coord);
}
}
this.simulateEvent(target, "mouseup", coord);
this.simulateEvent(target, "click", coord);
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment