In this post, you will learn how to create a hover button effect using expressions in After Effects.
Alright, so we are in After Effects, now let me show you what we are going to be creating today.
So we have a CLICK HERE button along with a cursor. Watch what happens when I take my cursor and hover it over the button.
As you can see, two things changed. The shape of my cursor is no longer an arrow. It is now a hand. Also, the color of my button is now red instead of blue.
Pretty cool, right?
So if I move the cursor away from the button, the hand will switch back to the arrow and my button will change back to blue instead of red.
Now let me show you how to set it all up using expressions in After Effects.
The Setup
So here is my setup in After Effects.
I have a Source Text, which I will use for the expressions portion of this post. I can show you visually what I am doing with expressions.
Then I have a CTRL null that controls both of my cursors.
So both the MOUSE ARROW and MOUSE HAND are parented to the CTRL null.
Next, we have the CLICK HERE text layer.
And lastly, we have the shape of our button.
Activating Expression
I am going to ALT+RIGHT CLICK (OPT+RIGHT CLICK on a Mac) on the stopwatch icon of my Source Text.
Next, I am going to define some variables.
So I am going to say point, you are going to be the position of our CTRL layer.
point = thisComp.layer(“CTRL”).transform.position;
Next, we are going to definitely our shape.
I am going to type shape = and then I will pick whip to the ORIGINAL SHAPE layer of our button.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
Sample Image Expression
Next, we are going to use the sample image expression.
So I am going to say shape.sampleImage(), and we are going to definite our point inside of the parenthesis along with the range. So I am going to say the sample is going to be between .5 and .5.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
shape.sampleImage(point, [.5, .5])
Now watch what happens, so right now we have 4 different numbers in my composition above our button. They are
all zeros.
However, as soon as I move my null and if I hover over my shape button, you can tell that everything has changed.
Now we have different numbers but the last number went from 0 to 1. So that is the one I am after and I am pretty sure it is the Alpha Channel.
So how do I get only this last number? Let me show you!
I am going to grab it by using an index. I am going to say give me the index number 3 because in After Effects we count our index starting at 0. So our fourth number is really 3. Because we start at 0 and then we go to 1, 2 and then 3. I hope that makes sense.
With that said, inside these square brackets, I am going to say 3.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
shape.sampleImage(point, [.5, .5])[3]
And if I let go, you can see that it gives me 1 when the cursor is over the button.
It will give me 0 if the cursor moves away from the button.
That is exactly what I am after here but I want to convert that 1 to 100. So instead of it going from 0 to
1, I want it to go from 0 to 100.
So I am going to give it a variable here and I am going to say A you are going to equal to this expression: shape.sampleImage(point, [.5, .5])[3].
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
Next, I am going to do a linear expression, so I am going to say linear() and then in my parentheses, I am going to say A, I want the number from variable A starting at 0 to 1 and when it is a 0 I want it to be 0 and when it is 1, I want it to be a 100.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 0, 100)
So if I let go, right now I am getting 0 when the cursor is not on top of the button.
But if I hover over my button, it went from 0 to 100. So that is exactly what I want.
Next, I am going to select the text and copy it.
Now I am going to go inside my MOUSE ARROW by first selecting it and then pressing T on the keyboard to reveal the opacity.
Click on the stopwatch icon to create an expression.
And then I will paste that expression inside the text editor.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 0, 100)
So if I let go, you can see that my arrow is sitting on top of my hand cursor.
But when I pull it away from the button, the arrow disappears.
We need to reverse that effect. I want the arrow to be visible when it goes over my button and I want it to disappear when it is not over my button.
So to do that, I am going to change a 0 to 100 and 100 to 0.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)
Okay, so now everything is in reverse. We see the arrow when the cursor is not over the button.
And we see the hand when the cursor is on the button.
So that’s good. Next, I am going to select my MOUSE HAND and hit T to reveal the opacity.
Then I will ALT+CLICK (OPT+CLICK on a Mac) on the stopwatch icon to activate the expression.
And we are going to paste the expression in here again
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)
This time we do not have to reverse anything because this expression does exactly what I want it to do.
So as you can see, the hand cursor disappeared when I am not hovering over the shape.
But if I go over the shape, the arrow goes away and the hand appears. That is exactly what I want.
Now let’s work on changing the color of the shape.
To do that we first need to select the ORIGINAL SHAPE layer.
Then press CTRL+D (CMD+D on a Mac) to duplicate the ORIGINAL SHAPE layer.
Change the name of it to BG for background.
I do not want to use the ORIGINAL SHAPE. I only want to use that for the sample image. So I do not want to touch it.
Now I am going to change the color of this BG shape. I am going to go into Effects and Presets and grab Fill and then drop it over the top on BG.
We are going to change the color of our shape.
So we have the blue color and we want it to switch from red and blue.
I am going to select the BG shape layer and then hit E to reveal the effects.
Then I am going to go inside Fill, select Opacity and hit S twice to solo Opacity.
I am going to ALT+CLICK (CMD+CLICK on a Mac) on the opacity stopwatch icon and just paste our expression inside the text editor.
point = thisComp.layer(“CTRL”).transform.position;
shape = thisComp.layer(“ORIGINAL SHAPE”);
a = shape.sampleImage(point, [.5, .5])[3];
linear(a, 0, 1, 100, 0)
That is pretty much it! So now if I drag my arrow over the button, the button will automatically change from red to blue and my arrow cursor will switch to hand.
I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.