How to use SVG to achieve image mosaic effect without using Javascript? The following article will give you a detailed understanding, I hope it will be helpful to you!
I forwarded an article by my friend Vajoy
on the public account before - Skillfully use CSS to stylize the image mosaic.
The core is to use an interesting property in CSS--image-rendering, which can be used to set the image scaling algorithm. [Recommended learning: css video tutorial]
What is image-rendering?
CSS property image-rendering is used to set the image scaling algorithm. It applies to the element itself, to images in other attributes of the element, and to child elements.
The syntax is relatively simple:
{ image-rendering: auto; // 默认值,使用双线性(bilinear)算法进行重新采样(高质量) image-rendering: smooth; // 使用能最大化图像客观观感的算法来缩放图像。让照片更“平滑” image-rendering: crisp-edges; // 使用可有效保留对比度和图像中的边缘的算法来对图像进行缩放 image-rendering: pixelated; // 放大图像时, 使用最近邻居算法,因此,图像看着像是由大块像素组成的 }
Among them, image-rendering: pixelated
is more interesting and can mosaic a low-precision image.
For example, suppose we have an image of 300px x 300px
, and we let it be converted into 30px x 30px
:
Let’s enlarge the distorted image to 300px x 300px
:
image-rendering: pixelated, you can get a mosaic image:
<img src="/static/imghwm/default1.png" data-src="pic.jpeg?30x30" class="lazy" alt="Let's talk about how to use SVG to achieve image mosaic effect" >
img { width: 300px; height: 300px; image-rendering: pixelated }
##image-rendering: pixelated<strong></strong> Limitations of achieving mosaic effect
OK, then why do you need to reduce and then enlarge first, and then use
image-rendering: pixelated? Let's make a comparison and set image-rendering: pixelated
directly to the original image:
Set
will only make the image more jagged, but will not directly produce a mosaic effect. This is also consistent with the description of
. When enlarging the image, the nearest neighbor algorithm is used, so the image looks like it is composed of large blocks of pixels
. Only based on the enlarged and blurred image can we use
to get a mosaic picture!
So, suppose we only have a clear original image and want to use CSS to get a mosaic effect. Is it feasible? Following this idea, we can think:
Can we use CSS to reduce the image and then enlarge it, and then use
image-rendering: pixelated?
. So, if you want to get a blurry image when there is only one original image, you have to turn to Canvas, which is a little troublesome. We just want a mosaic effect.
SVG filter overlay to achieve mosaic effectThis can lead to today’s protagonist,
SVG filter, using several layers of SVG filters By superposing, you can actually achieve a mosaic effect filter very easily. And, SVG filters can be easily introduced through CSS filters.
The code is actually very simple. SVG defines a filter and uses the superposition effect of multiple layers of filters to achieve a mosaic effect. Then, it is introduced through CSS filter and can be applied to any element:
<img src="/static/imghwm/default1.png" data-src="任意无需缩放的原图.png" class="lazy" alt="Let's talk about how to use SVG to achieve image mosaic effect" > <svg> <filter> <feflood></feflood> <fecomposite></fecomposite> <fetile></fetile> <fecomposite></fecomposite> <femorphology></femorphology> </filter> </svg>
img { width: 300px; height: 300px; filter: url(#pixelate); }
In this way, we get a mosaic effect:
If you just want to use this effect, you don’t even need to understand the entire SVG
<ul>
<li><a href="https://github.com/chokcoco/cnblogsArticle/issues/27" target="_blank">interesting! Powerful SVG filters</a></li>
<li><a href="https://github.com/chokcoco/iCSS/issues/106" target="_blank">Interesting! The generation scheme of irregular borders</a></li>
<li><a href="https://github.com/chokcoco/iCSS/issues/107" target="_blank">Shocking! Can you use SVG filters to create emoticons? </a></li>
</ul>
<h2 id="strong-Limitations-of-CSS-SVG-to-implement-mosaic-strong"><strong>Limitations of CSS/SVG to implement mosaic</strong></h2>
<p>Of course, the limitation of CSS/SVG filter to implement mosaic is that if you don’t want to If the user can see the original image, then using this method directly on the client is equivalent to directly exposing the original image. </p>
<p>Because the CSS/SVG filter method is to mosaic images on the front end, and the original image is required. </p>
<p>Of course, using the two above-mentioned image mosaic techniques, we can still use it to create some simple interactive effects, like this: </p>
<p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/deade6439637a00749d538cc6e2a383e-5.gif?x-oss-process=image/resize,p_40" class="lazy" alt="" loading="lazy"></p>
<p> You can find all the codes for the above DEMO and SVG filters in these two DEMOs: </p>
<ul>
<li><a href="https://codepen.io/Chokcoco/pen/gOXEJJE" target="_blank">CodePen Demo - image-rendering pixelated application</a></li>
<li><a href="https://codepen.io/Chokcoco/pen/JjOVjop" target="_blank" textvalue="SVG Pixel Filter">SVG Pixel Filter</a></li>
</ul>
<blockquote>
<p>Original address: https://www.cnblogs.com/coco1s/p/16134088.html<br></p>
<p>Author: ChokCoco</p>
</blockquote>
<p> (Learning video sharing: <a href="https://www.php.cn/course/list/1.html" target="_blank" textvalue="web前端">web front-end</a>)</p>
The above is the detailed content of Let's talk about how to use SVG to achieve image mosaic effect. For more information, please follow other related articles on the PHP Chinese website!

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor