Skip to content

Instantly share code, notes, and snippets.

@tonyfast
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save tonyfast/e3fb7c377d75d62b8b08 to your computer and use it in GitHub Desktop.

Select an option

Save tonyfast/e3fb7c377d75d62b8b08 to your computer and use it in GitHub Desktop.
d3 + color brewer + interaction
<head>
<style>
body{
background: black;
margin: 0px;
}
div{
font-size: 72px;
text-align : center;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="form-group">
<input type="text" class="form-control" placeholder="Start typing" id="words">
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<script>
;( function(){
d3.select('body')
.append('div')
function textcolor( word){
d3.select('div')
.selectAll( 'span.letter')
.data( word )
.call( function(s){
// append
s.enter()
.append('span')
.classed({letter:true})
//remove
s.exit().remove()
})
.text(function(d){
return d;
});
// split characeters
d3.selectAll('span.letter')
.each( function(d,i){
d3.select(this).style('color',s(i))
});
}
// init colorscale and text
s = setcolors( 'init' );
// Starts empty
d3.select("#words")
.on("keydown", function(){
textcolor(d3.select(this).property('value'));
});
function setcolors(op){
//
//convert colorbrewer to array
// colorbrewer = colorbrewer[1]['map']
var o = 6, //order of colormap
cmap = [],
d = d3.values(colorbrewer);
if( op != 'init' ){
console.log('holla');
d = d3.values( d3.shuffle( colorbrewer ) );
}
d.forEach( function(d){
d[o].forEach( function(d){cmap.push(d)})
});
return d3.scale.ordinal().range( cmap );
}
})();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment