Skip to content

Instantly share code, notes, and snippets.

@bricof
Last active May 10, 2017 13:27
Show Gist options
  • Save bricof/7a4e0f1d6801fb100a8ecbf3918a7eeb to your computer and use it in GitHub Desktop.
Save bricof/7a4e0f1d6801fb100a8ecbf3918a7eeb to your computer and use it in GitHub Desktop.
Animated Typing
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<svg width="960" height="500">
<text transform="translate(150 250)" font-size="18px"></text>
</svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function stylist_note_typing_animation(progress){
var text_val = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do ..."
var text_progress = Math.max(0,
Math.min( text_val.length + 1,
Math.floor(progress * text_val.length)))
d3.select("svg").select("text")
.text(text_val.substring(0, text_progress))
}
var progress = 0
d3.interval(function(){
var r = Math.random()
if (r < 0.8) {
if ((r < 0.1) && (progress > 0.2)) {
progress -= 0.01
} else {
progress += 0.01
}
stylist_note_typing_animation(progress)
}
}, 50)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment