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

Moving to manual mode

In our last lecture, we created a tracker that followed every single move of the user and added them into a TimelineLite timeline. The problem with that design was that we could only animate one thing at a time. It would be great if we could animate more than one item at a time. In this lesson, we will create a manual toggle button that will let the user to re-position all the elements before triggering a "key frame". We will complete this task in the following lecture.

Video Transcript

00:00So there's absolutely nothing wrong with the animation that we have right now
00:03But the problem that we have is that we could only animate one thing at a time
00:08But what if we wanted to be able to move more than one thing at a time using this controller?
00:13So to be able to do that we're kind of taking a live session here
00:17Which is going to be broken into two parts and in the first part
00:20We're going to start setting configuring figuring it out
00:23Until I reach a dead end where I'll need a break to think which I hope you do as well
00:28You'll stop for a minute or two. Just think about okay
00:30So how do I solve this problem and then we'll resume in the next lesson where we'll actually complete the task completely?
00:36So let's get to it. Let's get started. We're gonna do live together
00:40I don't even know what's gonna come out of it
00:43But we'll figure it out as we go
00:45The goal is to create some sort of a toggle button here that we'll be able to basically freeze that
00:50Automatic animations that way that we could basically start our animations and stop them whenever we want to
00:56We're not gonna make a very fancy pretty playback
00:59Oh
00:59I'm gonna have yours a button that says manual or auto and when it's in manual mode
01:04Nothing will be recorded and whenever I click on auto
01:07It will automatically record all the changes and then I could basically switch it back to auto or switch back to manual if I want
01:14To do it manually. So let's go ahead and start figuring it out
01:17so the first step is I'm gonna just add that controller and I'm gonna go ahead here and just add the new controller and I'll
01:23Just start off by auto because that's the status that it's gonna be in and I'm gonna call this on manual
01:29Manual toggle even because I will make it even more logical and I'm gonna go ahead and just create that button
01:38Functionality that is and
01:40To be able to create this functionality
01:42I need to know if we are in a manual mode or non manual mode and I definitely need a global variable for this
01:48So I'm gonna go ahead and create a quick little
01:50variable is
01:53Manual and
01:55the default will be false and
01:58I'm gonna go ahead and each time the button is clicked on I'm gonna automatically switch that
02:04Status from whatever it was to the opposite of whatever it was beyond that
02:08I also want to go to the item because we're using jQuery to trigger this events
02:13Then that this is going to be the actual HTML elements
02:16I'm wrapping it around in an HTML attribute
02:18Because what I want to do is change the content of that mat of that item and change the copy based on the status of the
02:25Variable, so if is manual that means we are in manual
02:28So I'm gonna write here manual so like that it'll be very clear to the user what status we're in and if not
02:34We're gonna set it to auto. So if I go ahead and click it save refresh, just take it for a ride
02:40I'll have auto. I'll have manual. I'll have auto
02:43I'll have manual and when I'm in manual condition, then I'm not going to be automatically triggering anything
02:50Beautiful. So now that I have that the next phase is to actually integrate this logic now to test this out
02:56What I want to do
02:57I'm gonna start with a very basic test here and then slowly slowly improve it
03:01What I really want to do is I'm for me the most important part here is in the on drag and where I'm adding the animation
03:07I'm only gonna add it if it's not manual. So if we're not at manual
03:13So I'm gonna go ahead here and I'm just gonna ask if
03:17Not manual
03:20Then we're gonna go ahead and do all this
03:26Now the problem that we're gonna have that we are having right now is if I go ahead and test this out
03:32My problem is is if I go click on refresh
03:34Let's say I add two items and I put these two items in here. Beautiful. They should replay without any problem
03:41Problem is is when I change myself to manual and then I go ahead and I move something
03:45And I go back to auto again and click on replay that motion didn't get captured at all
03:51And what I really wanted happening is that we'll have those two items animate at the same time to their next position
03:57For us to be able to do that
03:58We're gonna have to know what are the items that are there on the stage and for us to know
04:03What are the items that are on the stage?
04:05I have to go to this on add and literally save this SVG so I'll have access to it later
04:11So I'm gonna go ahead and create another global variable and I'm call this a items or let me
04:17let me use the same terminology as elements because that's what we use in the CSS a
04:22Elements, and I'm just gonna automatically set it to nothing
04:26And what I want to do is I want to basically add those elements into our
04:31Whenever we're adding the elements, I'm just gonna go ahead and also append that so I'm gonna push
04:37Our SVG right into that array that way I'll have access to this when I'm switching modes
04:45Now that I have access to it when I'm switching mode. I'm gonna switching modes. I'm gonna go back to this on manual toggle and
04:53What I want to do is basically I'm gonna again. I'm gonna ask myself if
04:57We just switch to not manual
05:02manual if we did not manual is
05:08Manuel so if we're not if we are
05:11Basically automatic if we're not manual then what I want to do is I want to loop through every single one of those
05:17Elements and then decide do did they change a position or not now because of I've always
05:24Checked up for that last position each time and every time it dragged I saved that
05:30that basically
05:32The movement I'm in a better shape because I'm it's not gonna capture all the movements that were
05:38But it's gonna capture one of the movements
05:41But really I need to be able to know what was the original position
05:45And if I do this just like this and the user drags at five times that I'm not gonna have the original position
05:51It's not good enough anymore, and I need really need to figure out a way to store the last position
05:58After the tween is done and not before it's we're gonna have to figure that out really really soon
06:03But first of all let's go ahead and create that loop
06:06And really what we want to do is we want to loop through the elements of a elements
06:11I'm just gonna go here and create a 4
06:13var I
06:15equals 0 I is smaller
06:19smaller than a elements length and
06:25I
06:26plus plus
06:28so okay, so we're gonna travel through each element and
06:32We literally want to we want to tween every element right now, but really this is kind of like a console TBD
06:40We just put here
06:42TBD because I for me to be able to do this and I'm this is what happens when we're thinking out loud together
06:48For this to be able to work my problem right now with the animations right now is that I'm storing that initial position
06:54On the star drag, but really when we think about it
06:58We know that position on our end drag ready because that's the position that we're ending is the position that we're gonna start the next one
07:05So ideally what we'd want to do is we'd want to save this position and and save this this P
07:11Basically right after we were done and added all our stuff inside. We want to save that position now
07:18Ideally, we don't want to do this at all because if we're adding this element in here
07:22We're creating a problem. We're creating a problem because
07:26For one this element right here. This value right here is
07:31Gonna change all the time and we don't need it anymore. We only need it in that first first instance
07:36so for us to sort that out we really have to go to that creation of that draggable element and
07:43We want to basically append to it this information that hat it has already by default in the beginning
07:49So I'm going to go ahead here and just comment this out, and I'm just going to create here a new variable and call this
07:55drag and
07:57I'm going to go ahead and just console it out because I want to see
08:06seconds
08:07Save this drag and I'm just going to go ahead and click on refresh and just add a new item to the stage
08:12And when I add that item to the stage first of all I'm getting an array and in that array
08:17I have one object now if I add another one again
08:21I'm going to get an array and in that array
08:23I'm going to get one object which I'm assuming is my object so I'm going to go ahead here and just test out that
08:28Idea that I have that this is my object and literally I'm doing this on the fly with you guys
08:33So I could see in my log that this is actually an array
08:37So I'm going to go here in just a position zero
08:40And I'm going to go ahead to my drag, and I'm going to tell it your last P equals
08:48this information
08:51Now if this logic worked, then the first time around when I call the on start drag
08:56I should already automatically see here in my console log. I should be able to read out the this
09:03Dot last P. I should get it already as soon as we start the drag even though
09:09We didn't create it supposedly although we did so if it did work then we know we're in the right direction
09:13So I'm going to go ahead here and save this for a second go back into my application click on refresh add
09:18That one element start dragging it, and I could see that I definitely have access to some information
09:24But I don't have an I don't have full access because my my x is not defined yet
09:30My y is not defined yet because nothing started no animation begun
09:34So what I'm going to have to do is first of all this is definitely working
09:37But this is not because there was no this so I'm going to have to that was a mistake, okay?
09:41So that's we're getting definitely close, so I'm going to paste that in there because that this was just
09:47Throwing us off click on refresh here one more time, and let's see if that will solve our problem
09:53And it sure did it solved our problem. Which really our initial position was zero zero beautiful
09:59so now we got that initial position initial information we could go ahead and
10:04literally the go well one of the stuff here with this manual and and non-manual is
10:11That we're going to we're going to ignore that
10:13amount of time of that animation and
10:16One of the stuff that we probably want to do with the on drag is maybe capture and not change the whole object
10:21but instead of changing the whole object to go ahead to that last P and
10:26Just change the actual stamp
10:31Because we want to make sure that we're not going to overload the user with the with time
10:35So I'm just going to make sure that if the user moves something around a hundred times
10:39Before they click on the actual play button
10:42It's going to always take basically roughly the amount of time that passed since that last movement now obviously
10:48Not very important, but we're slowly slowly getting closer
10:52So I'm going to thinking out because it is a complicated animation what we're doing here all right
10:57So let's just think it out loud together again. We created the item we defined its initial positions
11:02We added it to our a elements whenever we start dragging. We're updating that stamp
11:08whenever the ending happens and we calculate the animation which will give us an accurate amount of
11:13distance between the two and
11:15We're not updating that initial XY position
11:17So it won't matter if we move things around when we're in the non drag mode, and we're only updating at once
11:23We don't have to yeah, this everything here seems very logical see everything seems you're very logical still
11:29So if I go ahead here, and I'm going to test it out without going to manual mode
11:32I would expect everything to continue working just the way it did before
11:36So I'm going to add an item. I'm going to add another item
11:40going to add another item, and I'm just going to move things around a little bit, and then I'm going to click on replay and
11:47If everything worked the way it should then everything should continue working beautiful
11:52Now the next thing that I want to sort out before we even go into this on manual toggle mode is I want to
11:57Sort this issue of the from 2 the only reason we were using the from 2 was because we were basically
12:04Literally our
12:06Problem here was that we were we didn't know where we didn't
12:11Know that our starting point was always where it was supposed to be and the reason was because we didn't create a tween light
12:18Set or a timeline set and position our element right away
12:22So instead of just setting our positions or setting our information that we had for our regular CSS
12:27What I want to do is I want to go to my SVG element, and I'm going to go to my to my my timeline
12:34timeline light and I'm going to go ahead and I want to do two things one is I'm going to set its
12:38initial value of our SVG
12:41To I want to set its initial value wherever our timeline is currently right now. I want to set its opacity to be zero
12:50Now the idea behind this is that any animation that happened before this
12:54Even though we have this element on the stage already. We don't want it to be visible at all
13:00The next thing is I want to add a really quick animation for it to fade in
13:03So I'm going to add here an opacity of one, but I'm going to create here a two animation
13:08Oh, you know like but let's set also its positions
13:11So I want to make sure that my position is going to be set through my x we're going to set to zero
13:17For my y I'm going to set it to zero as well. So I'm literally setting those initial configurations just the same way and
13:25Then I'm going to create a two animation taking my SVG and I'm going to animate it for zero that two seconds
13:32So for zero that two and I'm just going to set my opacity to be one
13:37Now the idea behind this is because I want to basically fade it in
13:40But it also makes sure that if we do a reverse or replay the elements that we're adding in are not going to be visible
13:47Now beyond that that we took and you know if we want to stick to the same convention here
13:51We could literally just take this information and instead of putting here zero
13:55Just put inside the information from the drag element that way if in the future we decide
14:00They were going to put it in a different area
14:02Whatever the drag element knew that's the information that we're going to set also for our timeline now because we're adding it in the timeline
14:09It's going to be connected to a specific point in time, which is the current playhead where it's at right now
14:15What's powerful about this and that we set this originally here in this position is that we don't need to create a from to
14:22Any more because our x and y positions were already configures configured and set so instead of creating a from to
14:29I could literally create a timeline
14:32to and
14:34I just have to send in for my to my target, which is my this
14:38Target and I just want to send also my seconds which was my seconds and I just need to send the actual to itself
14:44Which was the secondary object right here? So that's all the information
14:48I really have to send and this should be enough for animation to still work now if it's still working
14:54It's going to make our life way easier in our on manual toggle because then we don't have to do a from to any more
15:00We just have to do a two because it will always know its last position
15:03So if I go ahead here and just click on refresh one more time to validate that everything is still working
15:09Just going to go. Oh and I have your small error. So
15:13Timeline light set is not a function. That's true because I was trying to approach the constructor itself
15:18So let me go back up there because what we're I don't know why I did that
15:21But I want to go to my TL to my actual object itself and I want to make sure yep on the bottom
15:27I did it right on the top. I didn't I'll click on refresh. Let's see that we're not getting an error
15:33okay, so I'm just going to copy a few items add them on to the stage and
15:38then I'm going to move them around a little bit and
15:41If I click on replay, I should have my items going in now
15:45I could see already that I have a slight slight little issue here because when I'm pressing on replay
15:50I could see that my initial values are starting from wherever point there were in again
15:55So if that is definitely a problem and we definitely need to sort that out somehow
16:00And let's take a deeper look into this to try to see what are we doing wrong here?
16:05So when we set our initial positions and we set them to be in the in that initial position of our X and our Y
16:12That seems right to me
16:15Let's see. Let's see. Let's see. Let's see
16:18So this all looks really good to me to be honest
16:21Let me save this make sure that everything is saved. Let me click on refresh
16:24And what I'm going to do is just create one animation take a light in one move it to point B
16:30Move it to point C. The idea behind this is this to check one thing at a time to see where does our animations break?
16:37I'm going to add another item and now I'm going to push them to here. I'm going to click on replay. So one
16:43So this seems to be working much better, but
16:48We could see that we could see that it's not a hundred percent better. So one is I'm adding one. I'm adding two
16:54I'm clicking on reflect replay. I would expect this first item not to be there
16:59So let's try to figure out why is that item still there even though we moved our playhead to position zero
17:05So when we moved our playhead to position zero, I think one of our problems going on here
17:13is
17:14That our not to be honest. I I don't see oh maybe because it's getting confused that maybe we need to set the opacity here to
17:23Inside of the CSS attribute. Let's give that a try for a second
17:28Because I don't think that CSS attribute was being actually triggered
17:31So maybe I have to explicitly let our setting know that this is CSS opacity
17:37I'm going to save this again. Click here refresh
17:41Let's see if that that did the trick
17:46Okay, so setting a property has only a getter line 16
17:50So at least we're getting an error which is already better than not getting any errors and in line 16
17:57Why would we get this error?
17:59I might actually need to do a little pause here to try to figure this problem out because I don't want you to watch
18:03Me just stuttering for an hour. So let's see. Let's try this just for one last second. So this is not there
18:09It's just something that we did recently
18:12So it must be somewhere here that we basically
18:15Mess things up for this animation
18:17So let's see. Let's see. Let's see. So I'm gonna do a pause here. We're gonna continue in a second
18:22I'm gonna solve this problem and then we're gonna continue

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!