Transforming HTML into image format with HTML2CANVAS

I have integrated HTML2CANVAS to save a canvas enclosed in a div as a .png image. The functionality works perfectly in Chrome, but I encounter an issue in IE 9, 10, and 11 where nothing happens. Despite setting breakpoints for debugging in IE, no errors are thrown.

The motivation behind wrapping the canvas in a div was to facilitate exporting the legend along with the chart/graph generated within the canvas tag.

Although AngularJS is in use, only a click event is utilized in this scenario.

While everything operates smoothly in Chrome without any errors, IE fails to respond appropriately. Even after investigating, some sources suggest that IE struggles with promises and recommend adding a polyfill to address the problem. However, implementing the polyfill has not resolved the issue.

Thank you

Below is the code snippet:

HTML

<div class="row">
    <div id="widget" class="col-md-12">
    <canvas id="lineChartCanvas" class="chart chart-line" chart-data="data" chart-labels="labels" style="height: 300px; width: 95%;"
            chart-legend="true" chart-series="series" chart-click="onClick"></canvas>
    </div>
</div>

Button click event

<input type="button" class="btn btn-primary" id="btnSave" ng-click="exportChart()" ng-show="isIEbrowser" value="EXPORT CHART"/>

Javascript/Angular

function exportChart() {

    var canvasdiv = document.getElementById("widget");

    html2canvas(canvasdiv,{

        onrendered: function (canvas) {
            var a = document.createElement("a");
            a.href = canvas.toDataURL("image/png");
            a.download = userContext.mrn + "_chart_" + datetime + ".png";
            a.click();
        }
    });
}

Answer №1

To ensure compatibility with Internet Explorer, utilize the BlobBuilder to convert canvas data into a blob object and save it as an image stream.

The following is an example code snippet that was implemented in a previous project:

if (navigator.msSaveBlob) {
                    var URL = window.URL;
                    var BlobBuilder = window.MSBlobBuilder;
                    navigator.saveBlob = navigator.msSaveBlob;
                    var imgBlob = canvas.msToBlob();
                    if (BlobBuilder && navigator.saveBlob) {
                        var showSave = function (data, name, mimetype) {
                            var builder = new BlobBuilder();
                            builder.append(data);
                            var blob = builder.getBlob(mimetype || "application/octet-stream");
                            navigator.saveBlob(blob, name);
                        };

                        showSave(imgBlob, fileName, "image/png");
                    }
                }

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

What's the best way to display the spinner while making an axios request?

When I put the spinner on a component did mount method with a setTimeOut method inside, it shows. However, I want the spinner to display only when there is no content on the page. After the axios request finishes loading, the page content should be shown i ...

jscript failing to load due to issue with jquery

Hello, I need help understanding why the code provided below is not functioning properly. You can view the code that does work here: http://jsfiddle.net/CDp8t/7/ <html> <head> <title></title> <script type=" ...

Is there a hierarchy to be followed within the <head> element?

I have been developing websites using HTML for nearly 3 years now, but since I had to teach myself, there are still a few things I am unsure about. One question that has recently become important is whether it is possible to create groups within the <h ...

Transitioning images smoothly and responsively with jQuery, creating a beautiful

Hey there! I'm looking for some help with transforming this jQuery effect. Instead of having fixed sized images, I want to set the size in percentage (width:100%; height:auto) so they can be responsive. Any creative ideas or suggestions? <scri ...

What could be causing the unexpected text display in my shiny app despite using html tags and bootstrap?

Here is a simple example that illustrates the error I am experiencing: library(shiny) run_with_enter <- ' $(function() { var $els = $("[data-proxy-click]"); $.each( $els, function(idx, el) { var $el = $(el); var $proxy = $("#" + $el.data("proxyCl ...

Changing the input to uppercase within Vue.js

I am looking to capitalize the input data from the user's full name model and would prefer if it is in Latin letters only. Utilizing Vue.js for Uppercase Conversion <p-input :value="value" @input="$emit('input', $event)&qu ...

Refresh the Angular ion-view from a different controller

Greetings to all fellow developers on stackoverflow! I am a newbie here and just starting out with AngularJS. My current project involves building an Ionic app, but I am encountering an issue that I need assistance with. The goal is to update the date di ...

Having difficulties executing AJAX requests on Codepen.io

My CodePen project that I created 6 months ago is no longer functioning properly. The project includes an Ajax call to retrieve data using jQuery.ajax(). When attempting to load the content from CodePen via https, even though the ajax request is through h ...

Express application failed to deploy due to boot timeout error R10

After researching extensively on Stack Overflow and various other websites, I have been unable to solve a problem with my small Express app using ES6 syntax. Upon deploying it to Heroku, I encountered the following error: LOGS : 2021-04-09T12:14:14.688546 ...

The process of how React attaches event listeners to elements created through the map method

Upon clicking discountKeyboard, an error was encountered: Alert: Functions cannot be used as a React child. This issue may arise if you return a Component instead of from render. Alternatively, it could be due to calling the function rather than returnin ...

Is there a comparable Javascript alternative to the Ruby gem "site_prism" for Page Objects design?

Is there a JavaScript framework for Protractor in NodeJS that provides a clean way to define Page Object Elements similar to site_prism? I've explored astrolabe but it doesn't quite meet the requirements. This is an example of how Page Objects a ...

React Js: Promise rejection due to SyntaxError:

I've been attempting to make a POST request using the fetch method in reactjs. Despite going through some documentation, I haven't been able to resolve my error. Can someone please lend a hand? Below is the snippet of my reactjs code: handleSub ...

Exploring the depths of JSON arrays: Understanding multidimensional structures

I have the following array: { "sEcho": 1, "iTotalRecords": 54, "iTotalDisplayRecords": 54, "aaData": [ [ "79", "testowy2", "testowy samochod", "12.00", "14.00", ...

Stop jQuery ajax from running any JavaScript code contained in a script or HTML response

As stated in the documentation: If html is specified, any embedded JavaScript within the retrieved data will be executed before returning the HTML as a string. Similarly, using script will execute the pulled back JavaScript before returning nothing. Is ...

Having difficulty accessing the sound file despite inputting the correct path. Attempts to open it using ./ , ../ , and ../../ were unsuccessful

While attempting to create a blackjack game, I encountered an issue. When I click the hit button, a king card picture should appear along with a sound. However, the sound does not play and the error message Failed to load resource: net::ERR_FILE_NOT_FOUND ...

Windows npm configuration settings

After receiving helpful answers to my previous question about using a named parameter in an npm run script, I encountered a new problem. It seems that the $npm_config_variable doesn't function correctly on Windows OS. I am in search of a solution that ...

Form submission is not functioning as expected

I am having trouble with my form submission. When I try to submit the form, nothing happens. I have tried various solutions but have not been able to resolve the issue. I have reviewed all available resources but still cannot figure out why my code is not ...

TemplateUrl cannot be located within the AngularJS MVC framework

Hey there, I've been diving into tutorials on Angular without the Asp.net Core part. Currently, I'm stuck at a point where I need to include partial HTML in my application using this method: Main module (function(){ "use strict"; angula ...

Troubleshooting server-side sorting issues with AJAX implementation, encountering problems with headers functionality

I'm encountering a problem: Some headers are not functioning properly to sort the table. For example, Brand and Model work as expected, but VehicleType only works once, and CarID and ReleaseDate never seem to work. Here is my index.php file: index. ...

How can I create a custom validator in Angular 2 that trims the input fields?

As a newcomer to Angular, I am looking to create a custom validator that can trim the input field of a model-driven approach form. However, I have encountered difficulties during implementation. When attempting to set the value using setValue() within th ...