MooTools Typewriter Effect Plugin

Written by David Walsh on Tuesday, July 8, 2008


This article may feature code that is no longer best practice in MooTools.
Click here to learn what has changed to make your code framework-compatible.

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.


Follow via RSS Epic Discussion

Commenter Avatar July 08 / #
rborn says:

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 :)

David Walsh July 08 / #
david says:

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

Commenter Avatar July 08 / #
Catar4x says:

Yes, Mootools is great !

Commenter Avatar July 08 / #
shakaran says:

Amazing! Thanks you!

Commenter Avatar July 08 / #
Lachy G says:

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

Commenter Avatar July 08 / #
Darren says:

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.

Commenter Avatar July 08 / #

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?

Commenter Avatar July 09 / #
Matthias says:

@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.

Commenter Avatar July 09 / #
rborn says:

@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….

Commenter Avatar July 09 / #
Matthias says:

@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.

Commenter Avatar July 09 / #
rborn says:

@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 :)

Commenter Avatar July 09 / #
Matthias says:

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!

Commenter Avatar July 09 / #
Badchenn says:

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.

David Walsh July 09 / #
david says:

@Badchenn: Another great idea!

Keep them coming everyone.

David Walsh July 09 / #
david says:

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

Commenter Avatar July 10 / #
Shane says:

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 ;)

Commenter Avatar July 11 / #

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. :-)

Commenter Avatar July 30 / #
Greg says:

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

Commenter Avatar December 15 / #
sam says:

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

Commenter Avatar December 23 / #
Phil says:

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

David Walsh December 23 / #
david says:

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

Commenter Avatar May 23 / #
Rashy says:

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

Commenter Avatar October 24 / #

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…

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools javascript framework.