Built with blockbuilder.org
forked from adry34160's block: 18 - Interactive text rotation with d3.js
license: mit |
Built with blockbuilder.org
forked from adry34160's block: 18 - Interactive text rotation with d3.js
<!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> | |
<p> | |
<label for="nAngle" | |
style="display: inline-block; width: 240px; text-align: right"> | |
angle = <span id="nAngle-value">...</span> | |
</label> | |
<input type="range" min="0" max="360" id="nAngle"> | |
</p> | |
<script> | |
var width = 600; | |
var height = 300; | |
var holder = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
// draw the d3.js text | |
holder.append("text") | |
.attr("class", "d3js") | |
.style("fill", "black") | |
.style("font-size", "56px") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(300,55) rotate(0)") | |
.text("d3.js"); | |
// draw the let it bi text | |
holder.append("text") | |
.attr("class", "letitbi") | |
.style("fill", "black") | |
.style("font-size", "56px") | |
.attr("dy", ".35em") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(300,130) rotate(0)") | |
.text("letitbi.net"); | |
// when the input range changes update the angle | |
d3.select("#nAngle").on("input", function() { | |
update(+this.value); | |
}) | |
// initial starting angle of the text | |
update(0); | |
function update(nAngle) { | |
//adjust the text on the range slicer | |
d3.select("#nAngle-value").text(nAngle); | |
d3.select("#nAngle").property("value", nAngle); | |
// rotate the text | |
holder.select("text.d3js") | |
.attr("transform", "translate(300, 55) rotate("+nAngle+")"); | |
// rotate the text | |
holder.select("text.letitbi") | |
.attr("transform", "translate(300, 130) rotate("+(360-nAngle)+")"); | |
} | |
</script> |