简体   繁体   English

JavaScript中的套索工具

[英]Lasso tool in javascript

Hay, I'm writing a simple web based image maker, and would like to know if anyone has any idea's how to implement a lasso tool. Hay,我正在写一个简单的基于Web的图像制作者,并且想知道是否有人知道如何实现套索工具。 I'd like to be able to save all the points so that I can easily send them to a database and retrieve them at a later date. 我希望能够保存所有点,以便我可以轻松地将它们发送到数据库并在以后检索它们。

As a basic plug-in, this would 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]);
        }
      });
  }
});

later, apply lasso() to any element and handle the events accordingly: 稍后,将lasso()应用于任何元素并相应地处理事件:

$("#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 co-ordinates. lassoPoint将是X,Y坐标的数组。 lassoPoints will be an array of lassoPoint . lassoPoints将是一个lassoPoint数组。

You should probably include an extra check for a "lasso enabled" flag of some sort into the mousedown handler, so that you can switch it on or off independently. 你可能应该在mousedown处理程序中额外检查某种“lasso enabled”标志,以便你可以独立地打开或关闭它。

Heres a plugin that seems to do just that http://odyniec.net/projects/imgareaselect/examples.html 这是一个插件似乎只是那个http://odyniec.net/projects/imgareaselect/examples.html

I haven't used it. 我没用过它。

I've never made one, but if you're looking to make your own, id imagine they work like 我从来没有做过,但是如果你想制作自己的,想象一下他们的工作就像

onmousedown record initial mouse coords(this is the coords of the corner of lasso box) onmousedown记录初始鼠标坐标(这是套索角落的坐标)

onmousemove subtract new coords from initial coords to get width and height of div being used for the visual lasso box. onmousemove从初始坐标中减去新的坐标,以获得用于视觉套索框的div的宽度和高度。

onmouseup, stop listening to mousemove, do something with coords and dimension of any lasso box existing onmouseup,停止听mousemove,用coords做任何事情和任何套索盒的尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM