Skip to content

Instantly share code, notes, and snippets.

@adrianobarroso
Created August 23, 2017 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianobarroso/ae7cb46e44e895b42b9a5529d214baa6 to your computer and use it in GitHub Desktop.
Save adrianobarroso/ae7cb46e44e895b42b9a5529d214baa6 to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script>
var width = 960;
var height = 500;
var radius = 20;
var margin = 100;
var x1 = margin;
var x2 = width - margin;
var y1 = height;
var y2 = height/2;
var drag = d3.behavior.drag()
.origin(function(d) { return d; })
.on("drag", dragmove);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.datum({
x: width / 2,
y: height / 2
});
var line = svg.append("line")
.attr("x1", x1)
.attr("x2", x2)
.attr("y1", y1)
.attr("y2", y2)
.style("stroke", "black")
.style("stroke-linecap", "round")
.style("stroke-width", 5);
var circle = svg.append("circle")
.attr("r", radius)
.attr("cy", function(d) { return d.y; })
.attr("cx", function(d) { return d.x; })
.style("cursor", "ew-resize")
.call(drag);
function dragmove(d) {
// Get the updated X location computed by the drag behavior.
var x = d3.event.x;
var y = d3.event.y;
// Constrain x to be between x1 and x2 (the ends of the line).
x = x < x1 ? x1 : x > x2 ? x2 : x;
// This assignment is necessary for multiple drag gestures.
// It makes the drag.origin function yield the correct value.
d.x = x;
d.y = y;
// Update the circle location.
circle.attr("cx", x).attr('cy', y);
}
</script>
</body><!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
svg.append("text")
.text("Edit the code below to change me!")
.attr("y", 200)
.attr("x", 120)
.attr("font-size", 36)
.attr("font-family", "monospace")
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment