Last active
December 17, 2015 13:19
-
-
Save JohnDelacour/5616176 to your computer and use it in GitHub Desktop.
Simple Drag Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var circle_data = [ | |
{x:20, y:60}, | |
{x: 45, y:56}, | |
]; | |
var square_data = [ | |
{x:100, y:100, width:80, height:80}, | |
{x:150, y:150, width:80, height:80}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
svg { | |
background-color: #111; | |
} | |
circle, .circle { | |
fill: #b36; | |
fill-opacity: 0.8; | |
} | |
#square, .square { | |
fill: #679; | |
fill-opacity: 0.8; | |
} | |
#report { | |
padding: 5px 8px 5px 8px; | |
background-color: #333; | |
color:white; | |
font: 14pt sans-serif; | |
position:absolute; | |
left: 85px; | |
top: 70px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Simple Drag Demo</title> | |
<script src="http://d3js.org/d3.v2.js"></script> | |
<script src="data.js"></script> | |
<link type="text/css" href="drag.css" rel="stylesheet"> | |
</head> | |
<body> | |
<svg width="960" height="500"> | |
<defs></defs> | |
<g id="g0" transform="translate(100 150)"></g> | |
</svg> | |
<div id="report"> | |
x, y | |
</div> | |
<script> | |
var svg = d3.select("svg"), | |
defs = d3.select("defs"), | |
g0 = d3.select("#g0"); | |
var squares = g0.selectAll("rect").data(square_data) | |
.enter().append("rect") | |
.attr("class", "square") | |
.attr("width", function(d) { return d.width }) | |
.attr("height", function(d) { return d.height }) | |
.attr("transform", function(d){ return 'translate('+ [d.x, d.y] + ')' }) | |
var circles = g0.selectAll("circle").data(circle_data) | |
.enter().append("circle") | |
.attr("r", 20) | |
.attr("transform", function(d) { return 'translate('+ [d.x, d.y] + ')' }) | |
var drag = d3.behavior.drag() | |
.on("drag", function(d, i) { | |
var dx = d3.event.dx, dy = d3.event.dy; | |
d.x += dx; | |
d.y += dy; | |
d3.select(this).attr("transform", "translate(" + [d.x, d.y] + ")" ); | |
d3.select("#report") .text("x: " + (d.x) + ", y: " + (d.y)); | |
} | |
) | |
circles.call(drag); | |
squares.call(drag); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment