Creating a typewriter effect
So far we were playing with functions as standalone elements. In this lecture, we'll create our first reusable animation. By the end of this lesson we will have a typewriting effect. Through this we will learn how to leverage TweenLite to create custom animations.
Video Transcript
00:00This is where the fun begins in this lesson toward well from here on we're only having fun in this lesson
00:07We're going to learn how to create a really cool typewriting effect
00:11So let's do it
00:12Let's jump right in now as I want to show you also the process of figuring it out
00:17And this is a really important process in general because we're going relatively quickly in this lesson
00:24We've seen how to work with the basic from to the to the from what I want to do next is I actually
00:29Want to animate nothing I want to animate
00:31Basically air and I want to be able to get that value to be able to output that value and then do something with it
00:38In our case, you know
00:39There could be so many different scenarios for this such as maybe you want to fade in or fade out music
00:45If you want to fade in fade out, maybe a video
00:48You could have a lot of scenarios where there isn't something built in and you want to just take raw data a raw number
00:55Animate that number and then do with it something whenever that number updates and that's exactly what we're gonna do in this lecture
01:02So let's get started
01:04So the first thing that I'm gonna do is I'm gonna go ahead and I'm gonna create a new object
01:08And it's just gonna be just an empty object that has nothing inside of it
01:12I can just say I'll just set a property to this object and let's say I'm gonna set a property of let's say
01:17let's say let's say
01:20This is gonna be index and I'm gonna set my index to be zero and
01:25I'm gonna set a word count and by default. I'm gonna set that to be zero as well
01:31the next step of what I really want to do is I want to go to this object and I want to go to that word count and
01:37What I really want to do is I want to fetch the actual value in and if I go right into our
01:43Application here what I really want to do is I want to fetch the value of our text here
01:47Or really I want to fetch here the value of the length of the text here now to do that
01:53What I'm gonna do is I'm gonna use the jQuery command and I'm just gonna approach our h2
01:58And then I'm just gonna ask for it for its text
02:02You'll see if I do that we're gonna get in our output the actual text that is coming out from our element
02:08By the way, we're inside of our console most modern browsers have a console we talk about consoles in our
02:15Earlier course, but we're assuming that you know how to get into a console
02:18And if not refer back to our starter course on working in JavaScript
02:24All right
02:24So now that we see that we could have access to our text
02:27Let's go ahead and what I want to do is I'm gonna go ahead here and just add this cut this content
02:33You know, I'm gonna go ahead here and just type your o dot and I'm gonna copy here
02:37I'm gonna put here our copy and I'm gonna go ahead and I'm just gonna find our h2 here
02:43And I could see that since it's dot slide h2, so I'm gonna go ahead and just do that
02:47I'm gonna use my my jQuery command
02:49I'm gonna put here a selector and ask for my in the slide itself for my h2 that way
02:54I'm making sure I'm only getting that one slide
02:57And in our case because we only have one slide there just in case if in the future
03:01We want to actually add anything in here and I'm gonna also get my text and set that text right in there
03:07If I want to I could also put a reference to our h2 so I could add a reference here to our
03:13HTML and I could just put your access to that specific object and if we're already doing this
03:19We kind of want to make it a little bit more efficient
03:21So I'm gonna go ahead here and just put that as the first property that way
03:24I don't need to call jQuery again
03:26But I could actually access that object and just paste its text into the copy area
03:32So I'm saving a little bit of calls because I'm not calling a function again
03:36I'm using the object because we already have it. Alright, so we have our object
03:40We set our HTML or our DOM element and the next phase is you know, maybe we might want to call it Dom
03:46But I'll keep it HTML then we fetched our copy and put it inside of our copy area
03:50Last but not least we really want to get our word count and to do that
03:54We're gonna have to refer to the jQuery documentation or the JavaScript documentation
04:00so in this case, you know in this case what we really want to do is we want to take our text and we want
04:04To get its length and if I do that that won't do anything
04:09So I'm gonna try and length and it's not a function. It's a property we get from the text directly our length
04:16Which is 27, you know, it's a little bit of playing around with sometimes
04:20I personally just like just typing it in the console
04:22And if I can't figure it out
04:24Then I'll go to the formal documentation and Google it up and find the actual thing that I want to use
04:29So in this case, I know it's my it's my
04:32Property of length so I could go right back into my editor and just go back into that property of word count
04:39And I'll set that word count to be Oh copy
04:43Dot length and there we go
04:45Now our word count should have the value of our count. So now that our setup and preparation is complete
04:51It's time for us to actually try to animate this value. So to do that
04:55I'm gonna go ahead and create a new tween light and I'm gonna animate to our property of
05:01Oh, we're basically gonna animate that object itself instead of animating directly the HTML element
05:07The next step after I could say how long do I want my animation to be in this case?
05:12I want it to take two seconds
05:13I'll put there two and the next is I'm gonna set what value what what values of our Oh
05:18Do I want to animate in this case?
05:20I really want to animate my index and I'm gonna set my index up to be the value of our word count
05:27So, you know, we're making it nice and complicated just so you can see the whole features here
05:32So basically what I'm doing is I'm saying to tween light. I want you to animate this Oh object
05:37I don't want this Oh object to to change its index and
05:42Reach that word count that we have that we just said it which was 27 and we're it's gonna take two seconds for it to get
05:48There now if we do that, we're not really gonna see anything
05:51And the reason we're not gonna see anything is because we're not doing anything visual
05:55so the next thing that I want to do is I'm gonna go back into my
05:59Documents and I'm gonna go into my green saw cuz I want to show you this process now
06:03I'm looking for on update because I know it exists and most chances are once you finish this course
06:09You'll know it exists, but you might forget the actual syntax
06:12So go into the documentation the design might change because you know websites change all the time
06:17But go into your tween light library into the documentation
06:21Into your show more because usually this is not one of the properties or methods of the actual tween light
06:27So it doesn't have really a home. So it's always in that introduction
06:30So if you just scroll around in the introduction, you'll find that somewhere down the line is the special properties ease and callbacks
06:38And this is exactly what we're looking for. We're looking for a callback
06:41There are no plugins required all this stuff that you could see here, which I encourage you to check out
06:46We're gonna explore many of them
06:47But not all of them and we'll find in one of these items here and one of these items here
06:52We're gonna find on update and on update is a function that should be called
06:57Well that will be called every time our animation updates or every enter frame if you're familiar with the
07:03Actions group jargon so on update what I'm gonna do is I'm gonna go right back into my editor
07:09And I'm gonna add that extra property into our animation and on update
07:14I want to call a function and I'll just call it on
07:17type in
07:19Update and you guessed it what this function is going to do once we're done is it's gonna update our copy
07:25each time the update happens, so I'm gonna go ahead here and just create that function really quickly and
07:31What I'm gonna do next is I'm just gonna console log it because I want to see the value
07:36So I'm gonna approach our Oh and I'm gonna see that I'm gonna have a problem because I don't have any access to that
07:41Oh, so to be able to solve that problem
07:44What I'm gonna go ahead and do is I'm gonna talk about another something really massive, which is called this
07:50Now JavaScript is an object oriented programming language, although many developers just treat it as just functions
07:57but
07:59Literally in this case I would love to be able to just say hey
08:03I want to talk to myself
08:05So I have a few options of doing this and we're gonna do the easy way this time around and next time around
08:11We're gonna look at a little bit more sophisticated way of doing this
08:14So it with my on update if we go into the our documentation again
08:18We'll see that there's also on update parameters, which needs to be an array
08:23So I'm gonna go ahead and add that on a parameters because that's gonna enable me to send in extra information
08:29to this on
08:30On type in update now, obviously if I wanted to I could have gone and created that Oh and set it on the global scope
08:38And I'm gonna ask you a question
08:40Why would it be a bad idea for me to just create this object in the global scope?
08:45And if you don't know the answer the answer is very simple
08:50Although right now we're creating an animation that is very specific to a one-time off
08:56But in reality what's gonna happen is many times we're gonna want to create animations
09:00They'll maybe want to use the same animation here and also here as well
09:04So we want to be able to create basically a logic that doesn't need to be saved in the global scope
09:10Beyond that if we don't save our animation information in the global scope
09:14There's a lower probability of us overriding something that we shouldn't be overriding
09:20So if you go back into our editor and we're gonna talk more about that as we progress in our chapters
09:25But what I want to do is I want to use that function that we just saw there that property
09:30Sorry that on update params which enables me to send inside of an array and in that array
09:37I could put properties that I want to send through so in this case
09:40I want to send through our object that we just created and I'll just go ahead here and I'll just just type here our
09:46Oh or
09:48Our target object and our target object we could go ahead and just log out our target object index
09:57Which would give us the current index, but we'll have a small problem, which we'll fix immediately. So if I save this
10:04Go right back there into my browser click on refresh look at the output panel
10:09We're gonna see that the numbers are growing exactly the way we would hope for them
10:12the only problem we have is that we're having all this extra information that we don't want and to
10:17Solve that extra information problem. What I'm gonna do is I'm just gonna
10:21Create here a separate variable here and I'll just call this index and I'll just save this index as
10:27The parse int which will basically take whatever is inside of your and turn it into an integer
10:33Which will basically concatenate the value or get rid of everything after the dot now
10:38If you don't like the word parse in you could use the math floor
10:41Which will do basically the same thing and it's basically just gonna take whatever number we have and get rid of everything
10:47After the dot that way when we're tracing out in our output, we could then basically see
10:52That our information is basically basically basically
10:56If I go here and click on refresh
10:59Is gonna come without those extra baggage and now that we don't have this extra baggage
11:04we could go ahead and start animating our copy by literally going ahead and
11:10Updating our copy each round
11:12So to update our copies round because we have access to the HTML now that we have our target object coming through
11:17We saved it in the HTML property
11:20We could go ahead and update the text itself and change the text and out and paste into the text
11:27The value that we want based on the current index that we're in
11:30so literally all we have to do is just take our old copy and
11:35I'm just gonna scroll a little bit down so you can see it better and I could slice this copy saying here
11:39I want everything from index 0 all the way to the current index that we're in and
11:45That's all I had to do to create and although it's a lot of steps and and it's fine if this is a little bit complicated
11:50I urge you to just watch this again or try to do it yourself even better
11:54So if we save this and just go back into our application
11:58We should see now that our oh and we have your little reference problem
12:01So we're gonna first fix that because we have your O is not defined and that's absolutely true
12:05There is absolutely no O here. O was only an up there our name of our object inside of our on type in
12:13update is
12:19Target object
12:20So if I save that go back into my application and click on refresh
12:23We should see now that programming is better and it's flowing beautifully into its position and we could very easily
12:32Reuse the same logic to create animation and something that is way more complicated
12:36now because we didn't use direct names and on on type in update really the only thing that we have to change if we want to
12:43Animate something else is our HTML element. So in this case if I wanted to I could say hey instead of that
12:48Let's just use this book with slide this
12:51Paragraph and I could literally just send the book with paragraph in instead of sending inside that
12:57H2 item and just by doing that small little change
13:00I could go ahead and we'll see that now this item is animating instead of our title now
13:05Obviously, although this is wonderful
13:07We still have a small problem and our small problem is is because we're working in this construct
13:14We don't have a way to reuse this twice because if we reuse this twice
13:18we have to basically duplicate this whole thing again now my homework for you is
13:23Solve this problem before you move on to the next lecture because in the next lecture what I'm going to do is I'm going
13:29To solve that problem by having this animate separately and this animate separately using the same code
13:35And I'll give you a hint. You're gonna probably want to use a function. That's the easiest way to solve that problem
13:40So see you in the next lecture where we actually will solve this problem because I really want to animate both the top section and the
13:46bottom section
