MooTools Typewriter Effect Plugin Upgrade

By  on  

Last week I shared my MooTools Typewriter Class with you. It was pretty well received and I got a few feature requests that I've implemented including "backspacing" and character variance delays. I'm not going to explain the old code, so click here to get the full explanation on the class.

MooTools 1.2 Class

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

	//options
	options: {
		container: $$('body')[0],
		message: '',
		delay: 150,
		cursor: 0,
		variance: 0,
		backChar: '|',
		backDelay: 30
	},
	
	//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++)
		{
			var pace = (this.options.delay * x) + $random(0,this.options.variance);
			var current = this.options.message.charAt(x);
			
			//spit out the letter
			if(current != this.options.backChar)
			{
				var go = this.setLetter.delay(pace,this);
			}
			else
			{
				var go = this.deleteLetter.delay(pace + this.options.backDelay,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++;
	},
	
	//deletes a letters -- goes backward
	deleteLetter: function() {
		
		this.options.container.set('html',this.options.container.get('html').substr(0,this.options.container.get('html').length - 1));
		
		//increment cursor
		this.options.cursor++;
	}
	
});

MooTools Usage

var t = new Typewriter({
	container: $('type-here'),
	message: 'This is output from the Typewriter class. This class is small but packs a really good|||||||||||SUPER HUGE punch. Isn\'t MooTools great?',
	delay:90,
	variance:200,
	backChar: '|',
	backDelay:30
}).start();

You'll see three new options:

  • variance: No one types at the same speed for every letter. Variance allows for random letter delay changes.
  • backChar: Which character signifies a backspace?
  • backDelay: How quickly should the typewriter delete?

Click here to view the new version.

Recent Features

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

Incredible Demos

  • By
    Select Dropdowns, MooTools, and CSS Print

    I know I've harped on this over and over again but it's important to enhance pages for print. You can do some things using simple CSS but today's post features MooTools and jQuery. We'll be taking the options of a SELECT element and generating...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Discussion

  1. Catar4x

    It works with the mootools 1.2dev version ?
    I will try this upgrade but the first doesn’t works.
    It’s perhaps my other scripts that fail the implementation of the code.

  2. Keep in mind that so much setTimeouts slows down everything. Maybe you should try creating an Fx with a transition to make that variance effect, that way you’ll use one setInterval instead of 100+ setTimeouts simultaneously.

    Other than that, very nice thingy!

  3. Would be nice if it could loop through x amount strings and continue looping.

    Is .periodical() the only way this can be done since since time cannot be calculated due to the speed variance?

    Surely someone knows a better way to loop function simultaneously after completion?

    I will have a go at it myself.

  4. One idea I have would be to use the existing text in the container, for instance rather than having to use the “message:” option in javascript, I would like to have the text already in the container.

  5. Yeah I have done that. All the text is already in the container and hidden. And I have added a small bit of code that then splits the text into an array and removes the hidden style/class.

    I’m just having trouble getting it to loop.

  6. OK I have finally managed to modify this script so that it can rotate through multiple messages.

    I took Leo Newballs comment on-board and fi there’s already a string in the container you specify in the options, it overrides the default message with this string and splits it into an array.

    If backChar is specified in the options, It randomly injects a character, or a block of characters(major typo) and cuts it into a random position within your string so it looks like authentic typing (i hope) and then deletes them.

    Does anyone fancy testing it?

  7. I love testing new items! I would love to give it a go!

  8. How will i send the code? Shall I just post it on here?

  9. Send you a message through your contact page Liam :-D

  10. tamamma

    Hi there, first of all thank you for everything. This website is very useful.
    I would like to know if there is the possibility to insert any html tag for a part of the text of the typewriter(for example bold or ecc) it would be very nice!!
    Thank you again

  11. @tamamma: Adding HTML in the message is not possible since the typewriter goes character by character. You would want to create something more advanced.

  12. Steve

    Firstly I’d like to echo the “thank you for everything”, this is perfect for what i’m working on except I need to be able to insert line breaks, any ideas on a simple way to go about this?

  13. @Steve: Sorry, this wasn’t set up for that. You could, however, add a special charachter into your string (like a “*”), and in the start() or setLetter() methods check for, and add a “
    ” instead of the special character.

  14. Same answer as the previous question.

    @tamamma: Adding HTML in the message
    is not possible since the typewriter
    goes character by character. You would
    want to create something more
    advanced.

  15. chris

    Is there any way to reset the text on an event? i.e. ‘mouseleave’

    I need it to function in a similar way to the Fx chain property –
    e.g. chain:cancel etc.

    At the moment I have the text appearing under each icon on a menu when ‘mouseenter’ occurs, with a simple element level text reset on ‘mouseleave’. However this causes some odd results when the Typewriter effect hasn’t finished ‘typing’ before the mouse moves off and then back on again, or onto another!

    Great work btw.

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