Use a Bezier Curve to Move a Graphic
Hints
Hint 1
Use the animation-timing-function property with a value of cubic-bezier, calling cubic-bezier with the arguments 0, 0, 0.58, 1
.
Hint 2
This is the syntax for a Bezier curve:
animation-timing-function: cubic-bezier(x1, y1, x2, y2);
Solutions
Solution 1 (Click to Show/Hide)
<style>
.balls{
border-radius: 50%;
position: fixed;
width: 50px;
height: 50px;
margin-top: 50px;
animation-name: bounce;
animation-duration: 2s;
animation-iteration-count: infinite;
}
#red {
background: red;
left: 27%;
animation-timing-function: cubic-bezier(0, 0, 0.58, 1);
}
#blue {
background: blue;
left: 56%;
animation-timing-function: ease-out;
}
@keyframes bounce {
0% {
top: 0px;
}
100% {
top: 249px;
}
}
</style>
<div class="balls" id= "red"></div>
<div class="balls" id= "blue"></div>
Code Explanation
- Remember that all
cubic-bezier
functions start withp0
at (0, 0) and end withp3
at (1, 1). In this example, the curve moves faster through the Y-axis (starts at 0, goes top1
y value of 0, then goes top2
y value of 1) than it moves through the X-axis (0 to start, then 0 forp1
, up to 0.58 forp2
). As a result, the change in the animated element progresses faster than the time of the animation for that segment. Towards the end of the curve, the relationship between the change in x and y values reverses - the y value moves from 1 to 1 (no change), and the x values move from 0.58 to 1, making the animation changes progress slower compared to the animation duration.