Last active
August 29, 2015 14:11
-
-
Save jorpic/726f8cf80357a3aba220 to your computer and use it in GitHub Desktop.
d3.js spinner
This file contains hidden or 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> | |
<style> | |
html, body { | |
margin: 0; | |
color: #444; | |
} | |
#spinner { | |
display: none; | |
text-align: center; | |
} | |
</style> | |
<body> | |
<div id="spinner"> | |
<div></div> | |
<span>Prepare for the worst...</span> | |
</div> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://coffeescript.org/extras/coffee-script.js"></script> | |
<script type="text/coffeescript"> | |
# | |
# adapted from http://bl.ocks.org/Mattwoelk/6132258 | |
# | |
arc = (svg, rInner, rOuter, duration, direction) -> | |
a = d3.svg.arc() | |
.innerRadius(rInner) | |
.outerRadius(rOuter) | |
.startAngle(0) | |
x = if direction == 'clockwise' then [0,360] else [360,0] | |
x = x.map((alpha) -> "rotate(#{alpha})") | |
timer = null | |
stop = false | |
spin = (selection) -> | |
selection.transition() | |
.ease('linear') | |
.duration(duration) | |
.attrTween('transform', -> d3.interpolateString(x[0], x[1])) | |
if not stop | |
timer = setTimeout((-> spin selection), duration) | |
svg.append('path') | |
.datum(endAngle: 0.66*Math.PI) | |
.style('opacity', 0.2) | |
.attr('d', a) | |
.call(spin) | |
stop: -> | |
stop = true # prevent new timer from starting | |
clearTimeout(timer) # stop pending timer | |
spinner = (config) -> | |
r = Math.min(config.width, config.height) / 2 | |
duration = 1500 | |
svg = d3.select("#{config.container} div") | |
.append('svg') | |
.attr('width', config.width) | |
.attr('height', config.height) | |
.append('g') | |
.attr('transform', "translate(#{config.width/2},#{config.height/2})") | |
arc1 = arc(svg, r*0.5, r*0.9, duration, 'clockwise') | |
arc2 = arc(svg, r*0.1, r*0.4, duration, 'ccw') | |
d3.select(config.container).style('display', 'block') | |
stop: -> | |
d3.select(config.container).style('display', 'none') | |
arc1.stop() | |
arc2.stop() | |
spinner | |
width: 600 | |
height: 600 | |
container: '#spinner' | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment