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
    How to Create a RetroPie on Raspberry Pi &#8211; Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Create a Photo Stack Effect with Pure CSS Animations or MooTools

    My favorite technological piece of Google Plus is its image upload and display handling.  You can drag the images from your OS right into a browser's DIV element, the images upload right before your eyes, and the albums page displays a sexy photo deck animation...

  • By
    MooTools onLoad SmoothScrolling

    SmoothScroll is a fantastic MooTools plugin but smooth scrolling only occurs when the anchor is on the same page. Making SmoothScroll work across pages is as easy as a few extra line of MooTools and a querystring variable. The MooTools / PHP Of course, this is a...

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!