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

Delaying animations

One of the most important things in online animations is timing. Many animations are too fast, too slow or overwhelm with too many things happening at the same time. In this lesson we're introduced to delays. Delays enable us to sequence animations and control animations, creating animations that make sense.

Video Transcript

00:02Timing is a really important thing. If you create animations and you don't have a good timing in them,
00:11there's just not going to be a good animation, even if they look really good.
00:15So, timing is so critical, and lucky for us, there's a really helpful function, or property,
00:21which is called delay, that enables us to basically delay our animation
00:25and find that perfect spot for animation to make it smooth, clean, beautiful, and fun.
00:32And that's exactly what we're going to do now. We will have better timing than this intro.
00:38This next lecture is going to be very short and very sweet.
00:44Now, you could always find all these stuff also inside of the documentation.
00:49Just find them. In our case, we're going to look at ease right now.
00:52And really with the ease command, it's... Oh, not ease, actually.
00:56We're starting with delay. Next lecture is going to be about ease.
00:58Or how about we do both in one lecture, because delay is very, very easy.
01:03So, in general, and I just want to talk about one thing about delays.
01:06In general, so far, we talked about our property.
01:09And if I jump in here, our time property as seconds and always as seconds.
01:14But really, it could be able to approach and access as frames,
01:18more commonly used in Flash than it is in JavaScript.
01:22I'm not going to talk about this much about working with frames,
01:25because it seems like time is the most logical way for us to work.
01:28It's the most intuitive way to work.
01:30But there is a way for you to work also with frames.
01:32If you like that idea, dig deeper into it.
01:35But we're going to work assuming that everything is time.
01:37The reason why I'm saying that is because I wanted to say that the delays
01:40are also going to be in seconds.
01:42But if we were working with a frame based animation and not seconds,
01:46then we would treat this number as frames instead of as seconds.
01:50In our case, we're treating everything as seconds.
01:52So, let's go ahead and add delays into our animation.
01:55So, first of all, I want to start off with our first animation,
01:58which was our from two.
01:59And all I have to do is just add this extra property of the delay
02:03to where do I want to actually start my, oh, not in the from two.
02:07I can actually probably I'd want to do this.
02:10Let's start with our typing effect. How about that?
02:12So, I'm going to add here another property, which will be the delay.
02:15Now, the delay property, I'm sorry, the delay property.
02:19Now, the delay by default, I could put here a default value
02:22if I don't want to put anything inside.
02:24So, I'll set a default value of no delay.
02:26And that's fine.
02:27You could always assign a value.
02:29Even if you assign a value of zero, that's fine.
02:32That will just mean there won't be a delay.
02:34And if I just set here delay, I could just go ahead
02:37and just type that delay variable that's coming from outside.
02:40And by the way, if this looks weird that I'm typing here,
02:43delay, delay, it's not weird.
02:46It's completely fine.
02:47This is not a variable.
02:49It's a property that belongs to this object.
02:51So, it is kind of a variable, but it's not competing in the same scope.
02:56Now, we haven't really talked much about scope,
02:59but it's something that's probably really important
03:01and as we go on throughout this course and future courses,
03:04and if you check out our object-oriented courses,
03:07scope really refers to who does this variable belong.
03:11So, this variable belongs to this function, right?
03:14But this variable does not belong to this function.
03:17It actually belongs to this object that we're creating.
03:20And that is why this knows who it belongs to.
03:23It belongs to this.
03:25Well, the delay knows it belongs to the function,
03:28so we don't have your conflict, all right?
03:30So, that's really in a nutshell why I could use delay, delay twice.
03:34Now, if it's confusing to you, then just use a different name
03:37so you don't get confused by it.
03:39All right. So, if I save this, all I have to do is send that extra parameter.
03:43So, I'll put here a parameter of a one-second delay here,
03:46and I'll put a parameter, let's say, of, let's say,
03:49we'll have your three-second delay.
03:50So, like that, this will have enough time to almost finish.
03:53If we go back and run our application just one more time
03:56and click on Refresh,
03:58we could see that our time is going to start typing in.
04:02This one is going to start typing in.
04:03And we definitely have a small problem,
04:05and our small problem right now is that because we added a delay,
04:09the animation is only actually showing us what's actually going on
04:13after the animation should have been going on, right?
04:17So, we should have started with nothing originally.
04:19So, one way that we could solve this is go to our code,
04:23enter our typing effect, or when we're creating this typing effect,
04:27and after we start our function,
04:28just call in our on type update and just send the information one more time.
04:33And another mechanism is a different mechanism
04:37that will basically enable us to basically say,
04:40hey, render the first frame no matter what immediately.
04:43So, that way there is, even if there's a delay,
04:46render that initial frame immediately.
04:49Now, the way we could do that, and let me show you the easy way first,
04:52you know, we'll show you the easy way first this time around
04:55by just calling that function one more time.
04:57That would be the easy way for us to solve that problem.
05:00If we go here and click on Refresh,
05:02we'll see that we'll start with the nothing
05:04because we called that initiation function.
05:07Now, another way around this is actually going back into my tween light
05:11and saying to it, hey, I want you to refresh immediately.
05:15Now, by default, and if I go back inside of my documentation,
05:20again, a lot of these things you don't have to memorize,
05:23it's called immediate render.
05:25Now, by default, immediate render happens when you set a from animation.
05:30And the reason is very simple,
05:32is because usually when you want to animate from somewhere,
05:35that means that you want to actually start from a different place.
05:39So, automatically, even if there is a delay,
05:41even if it's paused, it will first render the first frame
05:45or the first instance of that animation.
05:48Contrary to that, when we do a to animation,
05:51usually we're already in the place we want to be.
05:54So, our default for immediate render is false.
05:58But if we want to force it for our to,
06:01I could go ahead to immediate render
06:03and literally just set that as an extra property
06:06of the to function of this to animation
06:09and just go ahead and say, hey, I want you to immediately render.
06:12I want you to immediately render right now.
06:14This will make sure that even though there is a delay,
06:17we're going to first render the element once
06:20and then start our delay.
06:22So, if I save this and go back into my application
06:24and click on refresh,
06:25we'll see without needing to call the function again,
06:27it's going to set that initial rendering delay.
06:31Now, this is a perfect example of a scenario
06:33where this would be useful.
06:35In general, you just need to know that it exists,
06:38like you do right now.
06:39And then if you ever have a scenario where you need it,
06:42you'll know about it.
06:43You don't have to memorize it
06:44because you could always go to the documentation
06:46and find it and find that immediate render.
06:49Now, before we wrap this up, I want to solve a problem.
06:53And our problem is, is I really want to have a delay
06:57in our from to as well.
06:59To do that, how about we go to the documentation
07:01and find out how to do it.
07:03So, in this case, because it's the from to,
07:05I could go ahead and just find that method.
07:07So, I'm going to go here and find the method from to,
07:10completely blind right now.
07:15Here we go, from to.
07:16And it will load up the from to.
07:19And we'll see inside of our from to.
07:21Where are we?
07:23Oh, here we go, from to.
07:25So, we can see that first of all, we have our from to.
07:28Everything here seems to be, you know what,
07:31and it's kind of funny because I don't really know
07:38how do we add it in because I just thought about it
07:41and then I decided I wouldn't do it.
07:43And I could see that it's not even written
07:45in the documentation currently.
07:47So, I'm going to go ahead and try to solve it myself
07:49with the knowledge that we have already of delays.
07:52What if we just add the delay to the first property?
07:55Let's see what would happen.
07:57Excuse me.
07:59So, I just put here a slight delay.
08:01I'll just put here a delay of, let's say,
08:03three seconds and let's see what happens.
08:05If I go back and refresh, so that didn't do anything.
08:09So, I'm going to go ahead and try and put that now
08:11in the secondary item.
08:13And that did our trick.
08:24So, you can see sometimes it's really about just
08:26figuring it out.
08:27So, obviously we knew that delays work
08:29for the way that they work.
08:31We couldn't find it in the documentation.
08:33I didn't know how to do it.
08:34So, I just said, hey, let's figure it out together.
08:36So, it didn't work inside of the first item
08:38I tried in the next item.
08:39And sometimes if you can't figure it out,
08:41you could always go and ask in our comments section.
08:43I'm sure some of our students would answer,
08:45or myself, or go into the Green Sock forum
08:48and jump away and ask if it's something
08:50that you can't find in the documentation itself.
08:53But for the most part, you have your documentation
08:56and then just experiment.
08:57If you could see that you can't find it,
08:59or it's never a bad idea to send an email out to Jack
09:02or Carl, one of the guys from Green Sock,
09:05and say, hey, I have a great comment
09:07that would be worth adding into the documentation.
09:09You could always post that in their forum.
09:11For example, this type of scenario,
09:13I think would be a great kind of comment
09:15to say how do you do delays with the from to.
09:18All right, so that's it.
09:20We're done with setting up and configuring our delays.
09:22In our next lecture, before we wrap up this chapter,
09:25I want to talk really quickly about Eases.

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!