Animate CSS background-position with smooth results (sub-pixel animation)
Checkout this example:
#content { height: 300px; text-align: center; font-size: 26px; color: #000; position:relative;}.bg{ position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: -1; background: url(http://www.gstatic.com/webp/gallery/1.jpg) 0% 0% repeat; animation-name: MOVE-BG; animation-duration: 100s; animation-timing-function: linear; animation-iteration-count: infinite;}@keyframes MOVE-BG { from { transform: translateX(0); } to { transform: translateX(-187%); }}
<div id="content">Foreground content <div class="bg"></div></div>
jQuery background-position animation to run more smoothly
You want to use and look into CSS transitions and CSS animations for real smoothyness.
-webkit-animation: move 30s linear 0s infinite alternate;
-moz-animation: move 30s linear 0s infinite alternate;
@-webkit-keyframes move {
from { background-position: 0px 0px } to { background-position: 0px 400px }
}
@-moz-keyframes move {
from { background-position: 0px 0px } to { background-position: 0px 400px }
}
Demo: http://codepen.io/anon/pen/EaEMvy
Firefox CSS Animation Smoothing (sub-pixel smoothing)
The rendering engines for each browser is obviously different. Firefox does not implement an anti-aliasing effect on CSS animations. This does not inherently make it better or worse, it just depends on what you are animating. Linear transitions can appear undesirably blurred in Chrome for example.
It appears what you would like to achieve is to have an anti-aliased/sub-pixel smoothed transitions. We can't change the way the engine renders but we can manipulate the animation to appear softer to the end user.
ALL IS NOT LOST
I have modified your answer and rendered a smoother version next to your original. This should appear softer when viewed in Firefox.
CLICK FOR COMPARISON
Techniques used for this effect:
- Linear transitions instead of ease.
- Box-shadow on animated object. (Softened edge helps create fake AA effect).
- Rotate object. Adding the smallest rotate helps to better utilised the rendering engine.
CSS
#parent {
width: 50%;
float:left;
height: 326px;
background-color: yellow;
background: url(http://paint.net.amihotornot.com.au/Features/Effects/Plugins/Render/Grid_CheckerBoard_Maker/Grid_CheckerBoard_Maker.Paint.NET.001.png) top center repeat;
}
#child {
position: absolute;
top: 75px;
left: 150px;
width: 100px;
height: 100px;
background-color: black;
box-shadow:0 0 1px rgba(0,0,0,0.7);
animation: range-y 10s infinite linear;
-webkit-animation: range-y 10s infinite linear;
}
#move-x {
animation: range-x 10s infinite linear;
-webkit-animation: range-x 10s infinite linear;
}
#move-y {
animation: range-y 15s infinite linear;
-webkit-animation: range-y 15s infinite linear;
}
@keyframes range-x {
0% {transform: translateX(0);}
30% {transform: translateX(-8px) rotate(0.02deg);}
50% {transform: translateX(1px) rotate(0deg);}
65% {transform: translateX(6px) rotate(0.02deg);}
80% {transform: translateX(0px) rotate(0deg);}
89% {transform: translateX(-3px) rotate(0.02deg);}
100% {transform: translateX(0) rotate(0deg);}
}
@keyframes range-y {
0% {transform: translateY(0);}
20% {transform: translateY(13px) rotate(0.02deg);}
35% {transform: translateY(-1px) rotate(0deg);}
70% {transform: translateY(-14px) rotate(0.02deg);}
90% {transform: translateY(2px) rotate(0deg);}
100% {transform: translateY(0) rotate(0.02deg);}
}
@-webkit-keyframes range-x {
0% {transform: translateX(0);}
30% {transform: translateX(-8px) rotate(0.02deg);}
50% {transform: translateX(1px) rotate(0deg);}
65% {transform: translateX(6px) rotate(0.02deg);}
80% {transform: translateX(0px) rotate(0deg);}
89% {transform: translateX(-3px) rotate(0.02deg);}
100% {transform: translateX(0) rotate(0deg);}
}
@-webkit-keyframes range-y {
0% {transform: translateY(0);}
20% {transform: translateY(13px) rotate(0.02deg);}
35% {transform: translateY(-1px) rotate(0deg);}
70% {transform: translateY(-14px) rotate(0.02deg);}
90% {transform: translateY(2px) rotate(0deg);}
100% {transform: translateY(0) rotate(0.02deg);}
}
FINAL WORD
You can still tweak the effects a little either way to fit your requirements.
It's not perfect but I hope it helps soften the end effect for your actual animation.
Best way to animate object/image randomly around within a div?
You may want to take a look at tutorials like this one:
https://css-tricks.com/bounce-element-around-viewport-in-css/
body {
margin: 0;
}
img, div {
width: 100px;
height: 100px;
}
.x {
animation: x 2.6s linear infinite alternate;
}
.y {
animation: y 0.8s linear infinite alternate;
}
@keyframes x {
100% {
transform: translateX( calc(100vw - 100px) );
}
}
@keyframes y {
100% {
transform: translateY( calc(100vh - 100px) );
}
}
<div class="x">
<img class="y" src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Stack_Overflow_icon.svg/768px-Stack_Overflow_icon.svg.png" alt="codepen" />
</div>
Related Topics
I Need an Overflow to Truncate from the Left, with Ellipses
Correct Terms and Words for Sections and Parts of Selectors
Inner Border Over Images with CSS
Why Doesn't Min() (Or Max()) Work with Unitless 0
Using Two CSS Classes on One Element
How Is Padding-Top as a Percentage Related to the Parent's Width
CSS Text Align Justify Big Spaces
How Do Browsers Deal with Non-Integer Values for Height and Width
How to Preserve Line Breaks in <Code> Block
What Are the Rules Around Whitespace in Attribute Selectors
Is @Page { Size:Landscape} Obsolete
Css: Clean Solution to the Margin Collapse Issue When Floating an Element
Ie Not Rendering CSS Properly When the Site Is Located at Networkdrive