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

Reusing the typewriter effect

Coding is about creating reusable elements. Recording isn't about solving a specific problem as much as it's about creating solutions that could be reused. In this lesson we're going to go back to our previous example and make it reusable (for more than one animation). By the end of this lesson you'll have a good understanding on how to reuse code when animating.

Video Transcript

00:00In the last lesson, we created a typewriting tool, but it was only useful for that one single scenario.
00:07What if we wanted to create this mechanism that we could reuse not only in this project, but also in future projects?
00:13Or maybe, in our case, we also want to animate the headline, and also the paragraph, the body that was right underneath it.
00:19And that's exactly what we're going to do in this lesson.
00:21We're going to leverage our knowledge about functions, and we're going to see how could we reuse our code to make it basically work for us,
00:28needing to do the same thing over and over.
00:30We saw how to create a typing effect.
00:32What I really want to do is make it reusable and step us closer into the world of object-oriented programming.
00:39Now, before we do that, what I want to do is I'm going to take this animation and try to bundle it up inside of a function,
00:46because if we put it into a function, we can then reuse that function, which is already better than having it as a one-time off code.
00:53So I'm going to go ahead and go right into our code, and I'm just going to go ahead and create a new function.
00:58I'm going to call it typing effect.
01:01And typing effect, let's figure out the properties in a second here.
01:06What I'm going to do is I'm just going to grab everything here, cut it out, put it inside of typing effect.
01:11So this is going to be the function typing effect.
01:14Now, the function typing effect will probably just need to take, first of all, at least a basic one parameter,
01:20and the basic one parameter that it's going to need is what it's animating.
01:24So let's set this to be the selector.
01:27So I'm going to call it selector, and I'm just going to go ahead here and just cut this out, put it right there,
01:35and instead of that, send it into our selector.
01:38So literally, all we did is just took our logic, moved it into a separate function, and then called our function.
01:47Now, literally, that's really all we had to do to make this a little bit more dynamic.
01:51And if we run this one more time, everything should continue running.
01:54But not only should it continue running, but now we could also just go ahead and then create two animations.
02:00So we could go ahead and now go back to that initial item, which was our slide.h2, which will refer to our h2 right here.
02:12And by just doing that, we should now have both items animating at the same time and completing in the exact same time.
02:19Now, obviously, this isn't really working that well from a timing perspective.
02:24So how about we make also the timing of our animation a little bit more dynamic?
02:28So to do that, I'm just going to go ahead and add here the actual time of our animation as one of the properties.
02:36And then we could just send the time, that default time or play around with that default time just by setting that and exposing it so we could actually play with that.
02:47And really, really, those are the two critical parameters that we probably want to capture by default.
02:52So if we go here and we'll just set, maybe it'll be three seconds for our top element and maybe we'll have five seconds for our paragraph.
03:00And if we go ahead and click on refresh again, we should see that things are running much more smooth than they did before.
03:07Now, obviously, one of the problems that we have here right now is that if we're looking at this animation, we could see that our refresh rate isn't as large as we would hope for it to be because we're basically getting this kind of like almost a fluster of typing, but it's still a nice effect.
03:27And this is definitely an effect that could get approach people's eyes.
03:31Now, the only problem really that we have right now, the big problem is that they're both animating at the same time.
03:36All these three elements are animating at the three time, which is making it really hard for the eye to figure out where to go.
03:43In general, in animation, you always want to have one focal point.
03:47So even if there's a few things that are animating, it's best that only subtle things will be animating at the same time.
03:54But in this case, we have four massive things animating at the same time.
03:58They're driving our eyes to all these items at the same time, and we don't have a focal point.
04:03So it looks very messy.
04:05In the next lesson, what we're going to do is we're going to learn about delays.
04:08And if we worked in our earlier course, then you've seen delays already.
04:12But still, we're going to take the delays and figure out a way to make these animations just a tad bit better.

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!