Animations need order - creating a Que
In the last lecture we learned you to create our own original animation using JavaScript. In this lecture we will start converting our structure to enable us to que content. We will create the basic structure needing to complete this task in the next lecture.
Keynotes:
- Variable scope.
- Function scope.
- Creating a Que.
Video Transcript
00:00the last lecture we created a typewriter effect and in this one we're
00:04in ready to start using that effect in a more global scape which means that we
00:10need to make a few changes to it starting with triggering the animation
00:14only whenever it's ready to animate to do that we're gonna start by creating a
00:18cue for our animations we're not gonna trigger them yet because we still need
00:22to revisit our our animation and update it to let us know whenever it's
00:27animating which we'll do in the next lecture but in this one we're gonna go
00:30ahead and define a variable inside of a scope and kind of get a better
00:35understanding to variable scopes and functions and create that basic cue by
00:39the end of this lecture we'll have all the tools we need to cue up our
00:43animations but not yet trigger them so let's jump right into it and let's start
00:47creating a cue in the last section last lecture really we did something really
00:52amazing we created an animation but not only that
00:56towards the end of the lecture we updated the logic inside there to make
01:01sure that our text will actually receive the actual object that is going to
01:05manipulate that was a really big step by doing that we made sure that this
01:10text and title could be reused as many times as we want to literally we could
01:15just go ahead here and just duplicate this line and I'm gonna go ahead and say
01:19what do else do I want to animate well I want to animate this line which is
01:23actually inside of our talk ID talk there is a span which I want to animate
01:28I'm gonna go ahead here and just add that right in there and I'm gonna also
01:33make sure that I'm gonna call that span within and all that's left for me to do
01:39is also update our text to reflect what text we want it to work with and if
01:43I go ahead here and save this and click here on refresh we'll see that right
01:46away we're gonna have a really cool animation going on now this is a
01:50problem it's not gonna there's a few issues here that we have to address one
01:53by one the first problem is that both animations are happening in the exact
01:58same point in time and we don't really know when the first animation finishes
02:03and when the other begins because we don't really have here a predefined time
02:07we although calculated by going inside and counting the characters duplicating by
02:12amount a milliseconds it takes for each character to go up but that would be a
02:16little bit silly to do them we really want to create maybe some sort of a
02:19logic system that will enable us to know when an item is done to push in the next
02:25item without us needing to worry about it once we create it to do that I'm gonna
02:30go ahead and create a few new functions the first function I'm gonna do is I'm
02:34gonna create a function that it's called q text I'm not gonna place it outside of
02:39our init function as I've done previously because I really want to have
02:44a usage of a few variables that I'm gonna define inside of that init
02:47function so what I'm gonna do is I'm gonna go ahead and just break a line
02:52inside of my init function and create another function right inside of it and
02:57call it q text by doing that I'm literally enabling me to basically go
03:02ahead and I'm gonna just send that text object as the parameter what that's
03:07gonna do is again able me to reference variables that are sitting inside of here
03:11and easily approach them because I want to go ahead and create two new
03:15variables the first variable that I'm going to be creating I'm gonna call tq
03:20and it's gonna start stand for text q and it's gonna be an array so I'm gonna
03:25go ahead and just set it and configure it the next variable I'm gonna call is
03:29star is started and by default it's gonna be false now that I have it false I
03:36have my tq let's go ahead and update the logic here and text in q and
03:40instead of that I'm gonna go ahead and just call the q text because all our
03:44text elements are really doing the same thing we're taking the content from
03:48within them we're wiping them out and then we're creating an animation and I
03:52really want to make sure that it's going in a queue that only one by one
03:56these animations will actually take place and if I go ahead here and I'm
03:59just gonna copy these lines and delete them and I replace them with basically
04:04just sending in the items that I want to animate I could even make it more
04:08simple by just sending inside of here just a character and then I could
04:12actually go ahead and convert it once I'm inside there so let's go into that
04:16function so first of all I could go to that text and immediately just wrap it
04:20around in a jQuery object to turn it into a jQuery object beautiful now I
04:25have the text item the next thing I want to do is I want to store inside of
04:29a queue if this is not the first item so I'm gonna go ahead here and ask if
04:34is started if it is started then we're gonna do some them something else right
04:39if it's not started then we're gonna do the beginning phase which the beginning
04:43phase is really really similar to basically what we were doing so far but
04:47instead of that we're gonna go ahead and just take that text item and send it
04:51as the parameter and we're gonna send inside that text instead of calling it
04:56again and recreating it so already we're improving our performance a bit so
05:00that should cover our first item and if I go ahead and save this and click
05:04here on refresh and then we should see our first item animating second
05:07item oh I animating as well why is it animating because we never
05:11configured that is start so I'm gonna go ahead here and set here is start equals
05:15true so once this else happens and it should only happen once then it should be
05:19true and if I go ahead here and click on refresh we should see only the first
05:22item should animate which is what we want to happen beautiful the next step
05:27for us is we want to make sure that for all other items we want to make sure
05:30that we're basically adding them into our queue when we created a variable
05:35called TQ then I'm gonna go ahead and leverage a TQ and push inside of it the
05:40items that I want to basically approach so I'm gonna go ahead here and set my
05:43text to be this text object we've just created and I'm gonna let me start off by
05:50just sending in that text item no extra parameters no extra logic involved just
05:55pushing it in so we have a reference to that item the next step for me to be
06:00able to continue down the path is to know when our text in title completes
06:04right now we don't know when it completes so all we have is the first
06:08animation going on so what we're gonna do in the next lecture is move on to the
06:12next function that we need to create and go ahead and also update and optimize
06:16our text in title to enable it to basically from the present and promote
06:21whenever an animation completes so we'll do that in the next lecture in this
06:28lecture we created a queue for animations but we're definitely not
06:31done because we still need to go ahead and add a callback into each
06:35animation to let us know when to trigger the next animation in the queue and
06:39that's what we're gonna do in the next lecture
