MooTools Typewriter Effect Plugin

By  on  

Last week, I read an article in which the author created a typewriter effect using the jQuery JavaScript framework. I was impressed with the idea and execution of the code so I decided to port the effect to MooTools. After about an hour of coding, I had a smooth, customizable Mootools class that accomplished the same goal. Without further adieu, here's my MooTools 1.2 Typewriter plugin.

The MooTools JavaScript Class

//class is in
var Typewriter = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		container: $(document.body),
		message: '',
		delay: 150,
		cursor: 0
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
	},
	
	//start the typewriter
	start: function() {
		
		//for every letter
		for(x = 0; x < this.options.message.length; x++) {
			//spit out the letter
			var id = this.setLetter.delay(this.options.delay * x, this);
		}
	},
	
	//place the newest letter in the container
	setLetter: function() {
		
		this.options.container.set('html',this.options.container.get('html') + '' + this.options.message.charAt(this.options.cursor));
		
		//increment cursor
		this.options.cursor++;
	}
});

Once the class is initialized, options get set (also note the class defaults). Once the start method is called, we loop through every character and call setLetter() to place the next character into the container after the specified delay.

The Usage

var t = new Typewriter({
	container: $('type-here'),
	message: 'This is output from the typewriter class. This class is small but packs a good punch. Isn\'t MooTools great?',
	delay:100
}).start();

When creating your typewriter, you can pass it the following parameters:

  • container:: What element would you like the typewriter to write to?
  • message:: What do you want the message to be?
  • delay:: How much time would you like between letters?
  • cursor:: What position in the message (which character) do you want the Typewriter to start at?

Once you're ready for to start the typewriter, you'll call the start() method.

Recent Features

Incredible Demos

  • By
    Duplicate the jQuery Homepage Tooltips

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: The amount of jQuery required to duplicate this effect is next to nothing;  in fact, there's more CSS than there is jQuery code!  Let's explore how we can duplicate jQuery's tooltip effect. The HTML The overall...

  • By
    Create Your Own Dijit CSS Theme with LESS CSS

    The Dojo Toolkit seems to just get better and better.  One of the new additions in Dojo 1.6 was the use of LESS CSS to create Dijit themes.  The move to using LESS is a brilliant one because it makes creating your own Dijit theme...

Discussion

  1. would be nice if as an option you could implement some “typo” function, so randomly to mistype some chars and delete it.
    i think is just some “fancy” feature, but i thought you might like it :)

  2. @rborn: Awesome idea! I’ll think about those possibilities. Good suggestion.

  3. Catar4x

    Yes, Mootools is great !

  4. Amazing! Thanks you!

  5. I also think you should have the _ after the character its typing :D

  6. Darren

    Good work. It would be nice to be able to pass a string to the start function so you can add more to the string again with a second call. Or overwrite the first string. I also second the typo option and underscore option.

  7. Just a brain fart

    But it would be really cool if it was possible to overlay the text with something grunge, that would crack up the text link text

    Fontface embedding is properly the easiest way, but anyone have a idea with a image overlay/underlay that moves with the text?

  8. @Esben: I think the only way to this is writing a script which works with sIFR because this is the best way to embed only fonts I think.

  9. @Esben and @Mattias

    what about some png overlay, as css gradient text technique?

    would this be possible with a grunge pattern ?
    will not become a really typewriter font, but….

  10. @rborn:
    Nice idea! It should work I think. You need to know the height of your typewriting font and then you could create a seamless grunge-texture (as transparent PNG). The method could be creating a DIV-overlay (with the PNG as background-image) that changes it “width”-value referring to the amount of the typewrited letters. A problem would be the whitespaces I think but it would be a way.

  11. @Mattias

    if u go here

    http://www.webdesignerwall.com/demo/css-gradient-text/

    there is a “wow zebra” that seem to do the trick

    i think is just a matter image from there :)

  12. Ah yes alright – there isn’t a problem if you a have plain-colored background. I thought of a complex background (like a graphic or stripes). But the trick is very nice although. I’ll try that out later!

  13. Badchenn

    Another enhancement idea… As you know, we don’t always type at a steady speed. How about adding a speed variation? Maybe a range? Let’s say delay:100 variance:20 (where the variance could be a percentage of the delay setting). This along with the random worng letter/backspace feature would be cool.

  14. @Badchenn: Another great idea!

    Keep them coming everyone.

  15. @All: Version “2” is coming very soon. Any more suggestions before I release it to the world?

  16. Hi David,

    good work.

    My suggestions:

    Multiple messsges.
    Leading character (as seen on http://www.bbc.co.uk/news ticker)

    Hmm… not sure about rborn’s random mistype addition – from a user’s point of view I can see that being quite annoying.

    Still – I guess implementers don’t have to use that feature ;)

  17. Hey,

    I saw that you had used trackbacked me and thought I’d see what was going on.

    Awesome stuff. I’m glad that my code inspired you, and even across two different javascript frameworks. I was looking at the blinking underscore idea as someone else suggested and I actually finished doing it but never though that it was a feature anybody wanted. Guess I was wrong.

    Again thanks for the trackback and good luck with you Mootools class. :-)

  18. Greg

    Hey David,

    I was excited to find your Typewriter class. However, I’m using mootools 1.1 and receive the following error when I try to use it:

    this.options.container.get is not a function

    Any idea why? I assume it’s a version issue. Is it possible to tweak the class slightly to work with 1.1? I prefer not to deal with using 1.2; doing so breaks a lot of other script.

    Thanks,
    Greg

  19. sam

    Hi is there a way to have a few of these on one page each starting with an onclick

  20. Javascript newbie here. How would I make this start from an onClick instead.

  21. You would change the usage code to only fire on the click event.

  22. Rashy

    Hey David,

    Can you make a jQuery version of this plugin too ? Can you also add an option to add a arbitary text like “_” (Underscore) to the effect and delete it when the effect finishes. Eg:

    T_
    Te_
    Tes_
    Test

  23. Nice article but I think it needs to support rich format really… I.e. allow the text to have html tags within it so people can type out text that becomes a hyperlink. Admittedly could attach an event within the panel that loads the content, i.e. change the cursor to a hand, and even addClass to the text so it behaves like a normal hyperlink but I’m wondering whether or not that’s the best approach? Probably is, just a thought…

  24. Great script! Can you tell me how I can use a chain on this script? I want another script to be executed when the typewriter finishes. Thanks in advance!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!