Clipping images
In the last chapter we discovered TimelineLite, and in many ways, focused exclusively on the TimelineLite.add method. In this chapter, we take a deeper dive into the world of timelines. We'll explore the various ways of adding animations into a timeline, from creating the animations and sending them to a timeline, through using the timeline methods to create tweens. In this lesson, we will set up our JavaScript stage by adding eight images of Bella the dog and cropping them to partially show. We will use the CSS clip property.
Video Transcript
00:00Before we jump into the bowels of Timeline Light and learn how to basically work with almost all the most critical functions of it to add and elements and all the different ways of adding it, which is the main topic of our chapter.
00:14We want to jump first and set up our stage and figure out how we're going to configure our CSS, how we're going to configure our basic wrapper around.
00:22And that's a topic that we will do right now, which is basically figuring out and configuring, setting up our images. Let's just do it.
00:29So let's start with chapter three. So it's going to be a bit of a setup lesson, but we're going to learn a lot of stuff in the process, just not animation.
00:36What I'm going to do is what I want to do is I want to take all these images that I have here of Bella. I have your eight images.
00:42And what I really want to do is I want to stack all those images, one next to each other, and cut them. So basically have a mask of each of these images.
00:51That way I could have one, two, three, and all the images, basically all eight images, span throughout the content.
00:58That's our goal. That's what we're going to do by the end of this lesson.
01:01Now, we're starting off with just a little bit of copy of zero to geek learning. Better is better. And if we go into our HTML, let's get rolling with it.
01:09We'll see that I have here basically just an h1. My CSS is really stripped down. All I really have here is just a little bit of rules.
01:16One that says that our z index of our h1 will be 10 to make sure that it will be always visible.
01:21And that his position is absolute, so it won't move around and it'll be above everything.
01:26All right, so that's really all about the CSS and HTML, very simple HTML and CSS.
01:30So let's jump into the JavaScript and start working.
01:34Now, really, what I want to do is I basically want to load all the images. But let's start off with loading one image.
01:40And I'm going to go ahead and just create your var for image.
01:43And I'm just going to literally create the image and send it into jQuery. So I'm just going to go ahead here and create a new image.
01:51Instead, it's source. And literally, that's I'm basically not even appending it yet. It's kind of floating in the air.
01:58And what I'm basically what I basically want to do is I want to basically send the actual art.
02:02And it was Bella. And it's dot png on the end. And literally, oh, dot, dot png.
02:12And all that's really missing is to add the actual value itself. And let's just put one for now.
02:17And then we're going to make it a little bit more dynamic. So literally, that is the first, first step that we want to actually load the image.
02:24But really, we want to load this image more than once. We want to have a cycle of loading one image at a time.
02:30I don't want to load all eight images at a time. And I don't want to add it to the stage until it's completely loaded
02:35because I'm trying to avoid flickering as much as I can.
02:38So what I'm going to do is I'll just create your quick little function, which will be called load image.
02:43And the load image will enable me to sequence these calls. I could literally just grab this image creation process,
02:50which is not yet added to the stage. And I could just say, hey, load that image.
02:54And I could basically have the load image itself do a recursive kind of call where once it's done,
03:00it will move on to the next one and next one and next one.
03:02But before we even do that, what I'll do is I want to just create that full cycle of creating that first image.
03:08So I'm going to go to that image load and I'll tell it when you're done loading on image loaded,
03:16I could go ahead here and just create that function really quickly on image loaded.
03:20What do I want to do on image loaded? I probably want to load up the next image,
03:24but even before that, I probably want to add it to the stage.
03:27So the first step next is probably going to be to go ahead here and just add this element to the stage.
03:32So I could literally just go to my body and I'm going to go and just append that image, which is the this that just came back.
03:42So that was the image. It was just there. If I save this, I should be getting my image right there on the page.
03:48All right. So that's great. That's the first step is really good.
03:51Now, the only problem is, is once I move in and I add the next image,
03:55the next image is automatically going to be in a completely different position.
03:58But for now, let's just add all the images onto the stage and then we'll worry about making it look nice.
04:04So what I really want to do is I want to have a global variable that I'll know what is the next image that I'm loading
04:10instead of loading each time the exact same one, because what I'd want to do is when the image is loaded is go ahead and load the next image up.
04:17So to do that, what I want to do is I want to create here basically a global function.
04:22And in this global function, I'm going to just put everything that is I need for my project.
04:27And the idea behind here is I don't want to have a hundred different variables floating all over.
04:32Instead of that, I'm going to group them all together to my geek image info object.
04:37So it's going to be geek.i.i.
04:39And in my geek.i, I'm just going to put here for now two things.
04:42I need the actual index itself that we're in, which is one.
04:45And I also need the total. So let's give it a name that is applicable.
04:50Let's call it last. And our last item was item number eight.
04:53All right. So now we have a reference to the index and the last item.
04:58And I could then go ahead here and instead of setting here the value of one,
05:02I could literally just send in that geek.i.i into it.
05:07All right. Now, well, that's one. So now that I set the geek.i.i into it, it's going to do the exact same thing.
05:14But now I have actually a dynamic value here, which will enable me to then go on and check and see if my image was loaded.
05:22So let's do that even before we test it out.
05:24We could literally ask here if our geek.
05:27Oh, geek.i.index, right? Why didn't you correct me?
05:31So geek.i.i, next time correct me, right? I'm kidding.
05:37All right. So geek.i.index, as long as the index itself is smaller or equals,
05:43well, actually only smaller than last than geek.i.last,
05:48the reason why only smaller is because we're going to be calling it,
05:51we're going to grow it by one. So if it's seven right now, that's fine.
05:55But if it's eight, we don't want to do this again.
05:57So right now, if our value is underneath, which it is,
06:00what I'm going to do is I'm just going to go ahead to my index
06:02and I'm just going to bump it up by putting plus plus before it,
06:06which will basically just make sure that the value will grow by one.
06:09And now that the value grew by one,
06:11I could literally just go ahead and call that image load again.
06:14And if I do that and just save it, go back into my application, click on refresh,
06:18we should get all the images should now appear one after each other.
06:22So literally we could just slide ahead and see all the images
06:25one after each other in our application.
06:28So we have here the dog before food, looking at the food,
06:31really looking at the food, getting ready to nibble and enjoying himself.
06:36So those are our images.
06:39But really what I want to happen is I want those images
06:41to be stacked on top of each other.
06:43So to do that, what I want to do next is I want to,
06:47the first step is I want to stack them all together.
06:49So that's relatively easy.
06:50We know how to do that in CSS.
06:52What we want to do is we want to approach that image
06:55and just set its configuration.
06:57So once we're ready and before we add it to the stage,
07:00we could just go ahead and literally just go to the tween light
07:05and we could set this and we could say,
07:08okay, what are the things that we want to set?
07:10We could have done it with jQuery,
07:12but in our case, we're just going to go ahead
07:14and try to use the library as much as we can.
07:16And we could literally say,
07:18we want to put you in position in left zero.
07:21We want to do the same thing for the top.
07:24Let me just make sure here that we got here a tab.
07:29That way it'll all sit nicely flushed against each other.
07:32And we must also set a position that is absolute.
07:36And that should position all the elements one on top of each other.
07:43So if we go ahead and just click your refresh,
07:45all the images should be one on top of each other
07:48and we definitely could see how they're basically
07:50plopping all over each other
07:52because there is a slight delay between the loads
07:54and we could see how all the images
07:56are basically being uploaded onto each other.
08:00So if I make this a little bit smaller,
08:02it's a really quick animation that we didn't intend
08:05for that animation to take place.
08:07So what I want to do next is I want to make sure
08:10that as soon as the item is on the stage,
08:12that it doesn't start animating.
08:14So what I could do, for example, is set the opacity to zero,
08:17but even before that notice that this animation
08:20is going to be a problem for us
08:21because as soon as it's on the page,
08:23it's going to create a small flicker,
08:25which we're going to have to deal with
08:26and figure out how to sort it out.
08:28But the next stage that I want to do is
08:30I want to actually set a clipping.
08:33So if I go ahead here, I'm going to add that.
08:35And by the way, clippings are supposedly deprecated
08:39and CSS3, they don't exist anymore.
08:41But most of the browser venues,
08:43most of the browsers that I've been playing around with
08:46haven't implemented the new clip path,
08:51some of them partially and not completely.
08:53But I've noticed that clip for now is quite working,
08:58but obviously in a real world application,
09:00you might not want to work with this,
09:01but we will because we could, so we will.
09:04Now the clip logic works this way.
09:08It has basically, and I'm just going to put here a comma here
09:13so you could see it.
09:14Basically, when you set a clip in CSS,
09:17not the lip, not flip, clip.
09:21When you set a clip in CSS, and again,
09:23this is not, basically it's deprecated,
09:26so probably you don't want to use it,
09:27but we are going to use it anyways,
09:29is when you set a rectangle, you set the top position,
09:32the right position of the image, the bottom, and the left.
09:36And that will make sure that you could basically
09:38crop an image to the positions that you want.
09:40It's a very simple, very lovely way of doing it,
09:44and because it's cross-browser compatible right now,
09:47that's the one that we're going to use.
09:49So when I'm going ahead and I'm setting that clip,
09:51I could just send here in my jQuery,
09:54in my, well not my jQuery, into my tween light,
09:57I could literally just send whatever that rectangle is.
09:59And for us, really what we want,
10:01if we think about it for a second,
10:03there's a few items here.
10:05We have here the top, we have here the,
10:09when we're looking at it, we have here the top.
10:11Does the top ever change in our element?
10:15Not really, the top stays quite constant,
10:18because our top is always going to be at zero pixels,
10:21we want it to be at zero pixels.
10:23Alright, so that's good, that's step one.
10:25Our right on the other hand is a value,
10:27we need to figure out what that value is,
10:29and so is our left,
10:31so we need to figure out our right and left,
10:33so let's keep this open for a second.
10:35But when we're talking about the height itself,
10:38that's kind of something that we could probably do
10:40that is constant.
10:42So let's go ahead and just save into our geek,
10:46into that geek two there.
10:50I could set here a height.
10:53So I'm going to go here and just set a height,
10:55and I'm going to hard code a height,
10:56whatever I feel like,
10:57I'm just going to go ahead and put here 600 as my height.
11:00And I'm just going to go ahead and just use that height,
11:02and obviously in a real world scenario,
11:04you probably would want to figure something better out.
11:06So that's our height,
11:08and I'm just going to go ahead here and type here PX,
11:14PPPPX,
11:16and I'm just going to go and add that value right there.
11:20Things are going to get a little bit messy,
11:22but that's fine.
11:23We have here our top, we have here our bottom basically,
11:26which is our height.
11:28It's time for us to configure our left and right,
11:31so I'm going to go ahead and create here a really quick variable,
11:34and let's start with our left.
11:36And our left, what will be the value of our left?
11:39So when you think about it,
11:40the value of the left is literally going to be
11:43the amount of the width that we decided
11:46divided by how many items we have.
11:48So if we have here from 1 to 8,
11:50that means we have 1, 2, 3, 4, 5, 6, 7, 8, 8 items.
11:55So if we have 8 items,
11:56let's go ahead and just go ahead and decide
11:58what will be our width.
12:00So I'm going to go ahead and just say,
12:01let's just go to our body,
12:03and I'm going to ask what its current width is.
12:09Whatever it is when the user starts up,
12:11that's going to be the width that we're going to use.
12:13And the next item that I'm going to create is,
12:15I'm just going to create a very quick item for me,
12:17and I'll just call it something like slice width.
12:21And slice width will basically take the width of everything
12:25divided by the amount of items that we have,
12:28which was geek, i.i. last,
12:33and I also want to make sure that the value is round,
12:35but beyond that, I also want to make sure that
12:37I don't mind having a little bit extra.
12:39I don't want to have a little, not enough.
12:41So I'm going to go to math,
12:42and I'm just going to go and ask for a seal.
12:45I always mix the seal and seal for some reason.
12:52Don't ask me why.
12:53All right, because I'm dyslexic.
12:54That's why.
12:55All right, you asked me.
12:56I told you.
12:57All right, so slice width.
12:58So that was my dyslexic interpretation of the word ceiling.
13:02So we got it.
13:04We found it.
13:05We turned blue.
13:06We got the right one.
13:07Now that we have a slice,
13:08and now that we have something
13:09that is basically an eighth of our stage,
13:11I could go ahead here and just copy this value
13:14and position it inside of our left.
13:17That should be our left,
13:19because that's basically the position.
13:21Well, that is the width of each item,
13:23so we want to take that width of each item
13:25and duplicate it by how much?
13:29By our index minus one.
13:32Now, the idea behind index minus one
13:34is because our index starts at one,
13:36if I subtract one from it, then the value will be zero,
13:39which is perfect.
13:40That's what I want.
13:41When I move to two,
13:42the value will be basically the width of the last one.
13:45That way, each round,
13:46I'm going to grow by the width itself.
13:48So I'm going to go here
13:49and just set o to my geeki.index minus one.
13:55That should be my left,
13:56and let's already configure my right as well,
13:58and the right itself should be basically the same thing
14:01only without subtracting one.
14:07Now, all that's left for me to do is to position those items.
14:09So we said the left is going to be the last item,
14:14and I'm just going to put here left,
14:16and for our right item, we want to make it a PX as well,
14:20and comma,
14:22and we're going to connect right into here the right.
14:25Now, hopefully, if we did everything right,
14:27our images should be clipped into their position.
14:30Let's give it a try and see
14:31if we're going to see our images clipped and positioned.
14:34Ideally, that's exactly what's going to happen
14:36and what's going to happen.
14:37We should see our elements basically positioned
14:39one next to each other,
14:40and that's exactly what we're seeing,
14:42and this is working really, really nicely.
14:45So that's it.
14:46We're done positioning and clipping and setting
14:48and configuring our application,
14:50and we're ready to start moving into animation,
14:53which is what we're going to do immediately
14:55in the next lesson.
14:57Now, even before we bump out and go away here,
15:00this is one hard-coded value that I probably would avoid.
15:03So instead of that, I could literally just create here
15:06some sort of Geek,
15:09Geek.i.i content,
15:11and the idea behind this is if you change things around,
15:14you don't want to have hard-coded values that are not needed.
15:17That way, you could go up above here
15:19and just say, all right,
15:20I could approach and talk to my content,
15:23and only in one place I referred to something specifically,
15:27and that way I could change it very easily
15:29if I want to at any stage.
15:31That way I could just access everything
15:33from within that container that I had.
15:36But beyond that, if we look inside here,
15:38you could see that everything here is relatively dynamic.
15:40And if, for example, you want to make this also work
15:43for resizable browsers,
15:45it shouldn't be very hard because you could control these values
15:48and then refresh the information over again.
15:51You would probably want to move this logic of positioning
15:55and aligning into a separate function
15:58and then listen to whenever something changes.
16:00But we're not going to do that because this course is about animation.
16:03It's less about just JavaScript in general.
16:05With that said, we'll see you in the next lesson,
16:08and in the next lesson, we're going to continue
16:10and dive into animations in the timeline.
