Tips for obtaining images using CSS overlay effects

Within my album, I have curated a collection of images that I crafted into an image gallery using HTML and CSS. One detail I added was a black and white effect to the pictures via CSS. However, when attempting to download these black and white photos, the downloaded file always reverts back to the original color version without the CSS effect applied. Can anyone advise on how to successfully download images with CSS effects intact?

<style>
img {
  -webkit-filter: grayscale(100%); /* convert image to black and white */
  filter: grayscale(100%);
}
</style>

<p>Convert the image to black and white:</p>

<img src="pineapple.jpg" alt="Pineapple" width="300" height="300">

Answer â„–1

This amazing tool is what I use: DOM to Image library

If you prefer NPM:

npm install dom-to-image

then import,

import domtoimage from 'dom-to-image';

Here's a snippet of HTML with markup that should be saved as an image:

<div id="my-node">
    <img src="/assets/images/image.jpg"/>
</div>

<div class="btn-group">
    <button class="btn-primary" (click)="convertToPng()">Convert to PNG</button>
    <button class="btn-primary" (click)="convertToJpeg()">Convert to JPEG</button>
    <button class="btn-primary" (click)="downloadAsPng()">Download as PNG</button>
    <button class="btn-primary" (click)="downloadAsJpeg()">Download as JPEG</button>
</div>

CSS filters can also be applied, here's an example:

img {
    filter: grayscale(100%);
  }

To Convert to PNG:

convertToPng(){
    let node = document.getElementById('my-node');
    domtoimage.toPng(node)
    .then(function (dataUrl) {
        var img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
    })
    .catch(function (error) {
        console.error('oops, something went wrong!', error);
    });
  }

To Convert to JPEG:

convertToJpeg(){
    let node = document.getElementById('my-node');
    domtoimage.toJpeg(node)
    .then(function (dataUrl) {
        var img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
    })
    .catch(function (error) {
        console.error('oops, something went wrong!', error);
    });
  }

To Download as PNG:

downloadAsPng(){
    let node = document.getElementById('my-node');
    domtoimage.toPng(node)
    .then(function (dataUrl) {
        var link = document.createElement('a');
        link.download = 'my-image-name.png';
        link.href = dataUrl;
        link.click();
    });
  }

To Download as JPEG:

 downloadAsJpeg(){
    let node = document.getElementById('my-node');
    domtoimage.toJpeg(node, { quality: 0.95 })
    .then(function (dataUrl) {
        var link = document.createElement('a');
        link.download = 'my-image-name.jpeg';
        link.href = dataUrl;
        link.click();
    });
  }

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

Obtaining the ID of a moved item in Uikit's nestable component

I am currently utilizing the uikit framework and have a query regarding the nestable component. Despite seeking assistance in various places, none of the solutions seem to match my specific needs. My limited knowledge of JavaScript may be a contributing ...

Change the alert notification to a pop-up window

Is there a way to show a pop-up message instead of an alert when a user clicks on a specific product set? Here is the current code I have: <script> $(document).ready(function(){ $('.pre-sale-product').on('click&apo ...

What is the best way to allow CORS in an internet server by utilizing AJAX and PHP in order to obtain a font to be used on a different BigCart

Currently, I am using BigCartel for website design. I am looking to incorporate a custom font into my design. However, regardless of the server I use (currently a free Hostinger server), I am unable to enable CORS using htaccess. Recently added to .htacc ...

`Implementing a dynamic list with image and button using Bootstrap 5`

I've been exploring ways to create a container with 4 images aligned next to each other, accompanied by a button beneath each image. My goal is for the layout to be responsive, meaning that as the screen size adjusts, the elements inside should also a ...

Bidirectional updates in AngularJS with CSS styling

On the backend, certain HTML elements store their position and size persistently and retrieve them when the page loads. These elements can be dragged and resized by users, with any updates needing to be saved on the backend for consistency across sessions. ...

Deleting a division with the help of media queries

As a challenge, I am attempting to eliminate the .navbar-brand>img element once the screen size exceeds 768px by using media queries. Unfortunately, my efforts have not been successful so far. Although the styling applied to the img is successfully remo ...

Utilizing the GROUP BY clause within an INNER JOIN operation, and presenting the results in an HTML dropdown menu

My database includes the following tables: Workers (id, total_pay, date_of_pay, projects_id) Payments (id, payment, date, projects_id) building_materials(id, pay_of_materials, date, projects_id) additional(id, add_pay, date, projects_id) All tables have p ...

The Lightslider’s responsive height is being clipped

I attempted to utilize the Lightslider plugin in order to craft a responsive slider for showcasing my portfolio images. However, I encountered an issue where the height of each thumbnail in the portfolio appears to be cropped from both the top and bottom ...

Presenting MongoDB data in a Jinja2 HTML table

Currently, I am trying to find a way to display multiple documents one by one from my MongoDB collection. The code I have written to fetch the data is as follows: @app.route('/shops', methods=['GET', 'POST']) @login_required d ...

Navigating size measurements in percentages and pixels to achieve flawless alignment

Utilizing the calc property in a solution, I am facing challenges when trying to calculate the percentage width of an element with margins, padding, and borders set in pixels. Take a look at this demo: #form { margin: 200px auto; width: 360px; ...

Conceal a Column in Exporting an HTML Table using JavaScript

I am currently working with a table that has 12 columns, and I have a button at the end of the table which triggers a function when clicked to export the table into an .xls Excel file. However, I need to hide the 12th column in the exported file. Can anyon ...

What is the best approach to transform an HTML textarea value into JSON format?

Client .. inserting some content into a form <textarea name="data"></textarea> After typing the following data into the textarea: {title: 'Hello one!', author: 'someone'} {title: 'Hello two!', author: 'mygf ...

Struggling with aligning buttons in a horizontal navigation bar

I have been struggling to center the buttons on my navigation bar for quite some time now, despite my efforts searching through Google (I'm still a beginner in CSS). Below is the code I have for the navbar (excluding my numerous unsuccessful attempts ...

Navigate to a designated tab by selecting a designated button in HTML

In my modal, there are two tabs called Login and Registration, along with buttons named Login_Button and Registration_Button. When I click on the Login_Button, I want to display the Login tab. Similarly, when I click on the Registration_Button, I want to ...

What is the process for incorporating a full-page blog into a blog preview?

I customized a template, but there is a blog section within the div that I am struggling to replicate. Here is the test website for reference: Below is the HTML code for the blog section: <!-- Blog Start --> <section class="section bg ...

What is the alternative name for DOM elements in TypeScript?

When working with a HTMLImageElement, I can simply reference it like this: let image: HTMLImageElement; ... In Dart, importing the dom is possible using: import 'dart:html' as dom; Using 'dom' as an alias for the "dart:html" package. ...

Adding Font Awesome icons to Bootstrap forms in WordPress (using CF7): A step-by-step guide

I am currently working on creating a custom WordPress theme using Bootstrap4. Everything is going smoothly, except for my contact form which appears like this: https://i.sstatic.net/iEysI.png Below is the code I have implemented: <for ...

Create geometric shapes on Google Maps version 2

Can anyone guide me on how to draw a polygon on Google Maps with user-input coordinates? I have two input fields for latitude and longitude, and when the user clicks the button, I want the polygon to be drawn. The code I currently have doesn't seem to ...

A continuous loop of text is displayed in a slideshow format, with different image files used for the navigation buttons "back", "stop", and "forward"

I am in need of a unique slide show that displays text exclusively attached to CSS, with image files for the navigation buttons. I have been searching for days without success, as most options available are for image slideshows and not suitable for my text ...

What is the best way to eliminate the pause in my slideshow?

After spending a few hours working on this project, I've almost completed it. However, I'm facing an issue with the slideshow functionality. It seems to pause after cycling through all the images. Any suggestions on why this might be happening? ...