top of page
Hourglass Animation

Hourglass Animation

Flip an hourglass automatically or on a button click using the Wix Animations API.

Intermediate.png

Advanced

2K

Published:

August 11, 2021

Girl Enjoying her Drink

by

Anchor 1
Hire a Developer

Example Description

In this example we use the following 3 animations to demonstrate how to flip an hourglass automatically or on a button click:


  1. Hourglass: The flip effect of the hourglass.

  2. Balls in the hourglass: The falling effect of the balls in the hourglass. This animation is triggered once the hourglass is flipped.

  3. The flip button: Animation of the button when the site visitor hovers over it.

Example Code

Tab 1

.

Tab 2

.

Tab 3

.

This code solution can be complicated.

How We Built It


Page Elements


In our site we added the following page elements:


  • A transparent image of an hourglass with a black background.

  • A container box with the following elements:

  • A white container box the size of the hourglass to create a white background (mask) for the hourglass animation.

  • A group containing 5 circle vector images of different sizes and colors to create the balls in the hourglass.

  • A group containing 1 rectangle vector image and 1 text element to create a custom button (flip button) to trigger the flipping hourglass animation.

  • An additional identical rectangle vector image for the flip button animation.



Code


In the  $w.onReady() function we declare all the elements we want to animate. On mobile we also adjust the size of the hourglass to be compatible with mobile devices. Next we create the individual animations for each of the 3 animations:


1. balls_animation: Before creating the balls animation, we first position the balls in the hourglass at a random distance within our desired range. This allows the animation of the balls to be a bit different each time the hourglass is flipped. When doing this, we also make sure to perform certain calculations on the balls in the hourglass to mimic the behavior of sand particles moving in a real hourglass. Once the balls are positioned, we animate them to fall at our desired duration, delay, easing parameters, and x and y positions.


2. hourglass_animation: To flip the hourglass, we rotate the container with the hourglass balls and the hourglass mask by 180 degrees at a duration of 400 milliseconds. In our example, we want to run this animation automatically every 8 seconds. We simplify this by using states. Since there are 2 possible states of the hourglass, upright or flipped, we set an animation state to flip the hourglass according to that state. We then use a setTimeout() function to switch states after 8 seconds.

See the 2 states below:

  • State 1: The hourglass is upright, and the balls have already fallen. In this state, we set the hourglass flip animation to play (the hourglass to flip), and the balls animation to reverse (the balls to fall again).

  • State 2: The hourglass is flipped, and the balls haven’t fallen yet. In this state, we set the hourglass flip animation to reverse (the hourglass to go back to its upright position), and the balls animation to play (the balls to fall).


We also include the functionality of running this animation when the user clicks on the flip button by adding an onClick() event handler to the flip button.


3. button_animation: When the user hovers over the flip button, we move the flip button down and to the right, and the rectangle vector image up and to the left. This gives an illusion that the rectangle vector image slid under the button.

APIs We Used

Non-code example.

Non-code example.

Related Articles

Article Link

Hire a Developer

Velo solutions are powerful tools, but building them on your own can be challenging. Let an experienced Velo development shop build it for you, so you can keep working on your site or business.

Related Examples

Did this help?

Yes

|

No

Thanks for your feedback!

Custom Button Animation

Custom Button Animation

Create a custom button that rotates clockwise and changes color on click using the Wix Animations API.

Intermediate.png

Beginner

Solar System Animation

Solar System Animation

Create a simplified solar system animation using the Wix Animations API.

Intermediate.png

Advanced

Ball Drop Animation

Ball Drop Animation

Create a ball drop animation where balls fall through a maze and dot the letter I’s in your words.

Intermediate.png

Advanced

Text Microinteractions

Text Microinteractions

Create different microinteractions on 3 text elements using the Wix Animations API.

Intermediate.png

Intermediate

Vector Image Animation

Vector Image Animation

Create an animation of a ball moving and then taking the place of the letter O’s in your words.

Intermediate.png

Beginner

Anchor 2

This website was designed with Velo by Wix

bottom of page