Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression to mimic hover state for rectangular shaped layers

  • Expression to mimic hover state for rectangular shaped layers

    Posted by Tiffany Scobey on October 17, 2016 at 11:38 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!

    Tiffany Scobey replied 9 years, 5 months ago 2 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    October 18, 2016 at 12:19 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

  • Tiffany Scobey

    October 18, 2016 at 12:32 am

    Dan,

    Wow, that works beautifully! Exactly what I needed!

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

    -Tiffany

  • Tiffany Scobey

    November 21, 2016 at 6:51 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

  • Dan Ebberts

    November 21, 2016 at 6:56 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

  • Tiffany Scobey

    November 21, 2016 at 7:12 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

  • Dan Ebberts

    November 21, 2016 at 7:44 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

  • Tiffany Scobey

    November 21, 2016 at 7:49 pm

    Perfect! Thank you Dan!

    Tiffany

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy