Skip to content

Instantly share code, notes, and snippets.

@JohnDelacour
Last active December 17, 2015 09:38
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 JohnDelacour/5588283 to your computer and use it in GitHub Desktop.
Save JohnDelacour/5588283 to your computer and use it in GitHub Desktop.
2-speed Revolving Bar
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>2-speed Revolving Bar</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<link rel="stylesheet" type="text/css" href="revolving_bar.css">
</head>
<body>
<div id="topline">
<p>Click the background to run</p>
</div>
<script>
var w = 960, h = 500;
var angle_data = [];
var i, n = 0, limit = 360;
var steps = [0.3, 0.1]
while ( n < limit) {
n < 120 || n > 240 ? n += steps[0] : n += steps[1]
angle_data.push(n);
}
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h)
svg.append("rect")
.attr("id", "background")
.attr("x", "1")
.attr("y", "1")
.attr("width", w -3)
.attr("height", h - 3)
var t = svg.append("text")
.attr("id", "t")
.attr("x", "30")
.attr("y", "40")
.text("Seconds")
var t = svg.append("text")
.attr("id", "th")
.attr("x", "30")
.attr("y", "60")
.text("Degrees")
var g1 = svg.append("g")
.attr("transform", "translate(" + (w/2) + " " + (h/2) + ")")
var circle = g1.append("circle")
.attr("r", "140")
.style("fill", "none");
var rect = g1.append("rect")
.attr("id", "r1")
.attr("x", "-140")
.attr("y", -5)
.attr("width", "280")
.attr("height", "10")
.attr("transform", "rotate(0)");
var run = function() {
var tim = 8000;
rect.transition()
.delay(0)
.ease("linear")
.duration(tim)
.attrTween("transform", function() { // was function(d, i)
return function(t) {
d3.select("#t") .text("Seconds: " + d3.round(tim/1000*t, 4))
var th = angle_data[d3.round(t * angle_data.length)];
isNaN(th) ? th = limit : th;
d3.select("#th") .text("Degrees: " + d3.round(th, 2));
return "rotate("+ th +")";
};
}
);
};
d3.select("#background")
.on("click", function (){run()})
run();
</script>
</body>
</html>
#r1, circle {
fill: burlywood;
stroke: saddlebrown
}
circle {
fill: none;
stroke: saddlebrown
}
#background{
stroke:navy;
fill: aliceblue;
}
#t, #th {
font: 11pt sans-serif;
fill: #306;
}
#topline {
font: 13pt sans-serif;
color: #306;
position: absolute;
left: 380px;
top: 20px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment