Setting up for the PhysicsPropsPlugin
In this chapter, we will explore the PhysicsPropsPlugin that enables us to create isolated physics related animations per property. In this lecture, we will setup our stage and improve last lessons code as we set the ground for the PhysicsPropsPlugin.
Video Transcript
00:00brand new chapter, and in this chapter,
00:02we're going to explore the physics props plugin,
00:05a feature available exclusively
00:07to the club green sock members.
00:10So let's jump right in there and prepare our stage.
00:13So first thing that I wanna do is I wanna make sure
00:15that I load up the plugin,
00:17which is only available for members,
00:20and I'm gonna go ahead here and just copy the path
00:22and just make sure if you haven't done so already
00:25is download the club only package,
00:29put it inside of the JS last GSOP folder,
00:33and then load in the plugin physics props plugin.
00:36And I'm gonna go ahead and move into our JavaScript,
00:39which what I'm gonna do is I'm gonna basically duplicate
00:41the do physics to plugin,
00:43just to create our own interpretation of it.
00:46But instead of it, I'm gonna call it do physics plugin,
00:50and I'm gonna set in five items for now.
00:53And I'm gonna go ahead and I'm gonna duplicate our function
00:56that we've created earlier.
00:58And we're gonna just start modifying it.
01:00So we don't start over completely from the beginning.
01:04And I'm gonna go ahead here, just copy it,
01:07bear with me for a second.
01:08And this is going to be our starting point.
01:11I'm just gonna make sure
01:12that I delete the initial animation.
01:14And here we go.
01:15This is our initial starting point of our animation.
01:20All right, so let's get started.
01:21So in our do physics props plugin,
01:24we are expecting account, we're gonna get five.
01:27So we have a count of five.
01:28There are a few items that we're gonna animate.
01:30Angle doesn't exist.
01:31I'm gonna go ahead and delete angle.
01:34But instead of that,
01:35we're gonna control our velocity, our acceleration.
01:41Acceleration, making sure that I wrote it correctly
01:45because all we need now is typos.
01:48And next is going to be friction.
01:50And those are the three properties
01:52that we could control with the physics props.
01:54And we're gonna see what does that mean
01:56and how do we work with them?
01:58So next I wanna follow up with a problem
02:00that we had earlier when I told you
02:03that we're creating a lot of reflows.
02:05And when I say reflows,
02:06I mean that we're adding items more than once,
02:09one after a time into our stage,
02:11creating a bigger staggeration,
02:13a bigger effect on the overall load of our system.
02:18So to solve that problem,
02:19instead of creating a lot of separate div layers,
02:22what I wanna do is I wanna add all these items
02:25even before we start our larger loop.
02:30And to do that,
02:30what I wanna do is I'm basically gonna go into our items,
02:33I'm gonna loop through that count time.
02:36But instead of actually adding a div each time,
02:39I'm creating a separate loop
02:41and I'm just gonna create here another variable here
02:44and I'm gonna call it sdivs.
02:46And for sdivs initially, its value is gonna be empty.
02:50But what I wanna do is I wanna add to our sdivs
02:53that specific value each time.
02:55So I'm gonna just add to it that string that we have here
02:59all the way in the end here.
03:02Literally just appending the value each round,
03:05doing all those rounds initially.
03:07That way I could literally get rid of these lines
03:11where I'm appending the item,
03:12I'm appending the div layer and I'm creating it
03:15and I could literally just do all of that initially
03:18by just going and deciding where do I wanna put it.
03:20In my case, I'm gonna just go ahead
03:22and put it directly into our body.
03:25Because I wanna see everything this time around.
03:27And I'm gonna go ahead and I'm gonna basically append to it,
03:33or really in our case, I really wanna replace it.
03:35So I'm just gonna go ahead into the HTML itself
03:37and I'm just gonna send the string that we've created.
03:40Now if I do this and I'm just gonna comment out
03:43everything we have below
03:45and I just go ahead, refresh, save, save
03:47and then refresh, we'll see that now we have our items
03:50stacked one after each other coming into the stage.
03:53Now they affect each other right now
03:55so I wanna make sure that they don't.
03:57So I'm gonna go ahead into my CSS,
04:00I'm gonna find my item in my CSS, my circle
04:03and I'm gonna go ahead to the circle
04:05and I wanna tell it that its position is absolute.
04:09And by doing that, I'm gonna make sure
04:11that they're gonna basically all overlap each other
04:14which is what I want to happen by default.
04:17Now beyond that, what I wanna do
04:18is I wanna add here some extra properties for my item.
04:22So I'm gonna go ahead to each div item
04:25as it's being created, I wanna add into it a style
04:30and I'm gonna go ahead here and just add a style
04:32and literally I'm just appending into that style
04:35whatever values I wanna append.
04:38So in this case, what I wanna do
04:39is I wanna change my, let's say my top position
04:44and I wanna put their value, it's gonna be in PX,
04:47I wanna put their value that's gonna be
04:48more or less like the range of our screen
04:50but I'm just gonna go ahead just for the sake
04:52of convenience and I'm gonna go ahead
04:54and just really quickly create another two variables
04:57for our help and one I'm gonna call it top
05:00where by default, its value is gonna be zero
05:02and I'm gonna create another value
05:03which is gonna be left and I'm gonna default it
05:06to zero as well.
05:07And literally, all I'm gonna do is create a math random
05:11and although I could grab the size of my screen
05:14and so on and so forth, I'm gonna keep myself
05:15to a box of 500 by 500 and I'm just gonna make sure
05:19that I'm gonna math, seal it, making sure
05:23that we're gonna send here a complete value,
05:26no fractions, only integers.
05:28So that way, each time our loop is going to run,
05:31we're gonna have a different top, a different left
05:33and all that's left for me to do is assign these values.
05:36So literally, as I'm creating the item itself,
05:40what I'm also doing is I'm literally,
05:42I'm creating a backing to it
05:45so I'm basically creating its different positions
05:47and hard coding those positions
05:49right into the div element itself.
05:52And I wanna make sure that I don't have an error
05:54so don't put your space but a comma
05:57and if I save that, go back into my browser
06:00and click on refresh, we're gonna see that now
06:01my items are gonna be spread randomly throughout
06:04the different zones that I permitted it to go
06:07between that 500 and 500 but in this case,
06:09because our screen is so wide,
06:11I'm gonna change it to be a thousand
06:13and maybe 300 so we could always see everything.
06:16So let's go ahead there and change that range of our left
06:19to be able to go in a thousand direction
06:22and we're gonna lower this to 350, I think,
06:25so that way we could see all the items on the screen,
06:28all the five items as they're being created.
06:31Beautiful, so that's all we wanted to do
06:33in our setup to prepare everything for the animation
06:37which we'll be working on immediately
06:39in the following lecture.
06:42So we'll see you in the next lecture
06:44where we complete this task
06:46and actually star create animating them
06:48and taking advantage of the physics props plugin.
