Skip to content

Instantly share code, notes, and snippets.

@mtaptich
Last active December 10, 2015 01:44
Show Gist options
  • Select an option

  • Save mtaptich/c571e439a8d7b4b25a81 to your computer and use it in GitHub Desktop.

Select an option

Save mtaptich/c571e439a8d7b4b25a81 to your computer and use it in GitHub Desktop.
Variable Declaration

The fourth of a series of data visualizations on basic coding concepts. When you declare a variable, the next step is to assign it data!

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
width: 1024px;
margin-top: 0;
margin: auto;
font-family: "Lato", "PT Serif", serif;
color: #222222;
padding: 0;
font-weight: 300;
line-height: 33px;
-webkit-font-smoothing: antialiased;
}
.guide {
fill: none;
stroke: #7f8c8d;
stroke-dasharray: 2px 10px;
}
.circle {
stroke:#eee;
}
.c0{
fill: red;
}
.c1{
fill: blue;
}
.c2{
fill: green;
}
.c3{
fill: black;
}
.c4{
fill: pink;
}
.c5{
fill: brown;
}
.c6{
fill: purple;
}
.c7{
fill: orange;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 600,
translate_speed = 3000;
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var p1 = svg.append('path')
.attr('d', 'M'+500+','+100+'v'+200)
.attr('class', 'guide')
var p2 = svg.append('path')
.attr('d', 'M'+500+','+300+'v'+200)
.attr('class', 'guide')
// Static Vector
var assign = svg.append('text')
.attr('x',730)
.attr('y',105)
.style('text-anchor', 'middle')
.style('font-size', '24')
.text('{ Assign Data to Variable }')
var adjust = svg.append('text')
.attr('x',730)
.attr('y',305)
.style('text-anchor', 'middle')
.style('font-size', '24')
.text('{ Adjust Variable Data }')
var reassign = svg.append('text')
.attr('x',730)
.attr('y',505)
.style('text-anchor', 'middle')
.style('font-size', '24')
.text('{ Free Variable Data }')
// Model
function translateAlong(path) {
var l = path.getTotalLength();
return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + (p.x - 75) + "," + (p.y - 75/2) + ")";
};
};
}
function alter(){
var g = d3.select(this)
g.select('circle')
.transition()
.duration(translate_speed/10)
.ease('sin')
.attr("r", function() {
return d3.select(this).attr('r') / (Math.random()*1.3 + 0.4)
});
g.transition()
.duration(translate_speed)
.ease('sin')
.attrTween("transform", translateAlong(p2.node()))
.transition().duration(500)
.each("end", flank);
}
function flank(){
d3.select(this).select('rect')
.transition()
.duration(500)
.style('fill-opacity', '0.1')
.remove();
d3.select(this).select('circle')
.transition()
.duration(translate_speed/10)
.attr("transform", function() {
var t = d3.select(this).attr('transform').split(',');
return "translate("+ (-150)+","+t[1];})
.ease('sin')
.transition()
.duration(500)
.remove()
}
function assign_variable(){
var g = svg.append('g')
.attr('transform', 'translate(50,60)');
var circle = g.append('circle')
.attr('r', Math.random()*12+4.25)
.attr('transform', 'translate(75,32.5)')
.attr('class', 'circle c'+Math.floor(Math.random()*8))
g.transition()
.duration(translate_speed)
.attr("transform", function() {
var t = d3.select(this).attr('transform').split(',');
return "translate("+ (425)+","+t[1];})
.ease('sin')
.each("end", stored_variable);
}
function stored_variable(){
var g = d3.select(this);
var box = g.append('rect')
.attr('width', 150)
.attr('height', 75)
.style('fill', '#eee')
.style('fill-opacity', '0.5');
g.transition()
.duration(translate_speed)
.ease('sin')
.attrTween("transform", translateAlong(p1.node()))
.each("end", alter);
}
// Run model
setInterval(assign_variable, 1800)
d3.select(self.frameElement).style("height", (height+20) + "px");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment