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

Transforming the origin

This is our last lecture in this chapter dedicated to CSS. In this lesson we will take a look at the transformOrigin property and see how to work with it in the GSAP tween library. When we complete this lesson, we'll be done with chapter 2. This is a fun one, so let's get started.

Video Transcript

00:00I am going to continue my commercial for this really cheap piece of nothing that costs less than a dollar.
00:05It was actually 69 cents and I think that's brilliant for a USB drive.
00:09Alright, so my 69 little gadget.
00:13Now when we're talking about a 3D space, but not only a 3D space,
00:16and we want to animate something, by default if I'm flipping it around,
00:20its center point is going to be in the middle.
00:21So we're going to have like this animation where the element is sitting in place.
00:25But what we really want to happen maybe sometimes is we want to change that pivot point
00:29to be a different point, and it might be a point inside of that 2D space,
00:33or it might be a point that's actually even out of the area.
00:36So basically saying if we're going to animate the Z line, say hey I want your center point to be here,
00:40and then our animation is going to go basically background.
00:43Now obviously this is all an effect and it's a 2D effect that's mimicking a 3D environment.
00:49But it's a very powerful effect and that's exactly what we're going to do in this lesson.
00:53It's a fun lesson and with this one we wrap up our conversation about CSS in general.
00:58I urge you to look into the CSS documentation,
01:01and I urge you also to create your own variations.
01:06I would love to see how do you take this project that we created with this project in general,
01:11and put your personality into it.
01:13And the ones that we like, not only one, the ones that we like will find a way to integrate it into our site,
01:18and then you could add that into your resume, you could share it with friends,
01:21and it could be proud of something that is actually living in a live commercial site.
01:27We're happy to do that, but that's up to you if you create something really that impresses us,
01:31and we promise to give you feedback on anything you show us.
01:34So if I don't like something, I'll tell you why I don't like it,
01:37and then you'll have another chance to make it even better,
01:39which is a perfect opportunity to continue learning and creating something cool,
01:43and adding something into your portfolio.
01:45So I hope you take on this challenge.
01:47Last time I talked about it,
01:49post a link to your output or put the code of your output right down there in our comment section,
01:56and I'm sure to check it out and give you my feedback on it.
01:59And for now, let's go back to our origin.
02:02The lecture of this chapter, and it's our last lecture,
02:08dedicated directly to the CSS plugin and CSS3 in general.
02:14We'll see them as throughout our course,
02:16but we're not going to have a dedicated lesson dedicated just for that.
02:20Alright, so what I want to do,
02:22if you recall our little animation that we created that was like a fake 3D,
02:26but now that we're doing 3D, how about we go back and create something 3D there.
02:30So to make that happen, what I want to do is I want to create a flip for this whole section.
02:35So I'm going to go ahead and undo our animation in our on ready,
02:41one of our last functions there where we created that skew.
02:44So I'm just going to comment it out really quickly, or really slowly more accurately.
02:50Alright, that was a bad joke.
02:53I'm just going to copy ahead that tween again, because I'll just start from that template.
02:57But I don't want to animate the li, what I really want to animate is everything.
03:01So I'm going to go ahead and select that banner new,
03:04because inside of it is basically all our elements here.
03:07Or if I wanted to, I could go even one layer up,
03:09but I'm going to stick to that banner new.
03:12Alright, so now that I decided what I'm animating, I'll just send that in,
03:16that banner new.
03:20Next item is I'm going to choose how long my animation will be,
03:23and I want it to be exactly a second.
03:25And the rationale behind it is that our animation of our background
03:29that's revealing itself of our elements starts one second in.
03:33So I want it to kind of happen right with it as if it's banging the flow.
03:38Basically, the flow is as soon as it gets there,
03:41I want it to start basically its reveal of its background.
03:44Alright, so now that I got that configured,
03:47the next step is I'm just going to delete all these extra goodies
03:50and put in our transformation.
03:52So what I'm going to do is I'm going to start off with creating
03:54a very simple transformation of that, of rotate Y.
04:00Rotate Y.
04:03And I'll go ahead and I want to start off by a rotation of minus 360,
04:08because I want to give us a nice full circle animation
04:11as our application begins.
04:14The next step that I want to do is I want to,
04:17well, let's first see what would happen when we do this
04:20without adding anything.
04:22So just by starting our animation from minus 360,
04:25we'll have here a nice little flip going on
04:28of our whole element within itself.
04:31And you'll notice that our animation is happening right here
04:34in its center point.
04:36There is no real effect beyond that.
04:40Now, as we know, we could do 3D transformations,
04:44but even before we do a 3D transformation,
04:46what if we wanted to change that pivot point
04:48and say, I don't want to animate from here.
04:50I want to animate from maybe this point above.
04:53So to do that, what I'm going to do is I'm basically going to go back
04:56into my application, and in my animation rules,
04:59I'm going to add here just another one,
05:01and I'm going to call it transform origin.
05:05Transform origin.
05:10And in my transform origin accepts three parameters.
05:14The first one is for my X.
05:16The next one is for my Y.
05:18And my last one is for the Z perspective.
05:20The first two usually is approaching percentages,
05:23although you could write here a direct value.
05:26I like working with the percentages
05:28because then you could stay constant with it,
05:30and it's the most intuitive.
05:32Now, by default, this is the value of our origin transform
05:35in these three properties.
05:37So if I go ahead and click on refresh,
05:39we won't see any difference at all.
05:41But if I decide now, hey, I want to change my perspective
05:44of my X position, I want it to be at zero.
05:47I don't want it to be at the middle.
05:48I want it to be at zero percent.
05:50If I do that and click on refresh,
05:52we're going to see that our animation is going to,
05:54its pivot point is going to be right here,
05:56so our animation is going to have a much bigger gamut,
05:58which is a very cool animation on its own.
06:01Now, if I wanted to, I could have set also my top position
06:04for our animation, although in this case,
06:06it probably wouldn't be as dramatic
06:08or have much of an effect because we're only animating
06:12our, we're only animating in our, you might say,
06:17we're only animating from a horizontal perspective.
06:21So if I go here and I click on refresh,
06:23then we're not going to see much of a difference
06:25unless we add also an animation that would affect
06:27our Y, really our up to down as well.
06:31So with that said, how about we also create here
06:35something really cool.
06:37Now, if you go ahead and you try to add a perspective
06:39in your Z dimension, such as saying, hey,
06:41I want you to go back maybe 500 pixels,
06:45I want you to animate in this perspective
06:49that gives me a view of 500 pixels,
06:52so it's going to go back in the background there.
06:55Now, if I run this just like this,
06:57I'm not going to see any real effect here
06:59because we don't, well, I'm going to see a big effect,
07:02but a very big effect, but it's not one
07:04that I could really enjoy and the reason is,
07:06is because I didn't define also,
07:09could you guess it because we learned about it already,
07:11I didn't define also the transform perspective.
07:15And really what we're looking at right now
07:17is a transform perspective of zero, I believe.
07:20So if we go ahead and add our transform perspective,
07:23and I'll do that before we actually do our animation,
07:26and you could have added this directly into your CSS,
07:29but I'm going to add my transform perspective
07:32by setting it directly in our tween light.
07:37So I'm going to go ahead here and just set it directly here,
07:41and I'm going to go ahead and define my transform perspective,
07:47and I'm going to set its value to zero initially
07:50because I think that's what we're seeing here.
07:52And if we click on refresh,
07:54it's basically what we're going to be seeing here.
07:56This effect is very flat, although it's very dramatic,
07:59it's very flat.
08:01What I want to do is change our point of view
08:04by pushing us a little bit further back.
08:06So if I put here a value, let's say we put here a value of 300
08:10just to give us a bit more of a distance from this animation
08:15so we could actually see more details of it.
08:18And by stepping away further,
08:20you could see that our animation is already starting,
08:22we're starting to shape a form here,
08:24but it's really telling us that we need to take
08:26a much bigger distance from this.
08:28So how about we go back even all the way 2,000 back,
08:32giving ourselves a much larger perspective.
08:36And by the way, one of our last problems here
08:38is because our X position is really in a position
08:40that we don't really want it to be,
08:42I could go back in here and just define our position again
08:46to be in that 50-50 because I want it to be in that position.
08:50I want us to center point our animation in the center area
08:53where this will be the heart of the animation.
08:55And once I do that, we're going to see
08:57that our animation is a lot more connected.
09:01Now, obviously this is work in progress,
09:05but you could play around also with these values.
09:07So maybe 500 pixels doesn't work,
09:10maybe 100 pixels will create the effect that you want to have.
09:14And if you go ahead and click on refresh here,
09:16so maybe this is the effect that you want.
09:18Or maybe you want an effect that is even larger.
09:22So maybe you want the distance to be even larger.
09:26And that's going to give us a much bigger kind of space
09:29where it's going to be working in.
09:31So I personally feel like maybe it's going to be
09:33somewhere in that 200 line.
09:35And then with my perspective, the further I go out,
09:37I'm just going to put here a really crazy number
09:39such as 5000 to see what would happen.
09:41And that didn't really do too much for me.
09:43So I'm just going to go back and notch it down.
09:45This is really just a place where you play around
09:47and have fun until you find that value
09:49that just really works for you.
09:51And this is kind of cool.
09:54It might be a little bit too dramatic for our website,
09:57but it's a cool effect nonetheless.
10:00So that's it.
10:01We talked about quite a lot of stuff in CSS2, CSS3.
10:05We explored the ways you approach it,
10:07how you set properties, how you animate properties.
10:10We talked about the CSS, actual CSS definition,
10:15which would save you a little bit of processing
10:18by defining it with the CSS attribute.
10:20Or if you ever encounter a scenario
10:22where the animation isn't working,
10:24try to wrap it in CSS,
10:26and then there's a high probability that it will work.
10:29So that's it.
10:30We're done with this chapter.

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!