Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!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.9.2/themes/base/jquery-ui.css" type="text/css" />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
  <style type="text/css" media="screen">
  body { background-color: #f00; font: 16px Helvetica, Arial; color: #eaeaea; }
  #canvas { height: 500px; background: white; }
    #select_canvas {
    display: none;
    z-index: 9999;
    position: absolute;
    left: 0px;
    top: 0px;
}
p {
    width: 400px;
    border: 1px solid yellow;
}
body {
    padding: 50px;
}
  </style>
</head>
<body>
<button id="start_select">Start selecting region</button>
<canvas id="select_canvas" width="800" height="800"></canvas>
<p>L.</p>
</body>
</html>
 
$("#start_select").click(function() {
  $("#select_canvas").show();
  
});
$('*').bind('selectstart', false);
var start = null;
var ctx = $("#select_canvas").get(0).getContext('2d');
ctx.globalAlpha = 0.5;
$("#select_canvas").mousedown(function(e) {
    start = [e.offsetX, e.offsetY];
  for(var key in e)
    console.log("e key:"+e[key]);
    
}).mouseup(function(e) {
    end = [e.offsetX, e.offsetY];
    
    var x1 = Math.min(start[0], end[0]),
        x2 = Math.max(start[0], end[0]),
        y1 = Math.min(start[1], end[1]),
        y2 = Math.max(start[1], end[1]);
  console.log("("+x1+","+y1+")-("+x2+","+y2+")");
    
    var grabbed = [];
    $('*').each(function() {
        if(!$(this).is(":visible")) return;
      console.log("x1:"+x1+" x2:"+x2);
        var o = $(this).offset(),
            x = o.left,
            y = o.top,
            w = $(this).width(),
            h = $(this).height();
        
        if(x > x1 && x + w < x2 && y > y1 && y + h < y2) {
            grabbed.push(this);
        }
    });
    console.log(grabbed);
    
    start = null;
    
    
    
}).mousemove(function(e) {
    if(!start) return;
    
  //  ctx.clearRect(0, 0, this.offsetWidth, this.offsetHeight);
    ctx.beginPath();
    
    var x = e.offsetX,
        y = e.offsetY;
    
    ctx.rect(start[0], start[1], x - start[0], y - start[1]);
    ctx.fill();
});
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers