Is there a way to create animated image pixelation without relying on canvas, simply using CSS?

Pixelation can be achieved in CSS by following these steps:

  • Set background-image to a very small image (50px or 100px).
  • Apply image-rendering: pixelated on the element.

This will give you the desired pixelated effect.

Now, I am interested in animating this process by replacing the small image with a larger one once it has finished downloading in the browser:

let img = new Image()
img.src = largeVersion
img.onload = function(){
  // set css background-image to the new image perhaps, not sure...      
}

However, there are two issues at hand:

  1. I want to use background-size: cover for proper container filling which conflicts with any pixelation animation.
  2. transform: scale(0.1) doesn't yield the desired results as it scales the entire element.

My goal is to animate transform: scale(x) from a 50px pixelated image to a 2000px unpixelated image over a specific duration. Yet, my attempts have been unsuccessful so far. Perhaps using background-size might work, but the constraints prevent it.

I’m curious if there is an alternative solution without resorting to JavaScript/canvas like demonstrated in this example.

<style>
  div {
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
  }
</style>
<div style='background-image: url(/100px.jpg)'></div>

Answer №1

If you're looking to achieve pixelation with an svg filter and add some animation to it, here's how you can do it!

To start off, you can apply the filter on a div background by using the following CSS rule: filter: <url>(#filterid)</url>.

When pieced together, it will look something like this:

#myDiv::before{
content:"";
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
filter:url(#pixelate);
background-size:cover;
background-image:url(https://images.unsplash.com/photo-1475724017904-b712052c192a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2850&q=80)

}

#myDiv {
  position:relative;
  width:350px;
  height:250px;
}

.inside {
  position: relative;
}
<div id="myDiv"> <div class="inside"><h1>Hello</h1></div> </div>
<svg>
  <filter id="pixelate" x="0" y="0">
    <feFlood x="4" y="4" height="1" width="1" />
    <feComposite id="composite1" width="10" height="10" />
    <feTile result="a" />
    <feComposite in="SourceGraphic" in2="a" operator="in" />
    <feMorphology id="morphology" operator="dilate" radius="5" />
  </filter>

  <animate xlink:href="#composite1" id="anim-width" 
    attributeName="width" from="40" to="10" dur=".8s"
    fill="freeze" />  
  <animate xlink:href="#composite1" id="anim-height" 
    attributeName="height" from="40" to="10" dur=".8s"
    fill="freeze" />
  <animate xlink:href="#morphology" id="anim-radius" 
    attributeName="radius" from="20" to="5" dur=".8s"
    fill="freeze"/>
</svg>

You may notice that I had to create an inner div and use a pseudo-element ::before for the background, but hopefully this extra step won't be needed once support for backdrop-filter improves.

For more information, check out these references:

Pixelate svg effect: https://codesandbox.io/s/km3opvn6yv

Animate an svg filter: https://codepen.io/chriscoyier/pen/dPRVqL

Backdrop-filter documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to make a picture fill the entire background?

Is there a way to make pictures expand across an entire page with different resolutions? I'm trying to achieve this effect but unsure how. Take a look at my current example: . ...

Incorporate clickable selection buttons into an HTML layout without disrupting the placement of existing

I have set up an HTML document that features a collection of buttons enclosed within a DIV id labeled 'buttons'. My objective is to introduce a sort by button into the mix without disrupting the current layout. I attempted to achieve this goal by ...

Is there a way for me to view the specifics by hovering over a particular box?

Whenever I hover over a specific box, detailed information appears, and when I move the mouse away, the details disappear. HTML <nav> <div> <div class="nav-container"> <a href="./about.html" ...

How can I achieve a fade-in effect whenever the flag image is clicked?

A unique "international" quotes script has been created, showcasing Dutch, English, German, and French quotes. The script displays different quotes every day, with a draft-result visible in the upper right corner of this page ... The code used for this sc ...

Final div class nested within the HTML and styled with CSS

Is there a way to keep the last div (class) from sliding underneath? I applied margin-right to .artist_wrap. I assumed that using overflow: hidden would contain it within #music_videos_wrap, but it just disappears. Any assistance is greatly appreciated. H ...

Update the FontSize in the HTML dropdown menu using JavaScript

I am having trouble filling my Selection using a script. For example, when I try to populate my FontSizeMenu, I use the following code snippet: function FillFontSizeMenu() { FillSelection(GetPossibleFontSizes(), "fontSizeMenu"); } function GetPossib ...

I wonder about the origin of the text appearing on my website's search results page

Why are the meta tags description and title not showing up correctly on the site? They appear, but not with the content I specified. This website is built using React. Below is the meta tag code from the index.html file: <meta charset="utf-8" ...

What is the best way to display HTML content in a Jupyter Notebook?

While exploring my file system on a jupyter notebook server, I noticed that I can access and modify HTML and text files along with ipynb files. However, I am interested in viewing the files as rendered HTML rather than editable HTML. In simpler terms, is ...

Video background function that mimics CSS's background size cover for a specific element, instead of the entire webpage

In the process of developing a new website, I am looking to incorporate a large splash background as a "hero" image. Similar to the website found at , I am hoping to use a video as the background image showing a woman unloading a car. One key requirement ...

unique map customized in a separate browser window

Attempting to complete the code provided here but encountering issues as a novice in java-scripting. It seems like the map is not loading correctly. A new tab and text are generated when DIV is created. Seeking assistance on how to open a custom map in a ...

Is it possible to apply a template file to all of my HTML pages for consistency?

I'm currently in the process of developing a website and looking for a template that I can use across all pages - including a news page, home page, about page, projects page, etc. I am aware that Dreamweaver offers this feature, but is there a way to ...

The bottom section includes all the information

Hey there, I'm in the process of creating my personal website, but I'm having trouble with the footer covering two buttons. It's a strange issue that only seems to occur on my laptop; when I switch to my external monitor, the buttons are dis ...

Can WebGL be configured to render offscreen without its canvas being directly connected to the DOM?

Searching for an answer to what I believed would be a simple question has proven to be quite challenging. My goal is to utilize WebGL for image processing and generation tasks, with the intention of working offscreen. Specifically, I aim for WebGL to rende ...

Is there a way to customize the cursor of the webkit resizer (pulltab)?

I'm having difficulty implementing a custom cursor on my webkit pulltab. While I am able to change other properties like background color, the CSS "cursor" property remains unchangeable: #sessionbuilder::-webkit-resizer { background: #000; cursor ...

Ways to modify the background hue of a Bootstrap popover

I am trying to create 3 popovers with different background colors using Bootstrap and CSS. Ideally, I would like the background color to change based on the specific letter placed inside the span's class attribute. <div class="container"& ...

Attempting to implement image switching with hover effects and clickable regions

Hey there, I'm currently working on a fun little project and could use some guidance on how to achieve a specific effect. The website in question is [redacted], and you can view the code I've used so far at [redacted]. You'll find a code blo ...

What is the method for customizing the color of a placeholder text in a specific text field

Is there a way to style placeholder text in different colors for each text field? According to the Mozilla documentation, the code snippet provided will change the color of the entire input tag. If you'd like to learn more, follow this link: https:/ ...

Is the dropdown menu causing issues with the rest of the navigation bar?

While attempting to create my website, I encountered some challenges with the dropdown menu. After following a tutorial on YouTube and making some modifications myself, there seems to be an issue. The screenshot below illustrates that it pulls down the re ...

css The issue of ul and ol elements not inheriting the padding property

https://codepen.io/unique-user123/pen/abcDeFG Why is it necessary to utilize the ul, ol, and li selectors in order to remove the default 40px left padding and 16px top and bottom margin? Can't we simply use the body selector to achieve this? <hea ...

Uploading files in chunks using a combination of HTML, JavaScript,

I've been using a file chunking solution (although I can't recall its origin), but I've made some modifications to suit my requirements. Most of the time, the file uploads successfully; however, there are instances where an error occurs. U ...