JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Drawing Rectangles with Boxer</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<style type="text/css" media="screen">
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; }
#canvas { height: 500px; background: white; }
</style>
</head>
<body>
<h1>ui.boxer</h1>
<h2>Directions: click and drag in the white region to draw a box</h2>
<div id="canvas"></div>
</body>
</html>
JavaScript
// Boxer plugin
$.widget("ui.boxer", $.extend({}, $.ui.mouse, {
_init: function() {
this.element.addClass("ui-boxer");
this.dragged = false;
this._mouseInit();
this.helper = $(document.createElement('div'))
.css({border:'1px dotted black'})
.addClass("ui-boxer-helper");
},
destroy: function() {
this.element
.removeClass("ui-boxer ui-boxer-disabled")
.removeData("boxer")
.unbind(".boxer");
this._mouseDestroy();
return this;
},
_mouseStart: function(event) {
var self = this;
this.opos = [event.pageX, event.pageY];
if (this.options.disabled)
return;
var options = this.options;
this._trigger("start", event);
$(options.appendTo).append(this.helper);
this.helper.css({
"z-index": 100,
"position": "absolute",
"left": event.clientX,
"top": event.clientY,
"width": 0,
"height": 0
});
},
_mouseDrag: function(event) {
var self = this;
this.dragged = true;
if (this.options.disabled)
return;
var options = this.options;
var x1 = this.opos[0], y1 = this.opos[1], x2 = event.pageX, y2 = event.pageY;
if (x1 > x2) { var tmp = x2; x2 = x1; x1 = tmp; }
if (y1 > y2) { var tmp = y2; y2 = y1; y1 = tmp; }
this.helper.css({left: x1, top: y1, width: x2-x1, height: y2-y1});
this._trigger("drag", event);
return false;
},
_mouseStop: function(event) {
var self = this;
this.dragged = false;
var options = this.options;
var clone = this.helper.clone()
.removeClass('ui-boxer-helper').appendTo(this.element);
this._trigger("stop", event, { box: clone });
this.helper.remove();
return false;
}
}));
$.extend($.ui.boxer, {
defaults: $.extend({}, $.ui.mouse.defaults, {
appendTo: 'body',
distance: 0
})
});
// Using the boxer plugin
$('#canvas').boxer({
stop: function(event, ui) {
var offset = ui.box.offset();
ui.box.css({...