Animating SVG attributes
When animating with the GSAP, by default it figures out on its own what it's supposed to animate: The CSS, the properties of an object, and so forth. In this lesson, we're going to see how can we animate attributes of any object. In a real world scenario not all properties will work - a we'll solve that problem in the next lecture.
Video Transcript
00:00We created an SVG little circularly thing that we're going to use as our buttons.
00:05Now throughout this lesson, we're going to actually switch our buttons and use them,
00:09create the rollovers and all the really great things that we could do with SVG.
00:13Now, the reason why we're using SVG just because it's cool and just because it's modern,
00:17but beyond that also because in our next lesson,
00:19we're going to see how do you animate some properties that are just not intuitively
00:23animated or where to green sock library animation library just gets confused and to unconfuse it,
00:30we need to use a plugin.
00:31We're going to see in what scenarios does it get confused and that's it.
00:36Let's just start getting our slideshow working and it's going to be even better than our slideshow
00:40before because we're going to have more colors and effects and oh my gosh,
00:43this is going to be fun with our hour recording.
00:45And what I want to do next is I want to highlight our elements as our animations are happening.
00:50So as I roll over the elements,
00:52I want them to have a hot spot just like we had with our button.
00:56So let's go ahead and do that and throughout this lecture,
00:58we're going to meet a new plugin which is called the attribute plugin.
01:03So let's do this.
01:04All right.
01:05So in our code,
01:06what I want to do first is I want to locate the scenario where I want to highlight my element
01:11and it's going to happen inside of our slideshow
01:13and because we're going to update our slideshow now,
01:15let me upgrade our version to version two zero zero two because we're making a change.
01:20So really what I want to get is I want to get rid of those classes of active and not active
01:25and literally right here.
01:26I want to create the logic or command the logic.
01:29Now speaking of that,
01:31we don't really need in our CSS.
01:33If we search in our CSS,
01:34we can get rid of also everything that relates to our A in our panel
01:39because we're never going to really use it anymore
01:41because now we're doing something programmatically.
01:43So we can get rid of a little bit of file size.
01:46All right back into our logic.
01:47Let's see.
01:47How do we build this logic out?
01:50So step number one,
01:51what I want to do is I want to basically call my I'm going to create a function,
01:55a method that belongs to the slideshow that will basically do the job of highlighting buttons.
02:00So to do that,
02:00I'm going to just approach my that which will give me access to my object
02:04because we created that data object externally.
02:07And next I could go ahead and call the function that we're going to create right now,
02:11which I'm going to call highlight.
02:13High, high light button and I'm going to get send in the current index
02:19and recall we just set the index itself earlier and we defined our variable.
02:25So I'm just going to go ahead and send my index in
02:27so I could then refer back to the what button was just selected.
02:31So that's it.
02:31That's all the logic I want to change inside of my rollover itself.
02:35And what I'm going to do next is I'm going to go up here at some point above
02:39and continue and actually create that actual logic.
02:41So I'm going to do that right after our definition of the index,
02:44but it really doesn't matter because we're working with methods.
02:48So it doesn't really matter the location itself of the method itself
02:51as long as it's inside of our constructor.
02:53So I'm going to go ahead and create the function called highlight button
02:58and it's going to receive one property which is the index itself.
03:03And now it's time for us to build out the logic.
03:05So the logic is really simple.
03:07What I want to do is I want to run through all the buttons
03:10and bring them back to their default value.
03:13And I also want to animate my current item into the color that it should be now.
03:18So let me start off with highlighting the color that it needs to be.
03:21So I'm going to go ahead and do a two and then select our current button
03:25that was selected which is in our buttons index.
03:29Because we have a reference to our buttons index.
03:32Next is we'll define how long is the animation.
03:34I'm going to have it for a half a second.
03:37And last but not least we want to define the color that we want to change.
03:40In this case we want to change our fill color.
03:42So I'm going to go ahead and define my fill color
03:45and to make this really creative and fun instead of referring to a static value here.
03:50I'm going to go back to one of our background colors
03:52and set that inside of our button as well.
03:55So to do that I'm going to refer to our this
03:57and notice how when I'm working with a method that belongs to our object
04:02I could continue referring to this.
04:04Contrary to a method that belongs to our buttons,
04:07I couldn't use this anymore and I had to use a work around
04:10a variable to give me access to our object.
04:14In this case I could go ahead and use the this
04:16because we're actually inside of our button right now.
04:19So that this would always refer to our slideshow itself.
04:23All right. So next step is we want to define.
04:25So I'm going to go to the colors array that we had up there
04:29and literally just send in our index and that's it.
04:31Once I've done that whenever I roll over an item
04:34it's going to automatically get the color of it
04:37the background of the section that it belongs to.
04:39So if I save this go back into my application and click on refresh
04:43we're going to see that whenever I roll over an item
04:46it's going to automatically get that background color.
04:49Now we still have a couple of problems.
04:50One is it never gets unhighlighted.
04:53So let's solve that problem.
04:54We're going to have to use here or we have one of two options.
04:58Now the more saving saving programming saving
05:02if it's if we had there like hundreds of buttons
05:04we probably would want to create a simple logic
05:06such as storing the last thing that was highlighted.
05:09So I could create here some sort of variable
05:11which I'll leave that to you as your homework
05:14that will store the last thing that was highlighted
05:16and every time we highlight something new
05:18it will automatically dehighlight the last thing
05:21that was highlighted.
05:23That would be the most efficient way of doing it.
05:25I'm going to do it in a less efficient way
05:27just for the sake of practicing the skills
05:30that we learned so far
05:31but I would love for you to show me
05:33how could we do this without using a for loop
05:36and we really is no need to have a for loop
05:38but I'm going to create a for loop anyways.
05:40So I'm going to go ahead here
05:42and I'm going to create a var I
05:44and what I want to do is I want to loop through the buttons again
05:46so I could go ahead here and just copy this
05:49and just loop through my button.
05:51This let me ask you a question that I will answer immediately.
05:54Why in the case of buttons
05:56I'm not putting here this dot buttons
05:58but on the case of colors
06:00I'm putting this dot colors.
06:02The answer is really simple.
06:04Because we're living right now inside of this function
06:07when I can't find a variable buttons inside of this method
06:11it will automatically move to the scope above it
06:13which is basically right here.
06:15So physically just steps out to see
06:17is there another definition of button somewhere
06:19and there sure is there is a definition here of button
06:22that we defined in jQuery
06:23and there's also a definition up here.
06:25So this definition is passes through our method
06:28because our method is living inside of this larger function
06:31and that is why we don't need to put the word this before.
06:34We could because we also created access to that button
06:38with the this command
06:39but in our case we could literally just type button
06:41and we would have the same exact type of access
06:44so we don't have to do it.
06:46Contrary to that with colors
06:47we never created a variable called just colors.
06:50It was always assigned to the object.
06:52So for us to be able to have access to it
06:54we need to explicitly say that we're going to the object.
06:58With that out of the way let's continue with our logic.
07:01So we're basically looping through every single button
07:04and what I want to do is I want to ask
07:05if our index is not equal to I
07:09that means every other button besides our current button
07:12what I want to do that I want to tween it
07:14but this time around I want to tween it
07:16back to its original color.
07:17So I'm going to go ahead there
07:18and just send the object that we want to tween
07:21the amount of time we want it to take
07:23and last but not least the property
07:24that we want to animate in our case
07:26we want to animate our fill color
07:28and I want to set it to be white
07:29which I could even send an action
07:31the actual copy white.
07:33Now this is not the efficient way
07:36if we had 10 buttons there
07:37we're going to be doing 10 commands
07:39when we could have easily just created one command
07:42if we stored a variable.
07:44Now I really want to give you that as homework
07:47and I want to see if you could do it on your own.
07:48Now I'm not going to give you an answer to it.
07:50The only way you're going to get an answer to it
07:52is if you check out the comments
07:54and you post your comment in our website
07:57and then we'll talk about it
07:58and see if you came up with a better solution.
08:01So there is definitely a better solution
08:03and I want you to find it out
08:05because that is what is going to really determine
08:08if you're an okay developer or a great developer.
08:11When you see scenarios like these
08:13that a loop is just not needed
08:15and a simple variable could save us a lot of looping
08:18and a lot of extra code
08:20because this is going to run X amount of times
08:22and exponentially really
08:24because if we had a hundred buttons there
08:26this would run a hundred times.
08:27In our case it's only running four times
08:29so it's not the end of the world
08:31but we definitely want to try to think always
08:33to keep things the most optimized as we could.
08:36Now contrary to line externally where we have a loop here
08:40it isn't as much of a big deal
08:41because we're not doing this every single rollover.
08:44This happens only once when we instantiate our object.
08:48When we're creating a loop inside of our highlight button
08:50we're actually going to call this every single time
08:53or we roll over something.
08:55So if I roll over five items really quickly
08:57I'm going to be running five times the amount of this loop.
09:01So this could become really a lot.
09:02So if I had a hundred buttons
09:03and I rolled over really quickly
09:05if I went over into my application
09:07and I literally just did like something like this.
09:09So if I refresh this for a second
09:11if I literally went in here
09:12and I just did like this a lot of times
09:13I'm literally I could create
09:15I could crash a site this way
09:16if there was a hundred buttons there.
09:18So just by going in and out in and out in and out
09:20I could create a site that would crash.
09:22Now obviously sometimes you need to use a loop
09:24but that doesn't mean you always need to use a loop.
09:27And in this scenario
09:28it's the absolute wrong time to use a loop.
09:30And I encourage you to figure out how to do it better
09:33and I hope you will solve that conundrum.
09:36And if you do we're going to talk about you
09:38we're going to get an answer from me live
09:39and we could chat in the chatting area of our website
09:43with yourselves with other students and with me.
09:45All right, so let's continue.
09:46So now that we got it running
09:48and let me just make sure that it really what is running
09:50we'll see that when we roll over different elements
09:52they're going to get highlighted
09:54and they're going to get unhighlaid which is beautiful.
09:56This is exactly what we wanted it to do.
09:59The next step that I want us to do
10:01is I want us to actually animate these items in initially.
10:04And I'm doing this because well,
10:07let's break this down into two videos
10:08because this is getting to be a little bit long.
10:11What I want to do next is that
10:12I want to actually move into using the plugin
10:14the text plugin, sorry, the attribute plugin.
10:19We're going to see why and when do we want to use it.
10:21And we're going to do that immediately in the next lecture
10:23continuing our line of thought right now.
