FORUMS: list search recent posts

Expression to mimic hover state for rectangular shaped layers

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Tiffany Scobey
Expression to mimic hover state for rectangular shaped layers
on Oct 17, 2016 at 11:38:53 pm

Hi there,

I'm looking for the best way to mimic a hover state in after effects.

I make a lot of tutorial videos where I animate a cursor placed over a "button layer" and
then an "active button layer" over it is trimmed to only appear while I have the cursor over it.

I have been using this expression to affect the opacity of the "active button layer" when the cursor's anchor point gets within a certain distance of the anchor point.

min_distance=49.9;
max_distance=50;
c=comp("MAIN COMP").layer("cursor_black.eps");
d=length(to_world(anchor_point), c.to_world(anchor_point));
linear(d, min_distance, max_distance, 100, 0)


This works great for buttons that are square or circular (where the anchor point is pretty much the same distance from all the edges), however, buttons that are rectangular where the distance from the anchor point to the edge of the layer is very different depending on whether you measure from the anchor point to the top edge or bottom edge vs from the anchor point to the left edge or right edge.

Although this is working pretty well, I'm not sure if distance to anchor point is the best way to go about achieving my goal or if there is maybe some way to use the alpha information of the layer so it can be exact and I don't have to change the min and max distance in the expression for each button's dimensions, if thats even possible.

I tried using Dan Ebberts' Collision Detection expression,
which seemed like a great answer, but I could not get it to work. : /


Thank you in advance!


Return to posts index

Dan Ebberts
Re: Expression to mimic hover state for rectangular shaped layers
on Oct 18, 2016 at 12:19:38 am

Try this. It should work if the comps are the same size and aligned.

c=comp("MAIN COMP").layer("cursor_black.eps");
r = sourceRectAtTime(time,false);
ul = toComp([r.left,r.top]);
lr = toComp([r.left+r.width,r.top+r.height]);
p = c.toComp(c.anchorPoint);
if ((p[0] > ul[0]) && (p[0] < lr[0]) && (p[1] > ul[1]) && (p[1] < lr[1])) 100 else 0


Dan



Return to posts index

Tiffany Scobey
Re: Expression to mimic hover state for rectangular shaped layers
on Oct 18, 2016 at 12:32:40 am

Dan,

Wow, that works beautifully! Exactly what I needed!

Thank you so much for you're quick help!!! ?

-Tiffany


Return to posts index


Tiffany Scobey
Re: Expression to mimic hover state for rectangular shaped layers
on Nov 21, 2016 at 6:51:00 pm

Hey Dan,

Would you happen to know if there's an alternative to sourceRectAtTime? Because it works great , but if I have things in multiple comps at different times, then it has issues.


Tiffany


Return to posts index

Dan Ebberts
Re: Expression to mimic hover state for rectangular shaped layers
on Nov 21, 2016 at 6:56:47 pm

There's a hack where you can iterate sampleImage() to find the edges of layers, but it's pretty ugly and can be a cpu hog.

Give me an example where sourceRectAtTime() doesn't do what you want.


Dan



Return to posts index

Tiffany Scobey
Re: Expression to mimic hover state for rectangular shaped layers
on Nov 21, 2016 at 7:12:25 pm

If I have a comp that is used more than once at a later time in the project or comps that don't start at 0 then the "AtTime" part of the expression kinda throws it off. Since I have many comps within comps, it's verry difficult to try and match the timing of each comp for the expression to work.


Tiffany


Return to posts index


Dan Ebberts
Re: Expression to mimic hover state for rectangular shaped layers
on Nov 21, 2016 at 7:44:01 pm

Multiple precomp instances is definitely an issue, but having a precomp offset from time zero can be handled with a little adjustment to the expression:

C=comp("MAIN COMP");
cursor = C.layer("cursor_black.eps");
L = C.layer(thisComp.name);
r = sourceRectAtTime(time,false);
ul = toComp([r.left,r.top]);
lr = toComp([r.left+r.width,r.top+r.height]);
p = cursor.toComp(cursor.anchorPoint,time + L.startTime);
if ((p[0] > ul[0]) && (p[0] < lr[0]) && (p[1] > ul[1]) && (p[1] < lr[1])) 100 else 0


Dan



Return to posts index

Tiffany Scobey
Re: Expression to mimic hover state for rectangular shaped layers
on Nov 21, 2016 at 7:49:23 pm

Perfect! Thank you Dan!

Tiffany


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]