MooTools Typewriter Effect Plugin
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.
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 :)
@rborn: Awesome idea! I’ll think about those possibilities. Good suggestion.
Yes, Mootools is great !
Amazing! Thanks you!
I also think you should have the _ after the character its typing :D
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.
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?
@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.
@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….
@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.
@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 :)
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!
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.
@Badchenn: Another great idea!
Keep them coming everyone.
@All: Version “2” is coming very soon. Any more suggestions before I release it to the world?
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 ;)
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. :-)
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
Hi is there a way to have a few of these on one page each starting with an onclick
Javascript newbie here. How would I make this start from an onClick instead.
You would change the usage code to only fire on the click event.
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
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…
See same effect with Jquery http://motyar.blogspot.com/2009/12/cmd-and-typewriter-effect-with-jquery.html
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!