Skip to content

Instantly share code, notes, and snippets.

@jfost00
Created February 12, 2016 00:12
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 jfost00/c226dbce63ad3451526e to your computer and use it in GitHub Desktop.
Save jfost00/c226dbce63ad3451526e to your computer and use it in GitHub Desktop.
Slider Control

Built with blockbuilder.org

Using a slider to control the radius of a circle. First simple example to get started.

<!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;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width:100%; height: 100% }
</style>
</head>
<body>
<h1>Use the slider to control the radius of the circle:</h1>
<input type="range" min="1" max="150" id="nRadius">
<script>
d3.select("#nRadius").on("input", function() {
update(+this.value);
});
function update(nRadius) {
// adjust the text on the range slider
d3.select("#nRadius-value").text(nRadius);
d3.select("#nRadius").property("value", nRadius);
// update the circle radius
svg.selectAll("circle")
.attr("r", nRadius);
}
// Feel free to change or delete any of the code you see!
var svg = d3.select("body").append("svg")
svg.append("circle")
.attr({cx: 200, cy: 200, r: 100})
.style({ fill: "#5e91e2"})
console.log("you are now rocking with d3", d3);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment