Skip to content

Instantly share code, notes, and snippets.

@0x48piraj
Created October 27, 2022 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x48piraj/4cd7f8a1ae44cc70fbe89f87040a1ffe to your computer and use it in GitHub Desktop.
Save 0x48piraj/4cd7f8a1ae44cc70fbe89f87040a1ffe to your computer and use it in GitHub Desktop.
A small function for creating beautiful ASCII based animations on your website's title.
/*
usage: ASCIIAnimation(param1, param2, param3)
param1 - array of animation 'frames' (strings)
param2 - speed of animation in ms
usage:
var anim = new ASCIIAnimation([":)",":|",":("], 100);
*/
function ASCIIAnimation(animArray, speed) {
var currentFrame = 0;
for(var i = 0; i < animArray.length; i++) {
animArray[i] = animArray[i].replace(/ /g,"&nbsp;");
animArray[i] = animArray[i];
}
document.title = animArray[0];
currentFrame++;
this.animation = setInterval(function() {
document.title = animArray[currentFrame];
currentFrame++;
if(currentFrame >= animArray.length) currentFrame = 0;
}, speed);
this.getCurrentFrame = function() {
return currentFrame;
}
}
ASCIIAnimation.prototype.stopAnimation = function() {
clearInterval(this.animation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment