As the main plugin, this will probably look something like this:
$.fn.extend({ lasso: function () { return this .mousedown(function (e) { // left mouse down switches on "capturing mode" if (e.which === 1 && !$(this).is(".lassoRunning")) { $(this).addClass("lassoRunning"); $(this).data("lassoPoints", []); } }) .mouseup(function (e) { // left mouse up ends "capturing mode" + triggers "Done" event if (e.which === 1 && $(this).is(".lassoRunning")) { $(this).removeClass("lassoRunning"); $(this).trigger("lassoDone", [$(this).data("lassoPoints")]); } }) .mousemove(function (e) { // mouse move captures co-ordinates + triggers "Point" event if ($(this).hasClass(".lassoRunning")) { var point = [e.offsetX, e.offsetY]; $(this).data("lassoPoints").push(point); $(this).trigger("lassoPoint", [point]); } }); } });
apply lasso() to any element and handle the events accordingly:
$("#myImg") .lasso() .on("lassoDone", function(e, lassoPoints) { // do something with lassoPoints }) .bind("lassoPoint", function(e, lassoPoint) { // do something with lassoPoint });
lassoPoint will be an array of X, Y coordinates. lassoPoints will be an array of lassoPoint .
You should probably include an additional check for the lasso enabled flag in the mousedown handler so you can enable or disable it yourself.
source share