Skip to content

Instantly share code, notes, and snippets.

@sarahob
Last active March 10, 2016 15:34
Show Gist options
  • Save sarahob/ea19dd7e23dd4ef1e499 to your computer and use it in GitHub Desktop.
Save sarahob/ea19dd7e23dd4ef1e499 to your computer and use it in GitHub Desktop.
Randomise Text

Just a quick and dirty implementation to see what transitioning between random text would look like...

<!DOCTYPE html>
<meta charset='utf-8'>
<body>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js'></script>
<script>
var wordAr = ['None', 'Low', 'Medium', 'High', 'Critcial'],targetWord = 'Critical';
var svg = d3.select('body')
.append('svg')
.attr('height', 200)
.attr('width', 500);
var text = svg.append('text')
.attr('fill', '#000')
.attr('x', 100)
.attr('y', 100)
.style('font-size', '32px')
.style('font-family', 'Arial');
function transitionText(i){
text.text(function(){
return Number.isInteger(i) ? wordAr[i] : i;
});
}
var timer = 50, intId;
intId = setInterval(function(){
var i = Math.floor(Math.random() * 5);
transitionText(i);
timer--;
if(timer === 0){
transitionText(targetWord);
clearInterval(intId);
}
},150);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment