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

Dragging elements with Draggable

As we crack into this chapter, we're going to take TimelineLite control to a completely new level. In this chapter we move away from preconfigured animations and move the control to the hands of the end users. To enable this control we need to create Draggable components. In this lesson we will add SVG circles onto the HTML page and then use the Draggable component to make them draggable.

Video Transcript

00:00Our goal in this chapter is to create a really sophisticated timeline that the user controls.
00:07In essence, we want to let the user add things onto the stage, move them around to capture those movements,
00:13and then replay those movements to the user whenever they want to.
00:16And that's what we're going to do throughout this chapter, and even the next chapter will continue digging deeper and deeper into it.
00:22But before we do that, we want to add an engine that will add SVG elements onto the stage.
00:29So you could add whatever you want, we're just going to add an SVG element, show you a little bit of SVG by creating a circle together,
00:36adding it to the stage, and then making a drag and drop functionality using the Green Socks draggable component.
00:42So let's get started.
00:44Excitement in essence.
00:46And this lesson is brought to you with the generous support of the Cup of Whiskey that I have near me.
00:51So once in a while, you probably will hear me taking a swig.
00:54It's been a long day of recording.
00:56But it's also even fun when I have a cup of something nice to drink with me.
01:02Without further ado, what are we going to do in this chapter in general?
01:05And in this chapter, we're not walking away from timelines, but we don't want to get bored, so we're not going to only do timelines.
01:11And we're going to take a peek at another really cool utility of the Green Sock library,
01:17which is called draggable that enables us to drag things around really easily.
01:23Beyond that, what I'm going to do, Oto, is in this lesson, we're going to basically set up and configure a way for users to add things onto the stage dynamically.
01:31We're going to create some sort of a SVG circle, and users will be able to click and add another circle, click and add another circle, and drag them and move them around.
01:39That's going to be the premise of our lesson.
01:41And throughout the chapter, we're going to let the user themselves create the timeline.
01:46Very exciting stuff, very advanced stuff as well.
01:49So let's get started, and first off, let's jump right into our source files.
01:53And I just want to show you that the HTML, the body itself, is completely empty.
01:57We are loading up jQuery, we are loading up TweenMax, and we are obviously loading up our chapter for source files.
02:03In our CSS, there's a few helpers that we're going to look at them a little bit later.
02:07One of them is our control bar. One of them is a button itself, so that way we don't have to do the CSS together.
02:14And last but not least, is the elements that we're going to be throwing onto the stage.
02:18You can see that the name implies you could add whatever you want, not only the elements that we're putting positioning on the stage.
02:25So that's it. Let's get started working.
02:28Now, the first step that I want to do is I want to create that controller bar that we were talking about and start adding buttons into it.
02:34So to do that, I'm just going to call a function and create it also.
02:37And I'll call it setup controls, and I'm just going to go ahead here and copy this function,
02:47create it really quickly, and let's start creating that control bar.
02:55So I'm going to go ahead and just approach, and I'm just going to call that variable control bar,
03:00and I'm going to approach jQuery and just ask it to create a new div.
03:08Once I create my new div, I'm going to open to it an attribute, because I want to go to that control bar,
03:14and I want to add an attribute that basically will help it identify itself to the CSS attribute that it has,
03:21and also so it will have easier ways to reference it.
03:24So I'm going to give it an idea of control bar, control, control bar.
03:34I'll make sure that control bar, control bar, beautiful.
03:37So that should be good. It's not really visible at this stage because there's nothing visible in the control bar itself.
03:42So let's continue on. We also didn't add it to the stage, yeah?
03:46The next step that I want to do is I want to actually add something to it.
03:49So I'm going to add a button, and to add a button, I'm going to create basically a function that will add buttons for us.
03:54So I'm just going to call this create control, create control, and I'm going to call it add because we're going to call this add this control.
04:05All right, now let's go ahead and create that control really quickly, and I'm going to copy that create control function.
04:14So we have it ready here, and that will be our label.
04:18And I'm going to go ahead and immediately create that controller, and the controller itself is literally just going to be a button.
04:24So I'm going to call it btn, and I'm going to just create a new span here because I want to have a span.
04:34And we just created ourselves a nice little span, and I'm going to go ahead and set its copy to be that label that we just got through.
04:41And I'm also going to set my btn to have an attribute of that specific class, which our class itself was btn.
04:50All right, so now we have a button that has an attribute.
04:55We're going to also add a click event to it in a second, but let's just go ahead and add it to the stage.
05:00But what I really want to do is add it to that control bar that I've just created, and I also want to add a click event.
05:05So how about I add those extra parameters?
05:08One is I want to have some sort of a click event here, so I want to have some sort of a callback.
05:13So I'm going to call that callback function on add, and I want to also send the control bar,
05:19and I want that control bar to be basically the parent or the host of this button.
05:23So I'm going to go ahead and just add these attributes.
05:26This is going to be the callback.
05:28And last but not least, it's going to be our parent.
05:31So let's go ahead and first, as parents, let's just add this button into its parent.
05:36So to do that, I'm just going to go ahead here and type your parent.append,
05:41and I'm just going to add this button right inside.
05:44And even before we wrap this up, I'm going to go ahead to my button,
05:48and I want to add a click event to it and call that callback.
05:52All right, so that should be all that should be said.
05:56All I really need to do is create that on add.
05:58So I'm going to just go ahead here and create that on add.
06:02And for now, let me just put here a console log and just say hello.
06:07And before we start this, because if I go ahead and start this right now,
06:10nothing will happen.
06:11I also need to append my actual button control into the stage itself.
06:15So once we were done with creating our controls,
06:17I could go ahead and just append it right into my body
06:20because I have your nice empty body.
06:22So I'm going to go and append that and add that control bar right into our HTML.
06:29So if I go ahead and click on save, go and refresh it,
06:32I should have your nice little add button.
06:35And if I click on the add button, I should see that hello. Beautiful.
06:38Next step that's left for me is to actually have it actually add things.
06:42So let's jump right in there and let's continue going into that add function.
06:46What I want to do is instead of having here a silly hello,
06:49I want to actually create something.
06:51So I want to create an SVG.
06:53So I'm going to go ahead here and what I want to do is I'm going to create
06:56basically a colors array because I want to create here many different
06:59colors, many different circles.
07:01And I already prepared in advance a pile of colors.
07:04So let me just copy that really quickly.
07:07And here we go.
07:08I just created a random array of different colors
07:11that we could have pre-configured in advance.
07:13And what I really want to do is just randomly assign a color
07:16to the circle that we're going to create immediately.
07:18So let's go ahead and just see that we're creating here a completely
07:23random color console log.
07:25I'm just going to go to a colors and I'm going to select a math random.
07:32And I'm going to duplicate it by a colors length.
07:36And last but not least, I want to do a math floor
07:40to all that value that we just got.
07:42So that way I'm going to get a value between zero
07:44and the one one less than the length,
07:47which would give me basically each time a different color
07:49or randomly assign a color.
07:51And if I go ahead here and click on refresh,
07:53every time I click on add and I have your little a colors
07:57is not defined, which means I'm probably referring to it in a,
08:00oh, a color, I called it a not a colors.
08:03So let me a color.
08:05And I'm going to go ahead here and refresh again.
08:10And if I click on refresh, we'll see that each time
08:13I'm going to get a different of a different numeric value.
08:16It's going to be completely random.
08:18I'm happy with this randomness and now we're ready to take this color.
08:21So I'm just going to take this, I'm going to call it clr for color.
08:25And I'm just going to save this variable for a second
08:27and then move on to start creating my SVG element.
08:30So to create my SVG element, I could literally just send my SVG
08:33directly into jQuery again and create on the fly that SVG.
08:38So I'm just going to go ahead here and just create a very
08:41primitive basic SVG, starting with setting the SVG
08:45and closing the SVG, just like any HTML attribute.
08:50The next thing is I need to define the height of the element.
08:53So I'm going to go ahead and define a height of 100,
08:56a width of 100.
08:58So now I have a bounds and area for my SVG.
09:01Next thing that I want to do, and I could break lines,
09:03it's completely fine.
09:05The next thing that I want to do is I want to create a circle.
09:08And again, I want to close that circle.
09:11And by the way, because it's a shape, a self-contained shape,
09:14I could just open and close it in the same parameter.
09:17Next step is to define my position where I'm starting my animation from.
09:21And I'm going to set my x position, my circle x position,
09:25to be at 50.
09:27And I'm going to set my circle's y position to be at 50 as well,
09:34because I want it to be smack in the middle.
09:36And then I'm going to define my radius.
09:38I'm going to set that to be 40.
09:40And I'm going to set auto my stroke color.
09:43And I'm going to set that to be black.
09:45And I'm going to set auto my stroke width.
09:49And I'm going to set that value to be 2.
09:56And let me just add here again a word wrap,
09:59so we could see all in the same line.
10:02And last but not least, we also want to add a fill color.
10:07And I'm going to go ahead and just add the value of our CLR into it.
10:12And there we go.
10:13We just created an SVG with a color.
10:15All that's left for us to do is to append that into our body.
10:18So I could literally just go ahead and add it to my body.
10:22And I almost forgot, but not really, is that if I add this,
10:28it's going to have a relative position.
10:30And if it has a relative position, that's not really what I want.
10:34I want it to have the position that is absolute,
10:38so I could drag it around whenever I want to,
10:40which is going to be our next stage.
10:41So I'm going to just go ahead here.
10:43And before I even add it to the stage,
10:44I'm going to go to my SVG element.
10:46And I'm going to say to it, your attribute of class
10:50is going to be elements.
10:58All right, so let's give it a try
10:59and see if it's actually rendering.
11:01If I click on Refresh, hopefully, if we don't have any errors,
11:04which we do, unmatched string literal at line 29.
11:09Let's see what's going on in line 29.
11:12And I think that's because we're breaking a line
11:14and maybe it's confusing our browser.
11:18After all, even though.
11:19So I'm not going to break any lines.
11:20I'm just going to connect it all together again.
11:22Click on Save.
11:23And let's give it another try
11:24and see if we're still seeing any issue.
11:29Beautiful.
11:31All right, so we're not getting an error.
11:32If I click on Add, we're getting an item.
11:35I click on Add again.
11:36It's overlapping each other, so we're in a good start here.
11:40The next step for us to do is to actually make sure
11:43that whatever our element is that we're auto-going to.
11:47Oh, by the way, one of the stuff that I'm seeing here
11:49that is probably incorrect,
11:51and maybe because that the color is not coming
11:53the way we want it to come through, is that we also...
11:56No, I think the color...
11:58Nope, that is fine.
11:59I think the color is coming in through.
12:00So the next step that we want to do
12:01is actually make it draggable, so we'll be able to see it.
12:04So I'm just going to go ahead here
12:05and just go back into my browser
12:08because I want to load up the draggable component.
12:11So we have tween max loaded,
12:12but not the draggable because it's an extra.
12:14So I'm going to go ahead here
12:15and just load up draggable as well,
12:19and I'm just going to add it into my HTML.
12:21Just make sure you go to the source files
12:23at the Green Socks library, download draggable,
12:26and now that we downloaded draggable,
12:30we could literally just go to any HTML element
12:32and just create a new draggable event to it
12:35by literally just sending it through
12:37and letting it know that this item is now draggable,
12:40and literally just by doing that,
12:41our item should become draggable.
12:43So if I add this item, if I move it around,
12:46I should be able to add multiple different items
12:49without any problem.
12:51Now, I could see that my outline isn't really coming out
12:53the way I thought it would.
12:55I probably have a typo in the outline,
12:57so let's go ahead there and just fix that really quickly.
13:00So when I'm looking at my stroke width,
13:03stroke width,
13:09very odd because that looks totally fine to me.
13:12Stroke, let me set it to be three even, or four.
13:16Let's set it to be four, blank.
13:19That might have done it, black.
13:23And let's click on refresh here.
13:26And now we should have an item
13:28that has a nice little stroke around it,
13:32and we are done with this lesson.
13:35And what we're going to do in the next lesson
13:37is we're going to take this draggable elements
13:39that we have here and start adding back into our timeline
13:42and seeing how could create something with a timeline,
13:45how could we dynamically create some sort of an animation,
13:48let the user basically be the director of our movie.
13:51And that's what we're going to start doing immediately
13:53in 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!