I am currently exploring how to create an animation similar to the "Ken Burns" effect using CSS transform properties. While I have been using object-position to animate, I am facing challenges with the fluidity of the movement.
I am seeking help to achieve a smoother animation by utilizing CSS transform properties like translate or scale exclusively.
In addition, I have developed a demonstration of my current approach (or you can view it on Codepen):
body {
display: flex;
}
.image-container {
width: 150px;
height: 150px;
border-radius: 10px;
overflow: hidden;
}
.object-position .image-container img {
width: 150px;
height: 150px;
animation: kenBurns-object-position 20s linear infinite;
object-fit: cover;
}
.transform .image-container img {
width: 150px;
height: 150px;
animation: kenBurns-tranform 20s linear infinite;
object-fit: cover;
}
@-webkit-keyframes kenBurns-object-position {
0% {
transform: scale(1.2);
object-position: left top;
}
100% {
transform: scale(1.0);
object-position: center;
}
}
@-webkit-keyframes kenBurns-tranform {
0% {
transform: scale(1.2) translate(-50px,0px);
}
100% {
transform: scale(1.0) translate(0px,0px);
}
}
<div class="object-position">
<h1>Animation using "object-position"</h1>
<label>image ratio: 1x1</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/500x500">
</div>
<label>image ratio: 2x1</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/1000x500">
</div>
<label>image ratio: 1x2</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/500x1000">
</div>
</div>
<div class="transform">
<h1>Animation using "transform"</h1>
<label>image ratio: 1x1</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/500x500">
</div>
<label>image ratio: 2x1</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/1000x500">
</div>
<label>image ratio: 1x2</label>
<div class="image-container">
<img src="https://source.unsplash.com/featured/500x1000">
</div>
</div>
Key considerations:
- I require the animation to seamlessly adapt to images of different aspect ratios, whether horizontal, vertical, or square. The demo on CodePen features examples of all three formats as a visual aid.
- The end goal is to replicate the effect achieved with object-fit but with a much smoother motion. Currently, the animation appears jittery, especially during longer durations (plans are for animations lasting between 40 and 60 seconds).
Thank you in advance for your time and support. Any assistance in tackling this challenge would be greatly appreciated.