I’ve always preferred grayscale images because I think they look more artistic. Many image editors like Photoshop can easily convert your color images to grayscale. There are even options to adjust color depth and hue. Unfortunately, this effect is not easy to achieve on the web due to differences in browsers. 1. CSS Filter Using the CSS filter property is probably the easiest way to convert an image to grayscale. Historically, Internet Explorer had a proprietary CSS property called filter to apply custom effects including grayscale. Now, the filter property is part of the CSS3 specification and is supported in some browsers, including Firefox, Chrome and Safari. Previously, we also mentioned Webkit filters, which not only turn images to gray but also to sepia and blur effects. Adding the following CSS style can turn the image into gray Copy code The code is as follows:img { -webkit-filter: grayscale(1); /* Webkit */ filter: gray; /* IE6-9 */ filter: grayscale(1); /* W3C */ } Supports IE6-9 and Webkit browsers (Chrome 18+, Safari 6.0+, and Opera 15+) (Note: This code has no effect on Firefox.) 2. Javascript The second method is to use JavaScript. Technically, all browsers that support JavaScript should support JavaScript, including IE6 and below. Copy code The code is as follows:var imgObj = document.getElementById('js-image'); function gray(imgObj) { var canvas = document.createElement('canvas'); var canvasContext = canvas.getContext('2d'); var imgW = imgObj.width; var imgH = imgObj.height; canvas.width = imgW; canvas.height = imgH; canvasContext.drawImage(imgObj, 0, 0); var imgPixels = canvasContext.getImageData(0, 0, imgW, imgH); for(var y = 0; y < imgPixels.height; y++){ for(var x = 0; x < imgPixels.width; x++){ var i = (y * 4) * imgPixels.width + x * 4; var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3; imgPixels.data[i] = avg; imgPixels.data[i + 1] = avg; imgPixels.data[i + 2] = avg; } } canvasContext.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); return canvas.toDataURL(); } imgObj.src = gray(imgObj); 3. SVG The third method is from SVG Filter. You need to create an SVG file, write the following code in it, and save it as ***.svg Copy code The code is as follows:<svg xmlns="http://www.w3.org/2000/svg"> <filter id="grayscale"> <feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0"/> </filter> </svg> Then using the filter properties, we can connect to the SVG file by the ID of the element in the SVG file Copy code The code is as follows:img { filter: url('img/gray.svg#grayscale'); } You can also put it in a CSS file, for example: Copy code The code is as follows:img { filter: url('url("data:image/svg+xml;utf8,<svg%20xmlns='http://www.w3.org/2000/svg'><filter%20id='grayscale'><feColorMatrix%20type='matrix'%20values='0.3333%200.3333%200.3333%200.3333%200%200%200.3333%200.3333%200.3333%200.3333%200%200%200%200%200%200%201%200'/></filter></svg>#grayscale");') } In summary, in order to support grayscale effects across browsers, we can use the above methods together with the following code snippet to achieve it. This code will support Firefox 3.5+, Opera 15+, Safari, Chrome, and IE Copy code The code is as follows:img { -webkit-filter: grayscale(100%); -webkit-filter: grayscale(1); filter: grayscale(100%); filter: url('../img/gray.svg#grayscale'); filter: gray; } We can leverage the above code with the JavaScript approach and just provide the CSS filter as a fallback in case JavaScript is disabled. This idea can be easily implemented with the help of Modernizr. Copy code The code is as follows:.no-js img { -webkit-filter: grayscale(100%); -webkit-filter: grayscale(1); filter: grayscale(100%); filter: url('../img/gray.svg#grayscale'); filter: gray; } OK, you can see the cool effect on your browser! ! |
<<: Simple example of HTML text formatting (detailed explanation)
>>: How to implement Hover drop-down menu with CSS
1. Computed properties and listeners 1.1 Computed...
Preview: Code: Page Sections: <template> &l...
The project interacts with the server, accesses t...
1. Upper and lower list tags: <dl>..</dl...
Get the current date + time (date + time) functio...
Encryption and decryption are an important means ...
This article shares the mysql5.6.24 automatic ins...
Recently, when I was working on a conference heal...
1.Mysql connection method To understand the MySQL...
1. Replace your .js library file address with the...
1. Add a user . First, use the adduser command to...
URL: http://hostname.com/contextPath/servletPath/...
Table of contents 1. Calculated properties 1.1 Ba...
Table of contents The node version does not corre...
Cause of failure Today, when I was writing a caro...