02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Creating a typewriter effect

Animations are in the heart of the modern web. Creating smooth and engaging animations uplift every design. In this lecture we will learn how to create a very simple and cool animation: a type writing effect. We will do it on our main text title.

Keynotes:

  • Updating text with jQuery.
  • Timing an animation.
  • Avoiding global variables.

Video Transcript

00:00In the last lecture, we started working with jQuery.
00:03And in this one, we're gonna continue working with it
00:06as we create a typewriter effect.
00:09We're gonna learn how to update text using jQuery,
00:12we're gonna learn how to time animations,
00:14and we're also gonna learn about the process
00:16of the best practice of avoiding global variables.
00:20So let's jump right into it
00:21and let's start creating a typewriter effect.
00:24I wanna move on to another level
00:27of things you could do with JavaScript.
00:28So we saw in the last lecture
00:30how we could basically change the environment of the user
00:33subtly maybe for the user
00:34where the user wouldn't even notice it.
00:36I wanna move on a little bit into handmade-picked animations
00:40that you could create.
00:41In this case, I really wanna create an animation
00:43that would walk through the web visualizations,
00:46typing it in character by character.
00:49Let's go ahead and do it.
00:50It's actually not that complicated.
00:52What I'm gonna do is I'm gonna create a recursive function
00:54that is using a set interval to make it happen.
00:58To do it, I'm gonna go ahead and create a new function,
01:01and I'm gonna call this function.
01:02I'm just gonna go ahead and write before our update canvas.
01:05I'm gonna go ahead and create a function called text in title.
01:10And it's not, well, it's a kind of pun
01:11because it's both the text that's in the title,
01:13but also to update the title.
01:15Now, the idea I'm gonna go ahead and send two parameters,
01:18and the idea behind it is also to reduce the footprint
01:21of how much we have to manipulate the item itself.
01:25I'm gonna go ahead and set the first item to be current,
01:27which will be the current text
01:28that's inside of our article,
01:30and the next is gonna be our leftovers.
01:33And the leftovers are literally what's left
01:35to put inside of the document.
01:37Okay, now that we have the premise set up,
01:40I wanna go ahead and go inside of our init function,
01:43and I'm gonna do this before the canvas even,
01:46and I'm gonna go ahead and call the text in title.
01:51And I'm gonna go ahead and send the first parameters empty,
01:53saying that we're gonna start from completely nothing.
01:56The next parameter that I really wanna send in
01:58is the current value that is living right now
02:01within our text.
02:02To do that, I'm gonna go ahead and pick my title,
02:05which is our web visualization,
02:07which is an h1 that is sitting inside of a div
02:10with a class of article.
02:12With that information bestowed upon me,
02:14I'm gonna go inside of here,
02:15and I'm gonna go and call that specific item,
02:18literally by calling the jQuery function
02:21and asking for the article,
02:24article, and the article, I want the h1.
02:27Now, if you're very familiar,
02:29well, if you're familiar enough with CSS,
02:31literally everything you put inside of here
02:33is CSS selectors.
02:35Just like when we appended our head,
02:36we put here literally a CSS rule.
02:39So as long as you know CSS rules,
02:41you could approach everything,
02:42and it makes life really easy,
02:44and you don't have to know the intricacies
02:46of working with the JavaScript DOM,
02:48which is not as commonly used today as it was maybe
02:51in the earlier days of the web.
02:53So now that we have our object,
02:54and it's wrapped up in our jQuery object,
02:57I could literally go ahead and call the text function
02:59method, which we are gonna be using
03:01also throughout the text and title function,
03:03and that's gonna return to us the current value,
03:06and I'm gonna append that into our leftovers
03:08because that's really gonna be what we're gonna feed into
03:11to create our content.
03:13Okay, so now that we have that logic set in place
03:16and we know that currently we have nothing,
03:18and our leftovers is the content within there,
03:20I'm gonna go ahead here and literally first step
03:23is I'm gonna go ahead and actually update our current
03:26to also have inside of it the first character
03:29inside of leftovers.
03:30To do that, I'm gonna call the slice function,
03:32and I'm gonna slice out from position zero one character,
03:36making sure that I'm adding into the current variable
03:40the one character out of our leftover,
03:44which means for our leftover,
03:45I'm gonna have to go over to our leftovers,
03:47and I'm gonna have to reset that leftovers
03:49to only contain our leftovers
03:52sliced up from position one and onwards,
03:56making sure every time I'm calling this,
03:57I'm moving one character from the leftovers into the current.
04:01Okay, now that we have that set,
04:03all that's left for me to do is to go ahead here
04:05and call our article again dot article and h1
04:10and go to call that text method
04:12and send inside the current value that we want it to be.
04:16All that's left for us to do again next
04:18is to go ahead and call this recursively,
04:21but even before we do that,
04:22let's go ahead and test things out.
04:23We should see only one character, the W in our copy,
04:27as soon as we load, and that's exactly what we're seeing.
04:30Beautiful.
04:31So all that's left for me to do
04:32is to go ahead and call this text in title over and over,
04:35and I'm gonna go ahead and do that.
04:37I'm gonna ask here if our leftovers,
04:40if we have a leftover, if there is a length to it,
04:43if it's more than zero, good.
04:45If it's more than zero, I'm gonna create a set timeout,
04:48and that set timeout is gonna call this text in title,
04:53and we're gonna call it every 120 milliseconds,
04:56and I'm gonna send its parameters,
04:58which was the current and the leftovers,
05:00literally just re-echoing what was happening previously.
05:04If I go ahead and save this and click on refresh,
05:06we'll see that we have your everything working
05:09and our animation is working.
05:10Now, before I wrap this video up,
05:12I wanna talk about a few things
05:13that are a little bit out of scope, but we have a minute.
05:16Now, in general, we don't wanna use global variables,
05:20and if you notice, I didn't create any global variables,
05:23nor did I add any inside of our init function,
05:26because what I'm doing here is I'm passing those variables
05:29inside of our text in title, creating them then,
05:32and then passing them through each time,
05:34making sure that I don't need another parameter,
05:36another variable that is global,
05:38because if I wouldn't do that in any other case, I would.
05:42There's a lot of techniques and advanced techniques
05:44of how to work with JavaScript,
05:46and I urge you to check out my title
05:48with Packet and a few of them
05:49that focus on JavaScript performance,
05:52Java object-oriented programming, and many other titles
05:55that would give you a lot of tools
05:56in creating much better JavaScript in general.
05:59With that said, I wanna also wrap this up
06:01by making this a little bit better
06:03if we're talking about performance.
06:05Right now, we're calling this function
06:08that is triggering and getting us that our object
06:10each time we call text and title.
06:12Not that big of a deal,
06:14because we're not doing that many things.
06:16But if we wanna improve our performance even further,
06:19we could go ahead and instead of sending in our object twice,
06:23I could go ahead here and send that object
06:24as one more parameter.
06:26And it's fine in this case that I'm doing it twice
06:29because it's only happening once in this line.
06:31If I really wanted to make performance better,
06:33I'd maybe create a variable here and send it twice.
06:36So it's really a question of how deeply do you wanna
06:39improve the level of performance.
06:41Every time you call a function,
06:42you're hindering a little bit the performance.
06:45Once I've done that, I could go ahead
06:47and set here my object, my text, my text object,
06:51and I could go ahead here and instead of calling this item,
06:54I'm just gonna call the text item.
06:56And when I'm calling my timer, I could send this text.
06:59Now, by doing this, what I've done
07:00is I made my text item dynamic.
07:03This is gonna make it really powerful and easy for me
07:06in the next lecture to continue down the path
07:09of saying, hey, I wanna animate more things.
07:11So in this case, once we're done with this animation,
07:13I really wanna animate this item as well.
07:15And we're gonna take a look at that in the next lecture.
07:21In this lecture, we created a typewriter effect,
07:23but it was very specific in a way
07:25because it wasn't really, we can still use it
07:27and have it run a few animations,
07:29but we need some sort of order,
07:31something that we'll be able to basically control
07:33when an animation starts, if we wanted to start
07:36only when animation completes,
07:38which is really what we want.
07:39And that we'll start covering in the next lecture
07:42as we start building a queue for animations.
07:44We won't complete it in the next lecture,
07:46but we're gonna start off building up a queue
07:48and then we'll complete it the next lecture after that.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!