Skip to content

Instantly share code, notes, and snippets.

@uberFoo
Forked from mccannf/README.markdown
Last active August 30, 2016 20:06
Show Gist options
  • Save uberFoo/e8751978c3ccae9c4885 to your computer and use it in GitHub Desktop.
Save uberFoo/e8751978c3ccae9c4885 to your computer and use it in GitHub Desktop.

This is an example of the power of the D3 library and how you can use the drag behavior of D3 to control the position and shape of the SVG element.

var w = 750,
h = 450,
r = 120;
var isXChecked = true,
isYChecked = true;
var width = 300,
height = 200,
dragbarw = 10;
var drag = d3.behavior.drag()
.origin(function(d) { return d; })
.on("drag", dragmove);
var dragright = d3.behavior.drag()
.origin(Object)
.on("drag", rdragresize);
var dragleft = d3.behavior.drag()
.origin(Object)
.on("drag", ldragresize);
var dragtop = d3.behavior.drag()
.origin(Object)
.on("drag", tdragresize);
var dragbottom = d3.behavior.drag()
.origin(Object)
.on("drag", bdragresize);
var dragtopleft = d3.behavior.drag()
.origin(Object)
.on('drag', tldragresize);
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h)
var newg = svg.append("g")
.data([{x: width / 2, y: height / 2}]);
var dragrect = newg.append("rect")
.attr("id", "active")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("height", height)
.attr("width", width)
.attr("fill-opacity", .5)
.attr("cursor", "move")
.call(drag);
var dragbarleft = newg.append("rect")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw)
.attr("id", "dragleft")
.attr("width", dragbarw)
.attr("fill", "lightblue")
.attr("fill-opacity", .5)
.attr("cursor", "ew-resize")
.call(dragleft);
var dragbarright = newg.append("rect")
.attr("x", function(d) { return d.x + width - dragbarw; })
.attr("y", function(d) { return d.y + dragbarw; })
.attr("id", "dragright")
.attr("height", height - 2 * dragbarw)
.attr("width", dragbarw)
.attr("fill", "lightblue")
.attr("fill-opacity", .5)
.attr("cursor", "ew-resize")
.call(dragright);
var dragbartop = newg.append("rect")
.attr("x", function(d) { return d.x + dragbarw; })
.attr("y", function(d) { return d.y; })
.attr("height", dragbarw)
.attr("id", "dragleft")
.attr("width", width - 2 * dragbarw)
.attr("fill", "lightgreen")
.attr("fill-opacity", .5)
.attr("cursor", "ns-resize")
.call(dragtop);
var dragbarbottom = newg.append("rect")
.attr("x", function(d) { return d.x + dragbarw; })
.attr("y", function(d) { return d.y + height - dragbarw; })
.attr("id", "dragright")
.attr("height", dragbarw)
.attr("width", width - 2 * dragbarw)
.attr("fill", "lightgreen")
.attr("fill-opacity", .5)
.attr("cursor", "ns-resize")
.call(dragbottom);
var dragbartopleft = newg.append("rect")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("height", dragbarw)
.attr("width", dragbarw)
.attr("fill", "purple")
.attr("fill-opacity", .5)
.attr("cursor", "nwse-resize")
.call(dragtopleft);
var dragbartopright = newg.append("rect")
.attr("x", function(d) { return d.x + width - dragbarw; })
.attr("y", function(d) { return d.y; })
.attr("height", dragbarw)
.attr("width", dragbarw)
.attr("fill", "purple")
.attr("fill-opacity", .5)
.attr("cursor", "nesw-resize");
var dragbarbottomleft = newg.append("rect")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y + height - dragbarw; })
.attr("height", dragbarw)
.attr("width", dragbarw)
.attr("fill", "purple")
.attr("fill-opacity", .5)
.attr("cursor", "nesw-resize");
var dragbarbottomright = newg.append("rect")
.attr("x", function(d) { return d.x + width - dragbarw; })
.attr("y", function(d) { return d.y + height - dragbarw; })
.attr("height", dragbarw)
.attr("width", dragbarw)
.attr("fill", "purple")
.attr("fill-opacity", .5)
.attr("cursor", "nwse-resize");
function dragmove(d) {
if (isXChecked) {
dragrect
.attr("x", d.x = Math.max(0, Math.min(w - width, d3.event.x)))
dragbarleft
.attr("x", function(d) { return d.x; })
dragbarright
.attr("x", function(d) { return d.x + width - dragbarw; })
dragbartop
.attr("x", function(d) { return d.x + dragbarw; })
dragbarbottom
.attr("x", function(d) { return d.x + dragbarw; })
dragbartopleft
.attr("x", function(d) { return d.x; });
dragbarbottomleft
.attr("x", function(d) { return d.x; });
dragbartopright
.attr("x", function(d) { return d.x + width - dragbarw; });
dragbarbottomright
.attr("x", function(d) { return d.x + width - dragbarw; });
}
if (isYChecked) {
dragrect
.attr("y", d.y = Math.max(0, Math.min(h - height, d3.event.y)));
dragbarleft
.attr("y", function(d) { return d.y + dragbarw; });
dragbarright
.attr("y", function(d) { return d.y + dragbarw; });
dragbartop
.attr("y", function(d) { return d.y; });
dragbarbottom
.attr("y", function(d) { return d.y + height - dragbarw; });
dragbartopleft
.attr("y", function(d) { return d.y; });
dragbarbottomleft
.attr("y", function(d) { return d.y + height - dragbarw; });
dragbartopright
.attr("y", function(d) { return d.y; });
dragbarbottomright
.attr("y", function(d) { return d.y + height - dragbarw; });
}
}
function ldragresize(d) {
if (isXChecked) {
var oldx = d.x;
//Max x on the right is x + width - dragbarw
//Max x on the left is 0 - (dragbarw/2)
d.x = Math.max(0, Math.min(d.x + width - dragbarw, d3.event.x));
width = width + (oldx - d.x);
dragbarleft
.attr("x", function(d) { return d.x; });
dragrect
.attr("x", function(d) { return d.x; })
.attr("width", width);
dragbartop
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw)
dragbarbottom
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw)
}
}
function rdragresize(d) {
if (isXChecked) {
//Max x on the left is x - width
//Max x on the right is width of screen + (dragbarw/2)
var dragx = Math.max(d.x + dragbarw, Math.min(w, d.x + width + d3.event.dx));
//recalculate width
width = dragx - d.x;
//move the right drag handle
dragbarright
.attr("x", function(d) { return dragx - dragbarw });
//resize the drag rectangle
//as we are only resizing from the right, the x coordinate does not need to change
dragrect
.attr("width", width);
dragbartop
.attr("width", width - 2 * dragbarw)
dragbarbottom
.attr("width", width - 2 * dragbarw)
}
}
function tdragresize(d) {
if (isYChecked) {
var oldy = d.y;
//Max x on the right is x + width - dragbarw
//Max x on the left is 0 - (dragbarw/2)
d.y = Math.max(0, Math.min(d.y + height - dragbarw, d3.event.y));
height = height + (oldy - d.y);
dragbartop
.attr("y", function(d) { return d.y; });
dragrect
.attr("y", function(d) { return d.y; })
.attr("height", height);
dragbarleft
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
dragbarright
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
}
}
function bdragresize(d) {
if (isYChecked) {
//Max x on the left is x - width
//Max x on the right is width of screen + (dragbarw/2)
var dragy = Math.max(d.y + (dragbarw), Math.min(h, d.y + height + d3.event.dy));
//recalculate width
height = dragy - d.y;
//move the right drag handle
dragbarbottom
.attr("y", function(d) { return dragy - (dragbarw) });
//resize the drag rectangle
//as we are only resizing from the right, the x coordinate does not need to change
dragrect
.attr("height", height);
dragbarleft
.attr("height", height - 2 * dragbarw);
dragbarright
.attr("height", height - 2 * dragbarw);
}
}
function ldragresize(d) {
if (isXChecked) {
var oldx = d.x;
//Max x on the right is x + width - dragbarw
//Max x on the left is 0 - (dragbarw/2)
d.x = Math.max(0, Math.min(d.x + width - dragbarw, d3.event.x));
width = width + (oldx - d.x);
dragbarleft
.attr("x", function(d) { return d.x; });
dragrect
.attr("x", function(d) { return d.x; })
.attr("width", width);
dragbartop
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw)
dragbarbottom
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw)
}
}
function tldragresize(d) {
if (isXChecked && isYChecked) {
var oldx = d.x;
var oldy = d.y;
d.x = Math.max(0, Math.min(d.x + width - dragbarw, d3.event.x));
d.y = Math.max(0, Math.min(d.y + height - dragbarw, d3.event.y));
width = width + (oldx - d.x);
height = height + (oldy - d.y);
dragbartopleft
.attr('x', function(d) { return d.x; })
.attr('y', function(d) {return d.y; });
dragbarleft
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
dragbartop
.attr("y", function(d) { return d.y; })
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw);
dragrect
.attr("x", function(d) { return d.x; })
.attr("width", width)
.attr("y", function(d) { return d.y; })
.attr("height", height);
dragbarbottom
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw);
dragbarright
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
dragbartopright
.attr('y', function(d) { return d.y; });
dragbarbottomleft
.attr('x', function(d) { return d.x; });
}
}
function bldragresize(d) {
if (isXChecked && isYChecked) {
var oldx = d.x;
var oldy = d.y;
d.x = Math.max(0, Math.min(d.x + width - dragbarw, d3.event.x));
d.y = Math.max(0, Math.min(d.y + height - dragbarw, d3.event.y));
width = width + (oldx - d.x);
height = height + (oldy - d.y);
dragbarbottomleft
.attr('x', function(d) { return d.x; })
.attr('y', function(d) {return d.y; });
dragbarleft
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
dragbartop
.attr("y", function(d) { return d.y; })
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw);
dragrect
.attr("x", function(d) { return d.x; })
.attr("width", width)
.attr("y", function(d) { return d.y; })
.attr("height", height);
dragbarbottom
.attr("x", function(d) { return d.x + dragbarw; })
.attr("width", width - 2 * dragbarw);
dragbarright
.attr("y", function(d) { return d.y + dragbarw; })
.attr("height", height - 2 * dragbarw);
dragbartopright
.attr('y', function(d) { return d.y; });
dragbarbottomleft
.attr('x', function(d) { return d.x; });
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
svg {
border: solid 1px #aaa;
}
#checks {
margin: 10px;
}
</style>
</head>
<body>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<script type="text/javascript" src="dragrect.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<div id="checks">
X-axis:<input type="checkbox" id="xChecked" checked/>
Y-axis:<input type="checkbox" id="yChecked" checked/>
</div>
<script>
$('#xChecked').click(function() {
isXChecked = !isXChecked;
dragbarleft
.attr("fill-opacity", (isXChecked ? 0.5 : 0))
.attr("cursor", (isXChecked ? "ew-resize" : "move"));
dragbarright
.attr("fill-opacity", (isXChecked ? 0.5 : 0))
.attr("cursor", (isXChecked ? "ew-resize" : "move"));
});
$('#yChecked').click(function() {
isYChecked = !isYChecked;
dragbartop
.attr("fill-opacity", (isYChecked ? 0.5 : 0))
.attr("cursor", (isYChecked ? "ns-resize" : "move"));
dragbarbottom
.attr("fill-opacity", (isYChecked ? 0.5 : 0))
.attr("cursor", (isYChecked ? "ns-resize" : "move"));
});
</script>
</body>
</html>
@jakeNiemiec
Copy link

Nice attempt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment