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

Following the users moves

In the last lesson, end users added as many circles as they wanted as we connected those circles to the Draggable component - making each circle draggable on the stage. In this lecture, we want to capture user actions and store them inside of our TimelineLite animation. By doing so, we can replay all end users steps.

Video Transcript

00:00Now that we have a drag-and-drop component and we could add as many items as we want
00:05The next step that I want to do is I want to capture every single
00:09Addition and every single movement of the components and that's exactly what we're gonna do in this lesson
00:15We're gonna record those movements. So exciting not only because I'm getting close to the end of my cup
00:24But also because our animations are gonna
00:26Really go through something revolutionary something amazing something incredibly fun. Alright enough superlatives
00:33Let's just do it. What I want to do next is I want to capture all those moments as a user is adding elements
00:39I want to automatically capture when the user moves elements and positions
00:44positions them in different specific points as they're going through their creation and
00:50Then replay it for them
00:51So let's go ahead and start figuring out how to do it in the first step that we need to do is
00:55We need a timeline
00:57So I'm going to go ahead and create a global timeline variable because I'm gonna need access to it throughout and I'm just gonna call
01:03it new timeline
01:07Timeline light and I could save it and the next step literally is to try to figure out at what position and what do I want
01:14To record now, what I really want to record is the motion that the user does
01:19So in this case when the user starts from a specific position
01:22I want to record that position and I want to record the position when the user drops that element and
01:28Then send that into my animation library and animate it for let's say a half a second
01:33So let's go ahead and do that or even better than that
01:36How about we animate it for the duration of time it takes the user to take it from point a to point B
01:42So let's go ahead and start figuring it out
01:44So the first step that I want to do is I want to first capture the initial information of the user
01:50So to do that, I'm going to go ahead and back into where I'm appending that draggable control
01:56I want to add callbacks that exist for this element which are in the second parameter
02:01Which is very very similar to the way we worked with our
02:04Timeline light and our and our tweens and whatever where we send in dynamic basically properties that we want to work with
02:10We're not going to go into every single one of the properties, but we're going to focus right now on two properties one property is
02:18The one that enables us to know when the dragging started and another one helps us know when the dragging has over is over
02:25So the first one is called on drag
02:28Start no, no surprise and by the way the same
02:32Type of logics if you want to add in parameters
02:35You would write on drag start params and then send an array in if you wanted to change the scope you would write on drag start
02:42Scope we're not going to go into all those details our focus is really on the timeline
02:47And this is kind of a bonus that we're working with the draggable so on drag start
02:52I'm going to call my on drag start function, which I'll create oopsie, which I will create immediately and
03:00Why did I put there a
03:02No bad Ben
03:06Does that mean I drank too much?
03:08Yeah, all right, so the next one is on drag end and the same applies there's on drag and params on drag and
03:16Scope, but we're not going to go into those we don't need to go into those
03:19You know what those mean already because we've seen them in a lot of different iterations
03:23So let's go ahead and create those functions really quickly
03:27Funk-shin all right, so that's the drag end and we also want to create one for the drag start
03:35Now
03:37Contrary to the tween libraries that have a data variable that I could just append data into them whenever we want to
03:44Unfortunately the draggable doesn't have something built in by default, but what I could do very easily as well
03:51Maybe it does by didn't find one
03:52But it doesn't really matter because one of the beauty beauties of JavaScript is that
03:57Objects that are created are just objects like just like any other object and because our this is coming through
04:03That's the scope I could literally just go to the this and say hey from now on you're gonna have a data object
04:09But I'm gonna go ahead and give it a name that is not likely to ever be named by the creators of the library
04:15I'm gonna call it last P
04:17Because there's no reason why they would ever create something that would call it be called last P
04:20And I'm gonna set in my exposition in it
04:23Which is actually one of the properties of the draggable which will give me the exposition that I'm currently in and so will
04:31They have a Y position, so I'm literally gonna send that save that X and save that Y
04:37Beyond that I also really want to figure out the time right now so to do that
04:41Let me just do a quick console that log, and I'm gonna create a new date, and I'm gonna ask for get
04:48What date I believe?
04:50Let's see if that's that I believe that we're gonna get the milliseconds since midnight in 1970
04:56December 31st in 1970 so if I'm just gonna save this add something
05:01Move it. I have a little errors. Let me fix this
05:04Value date is not a function alright, so it wasn't date. That was a bad guess
05:11Let me then you know let me just go quickly find it out, so I'm gonna come right in here and new date
05:20Get date for time oh yeah, I think it was no maybe get time
05:34Bingo all right memory didn't fail me after all so get time, so I'm gonna go ahead here and just get time
05:43And I'm just gonna kick take this get time and just put here a stamp
05:52And it's gonna be our time stamp and literally this is gonna be it my way to store
05:59What second in time did this drag begin in that way I could when we're in the drag and then calculate how long?
06:06How much time passed literally if I just go into my console, and I do a log
06:10I could literally do the same type of logic right here grab that information and just substitute from it that this last
06:19p dot
06:21stamp
06:22Why did I do step?
06:24stamp so I go ahead here just save this really quickly I
06:30Should be getting when I move things around also how long it took me to move it
06:35So we'll see that this took me 311 milliseconds this took me 200 milliseconds
06:41this will probably take me a lot more than that and
06:44That is it so that's basically the information we need obviously we're gonna divide it by a thousand because we need to send
06:50Seconds into tweens and the next step is to actually animate this so I'm gonna go ahead and start creating that so first of all
06:56Let me create that variable for var seconds, and I'm just gonna take all this value that just got back and
07:05divide it by 1,000
07:08Which will give me the amount of seconds that I need to put into my animation once I start creating that animation
07:14So I'm gonna go ahead and just create here a tween light
07:17from two and
07:19My front to first of all my target is going to be the target itself from like this
07:24The next thing is how long do I want the animation to be it's gonna be the amount of seconds the user actually was moving
07:29around
07:31Next properties that I want to animate is the beginning positions that I had
07:35Previously for my x and my y so for that x it would be this last p dot x for my y it would be this
07:44Dot last p dot y is there any other property we want to animate?
07:48No, but next is we want to set the two and the two is basically where we are at right now
07:55So I go ahead just add that right in there
07:58The value would be basically the this dot x because the dicks this dot x is the x that we're in right now
08:03And so is for the y and that's all we really needed to do to create the tween
08:07So what will happen now if we run this is it should replay the motion that we just did
08:12Immediately after the motion and it should take the same amount of time
08:15But it's gonna go in a straight line to that point so it doesn't matter how many squiggly lines we make
08:19It's gonna go from point a to point B in the amount of time that it took us to get there
08:24So if it took us this amount of time
08:25It's gonna take us that amount of time as well if I go up here
08:28It will go up here if I go up to here, and I wait a bit
08:32Then it's gonna take a much lower smaller
08:34Well it take will take a longer time to get there, but obviously I don't want this necessarily
08:39What I really want is I want the whole animation to take place
08:42And for the whole animation to take place I need to store this
08:46Animation and for that I'm gonna put it into my into my timeline
08:49So I'm just gonna go ahead and wrap this into my TL add and just add this animation inside
08:56Now just by adding the animation. It's not gonna change much because if I add the animation
09:01I'm gonna have the information, but I'm not really gonna play it through
09:05I'm just gonna play the last thing that was
09:07But what I really want to do is I want to well what I really want to do is I want to be just in
09:11The position that I'm in and not play it at all and then have a controller that would just play the whole thing over
09:17So let's get that sorted so to get that sorted. I'm just gonna go ahead here and
09:22I want to add a pause state to my timeline, and I could do that as part of my constructor parameters literally just saying paused
09:33True now if I set it to pause true
09:36It's not gonna animate, but that's not good enough because if I set it to pause true
09:40I add the item I move it to a certain spot
09:43It's gonna go back to that initial position because I said here an animation of from two
09:48And in this point in time where our animation is we really are in that point
09:53But what we really want to do is we want to push our timeline to go to the end of its total duration currently
09:59So I'm going to go to my timeline
10:00I'm going to tell it that it's time is not going to be the time that it's in right now
10:04But we want it to be the total
10:06duration and
10:08By doing this when I go back and I click on refresh
10:11Not only that the information is going to be stored the user won't even be able to see that the information is being stored
10:17It's not going to be saying that it's going to be intrusive to the user in any means ways or forms
10:24The next step is I want to also add a controller so we could replay this whenever we want to
10:29So I'm going to go ahead here and find where we had our controllers
10:32And I'm just going to add another controller and let's call this controller
10:37How about we call it repeat or replay?
10:41Replay and I'm going to call the function on replay
10:44Our callback will be on replay
10:47And I'm going to go ahead and just create that function right after our last drag and I'm going to call it function on replay
10:56scroll a little bit up so you could see better and
10:59I'm going to go to my if I'm paused to my timeline first of all
11:04I'm going to go to my timeline seek go to position zero because I want to play from position zero
11:09And I'm going to ask if I am paused
11:11I
11:13Don't know if I am if I am paused then I want to go to the timeline paused
11:19false
11:20Literally undoing any posiness only if there was a pause now if I go ahead and do this and click on refresh
11:26I should have a secondary button now and
11:29now I could add let me add a few items and I'm just going to move them all in some sort of
11:35Path here, and I'll add another one just move it into some sort of a some sort of something here
11:41And I'm just going to go ahead and just create some crazy animations that don't really mean nothing moving things around
11:48just for the sake of
11:50animation and if I click on replay
11:54All my animations all my phases will be remembered and we will be able to move things around
12:00As we see fit, so that's it for this lesson in the next lesson. We're going to continue controlling our animations
12:07We're not walking away from this, but what I want to do is instead of having an automatic recorder
12:12Which is what we have right now is have here some sort of a toggle between automatic recording to manual
12:19Recording and the idea behind it is because right now
12:21We're animating one item at a time is that we can move all the items that we want click on record and add more
12:27Things into our timeline, so we're still playing around with the timeline, and we'll see you in the next lesson

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!