So you can get pixelation in CSS by doing the following:
- Set
background-image
to a very small image (say 50px or 100px). - Set
image-rendering: pixelated
on the element.
That will give you the pixelated look.
Now I would like to animate this, by replacing the "very small image" with a large image after it finishes downloading by the browser:
let img = new Image()
img.src = largeVersion
img.onload = function(){
// set css background-image to the new image perhaps, not sure...
}
The problem is two-fold.
- I want to have the
background-image
usingbackground-size: cover
so it properly fills the container element. So you can't use background-size in any pixelation animation. transform: scale(0.1)
(to get close to the original pixelation size) doesn't work because it scales the whole element.
I would like to do something like this: animate transform: scale(x)
to go from 50px pixelated image to 2000px unpixelated image, over 0.3 or 0.5 seconds. But that doesn't work. I thought maybe using background-size, but that doesn't work either because of the constraint.
Wondering if there is any way to accomplish this.
I have seen this which does pixelation using canvas. Wondering if there is no other solution that works without using JS/canvas.
<style>
div {
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
</style>
<div style='background-image: url(/100px.jpg)'></div>