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

Adding labels

In essence, a label is a name associated to a point in time in a timeline. That name can then be used to interact with that point in time. In the process of creating a label, we will visit some HTML5 features as we create an editable text layer on our HTML page(with the editablecontent parameter). We will take a deeper look into the idea of chaining methods, exploring how it's done in jQuery. Chained methods are available in TimelineLite/TimelineMax of GSAP as well. We will explore how to use anonymous functions and many other small tips. In this lesson, we will learn how to add labels into a timeline and how to jump to that point in time using the label.

Video Transcript

00:02Congratulations, congratulations.
00:06So you're in chapter five.
00:07I am beyond thrilled and I remember once I said that I'm thrilled and then someone commented
00:13that you don't look thrilled.
00:15So sorry, this is as thrilled as I'm going to look like.
00:19Okay, so this is the last chapter is about timeline max really, we're going to ease into
00:25it first talking about a couple of more stragglers, a couple of more little things that we want
00:30to talk about the timeline like mainly focused on labels, we're going to see what labels are
00:35they enable us to basically highlight a specific point in our timeline and say, Hey, let's call
00:39you a specific name.
00:41So it's easier for us and then refer back to that point in time whenever we want to.
00:45And that's exactly going to be our main topic of our first lesson.
00:48That's going to lead us into a whole conversation about how to improve our advanced editor in
00:52the next few lessons.
00:53So let's get started at five.
00:55Now what we're going to do in this chapter and really what we're going to do in the
00:58next few lessons really, is we're going to dive into even more deeply the world of timelines.
01:05And we're going to dive into initially the labels.
01:10Now labels enable us to jump to specific points in our animations whenever we want to.
01:16So I'm literally could jump into specific points and replay those specific points if
01:20I want to.
01:21A label is basically a placeholder in my animation that says, Hey, I'm going to name this point
01:26in my animation a certain name.
01:28And then I could more easily approach that point with that said, let's see how we create
01:33this logic and we're going to go back and our source files are going to be the exact
01:36same source files as we had in the end of chapter four.
01:40So in our, uh, we're just updating our chapter five and inside here we're going to have
01:44the same CSS that we're going to modify in a minute.
01:47And we have our JavaScript, which is right now the exact same file as the ending file
01:51that we had in our chapter four.
01:54So with that said, let's start working.
01:56So what I'm going to do and what I want to do is I'm going to create a label and I'm
02:00going to position it right here.
02:02That way I could change labels whenever I want to.
02:04So let's go ahead and first create that label logic.
02:08So I'm going to add here and add label logic, and I'm going to just go ahead and
02:13create that function right underneath our setup control.
02:21And again, I just put, put, put that in right after our on ready.
02:24We set up the controls or adding the labels right after that.
02:27And in our ad label logic, what I want to do is I want to basically create a new
02:32dev layer.
02:33And for that, I'm going to approach jQuery and create a new dev dev layer.
02:37Now for the first time, I'm going to show you something that is called chaining and
02:40chaining enables us in jQuery to basically append the next function that we want to the
02:46object that's returning.
02:48What's really powerful about this is that all the methods in jQuery return back the
02:54actual object itself.
02:56And that makes life really easy because I could then go ahead and add another item
03:00whenever I want to.
03:02So the next thing I want to change, let's say the properties.
03:06And I could go ahead and just set my ID and set that to the label ID.
03:12And then I'm going to go ahead and create that in a minute.
03:15And what I want to do next is I want to append it to our body.
03:20All right.
03:21You can see that instead of creating a variable and putting this in a lot of
03:24different lines, I'm basically doing the whole thing in one clump.
03:28Now I want to create this label ID before we look at it.
03:31So I'm going to go right into our CSS and I'm just going to create that label ID.
03:37And the first thing I want to do with that label ID is I want to set its
03:39position to be absolute, as I don't want it to be affected by anything.
03:46And I want it to Z index to be one, I want it to be above everything.
03:49I want it to be on the right side and I'm going to put it at five pixels
03:52to the right.
03:54And I also want to set a top position and for the top position I'm going to
03:57set that also five pixels from the top.
04:00Let's also set for it a font size and I'll set that at 18, 18 pixels.
04:07And let's set also a color and I'll set that to be white.
04:12All right.
04:13So we have ourselves a label and if we just save this, go back to our
04:16application and click on refresh, we should see that label right here.
04:20Now the next step that I want to do is I actually want to make this
04:22label editable so I could come inside here and actually put some copy in here.
04:28So to do that, what I'm going to do next is I'm going to go to my element
04:31here and in my ID, right after my ID where I'm adding in my attributes,
04:35I want to add one more HTML attribute, an HTML5 attribute by the way,
04:40which is called content editable, editable, content editable.
04:47Now this property basically turns any HTML element to be editable,
04:52which is any text and that's really, really handy.
04:55Just by doing that and saving that, I could go ahead into my application,
04:58click on refresh and now this label has become a text that I could actually
05:02go ahead and click and edit it.
05:03Now obviously this depends if your browser supports it,
05:06if it has that HTML5 support or not, if you don't, then it won't work.
05:11But we're going to work in an HTML5 format because we live in the future,
05:16we live in the now.
05:18The next step that I want to do is I want to make sure that I know
05:21when the user clicks out of that area because I'll want to add that label
05:24every time the user finishes changing a name.
05:27So to do that, I'm going to add another method and chain another method
05:31and I'm going to call it blur, which is a function that will basically trigger
05:35and a callback that will trigger whenever the user completed their action.
05:39And I'm going to go ahead here and call the on label change,
05:42in which basically it's whenever the user clicks their mouse
05:45in a different position, clicks it out of that element.
05:50So again, I'm going to create that function really quickly on label change.
05:55And what do I want to do on label change?
05:57All right, so first of all, let's test it out and see that it's working.
06:00So I'll just do a console.log and I'll just trace, whoopsie, this.text.
06:11So we'll see that label.
06:12And if I go ahead and save this, click on refresh right here,
06:17go right in there and just change something and just call this start,
06:22let's say, click out of it.
06:24We'll see that we're going to get that start popping up
06:26inside of our console, which it means that we're
06:29ready to add this logic right into our timeline.
06:33So I'm going to go to my timeline and add to the function add label.
06:37Just send in that string label that I want to give it.
06:41That's all I need to do to give a name to the specific point in time
06:44that I'm in.
06:45Now, the next step that I could do is literally,
06:47because I want to be able to actually see it in action.
06:50So once we've done that and we configured it,
06:52what I'm going to do is I'm just going to create a new label
06:55variable, because I want to basically refer to this
06:57and create a new button that will basically
06:59go to that point in our playhead, or more accurately,
07:02play from that point on in our playhead.
07:07And now that I have that text here,
07:10I want to go and create a new control.
07:12So I'm just going to copy one of the controls
07:14that we have already here.
07:16I'm just going to have to make sure that I refer back
07:19to the control bar, because I don't have a reference here,
07:21but I do have an ID.
07:23So I'll just recreate that reference when I'm sending in.
07:26So let me just make sure that I do that immediately.
07:28And I'm just going to send in that label, that ID of control
07:34bar.
07:35That way, it's going to be the right value there.
07:37All right, so we're going to give this function.
07:39We're going to call this play space,
07:41and then the name of the label that we just send into it.
07:45And I don't want to call on kill,
07:46but I'm going to create a built-in anonymous function right
07:50into my function, because what I want to do
07:52is I want to refer to the value LBL,
07:55but I don't want to start sending parameters.
07:57And I don't need to.
07:58So I'm creating an anonymous function.
08:01And what I'm going to do is tell my timeline, go to play,
08:04but play from position LBL.
08:07And literally, that's all I had to do to create a really cool
08:10controller.
08:11And it's time for us to test it out and see it in action.
08:14So if I go ahead into my browser, click on Refresh,
08:17and I'm just going to go ahead here
08:19and just give it a name.
08:20So I'm going to call this start.
08:23And if I click out, we'll see that we'll have
08:24your new play start.
08:26There's nothing in here, so let's go ahead
08:27and start adding things.
08:28So I'm going to add one item, add another item,
08:32add another item, and we're going to,
08:36let's save this position as triangle or try.
08:40And I'm going to go ahead and add another item,
08:42and I'm going to call this square,
08:45or let's call this SQ.
08:51And I'm going to go ahead and turn this into a line,
08:57and I'm going to call this line.
09:00So I just created a lot of different labels.
09:05And I could literally just go ahead and play from the start
09:08or jump to the play line or play square or play triangle
09:12or play square or play triangle.
09:14Whatever I want, I could literally just jump
09:16in my play head because now I have labels there
09:20that make it really easy to jump to specific points
09:23of interest in my animation.
09:25So then I could create whatever animation I want
09:26and literally position labels whenever I want to.
09:29Now this was a very sophisticated sample
09:31because that's what we do.
09:33And in the following few lectures,
09:35we're going to continue working with labels,
09:36but before we wrap up this topic,
09:38I just want to go through it one more time.
09:41So we created the add label logic,
09:43and when we added the label object,
09:45we said that we were chaining the calls
09:48because jQuery is always returning to me the object.
09:51So I went ahead and created an object,
09:54put inside of it some copy, set its attributes,
09:57made it editable, appended it then to the body itself,
10:00and made sure whenever the user blurs
10:02out of that specific element,
10:04they will have a trigger of on label change.
10:07Then we captured that label that we got.
10:10We send it into our timeline and defined a new label
10:13with the same exact text,
10:14and then created a new control,
10:16basically creating a play with that label,
10:19sending in an anonymous function
10:21that would then trigger the timeline play,
10:24sending in the label that was there.
10:26All right?
10:27And this, by the way, this logic,
10:29and if you're thinking,
10:30well, why does the LBL always work
10:32and it's always the right LBL?
10:34And the reason is, it's very simple.
10:36It's because the values themselves right here
10:39are captured inside of this instance itself.
10:42So the information inside of LBL
10:44is available to this function
10:46and will always be available to this function.
10:49Because this function is living inside of this scope.
10:53All right?
10:54So that's it for this lesson,
10:55and in the next lesson,
10:56we're gonna continue taking a few more looks at the label
10:59and taking it a little bit further deeper into the world
11:03of a really cool editable animation stage.

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!