excanvas G_vmlCanvasManager is undefined

1.3k views Asked by At

I am trying to grab an google chart generated image from my page and draw it on a canvas, so I can use canvg lib function and transform it on a jpeg image, then it can be attached to an pdf file. My problem is that some versions of IE can't handle canvas attribute. To overcome this I'm trying to use excanvas lib. In the code below I capture the image from the container.

  function getImageFromResource(containerId){

             var browserTypeAndVersion = getBrowserTypeAndVersion();

             var container = document.getElementById(containerId);

             var chartArea;

             var isIE = false;
             if(browserTypeAndVersion[0] == "MSIE" && 8 >= parseInt(browserTypeAndVersion[1])){
                isIE = true;                     
             }
             if(isIE){
                 chartArea = container.getElementsByTagName('iframe')[0].parentNode;
             }else{
                 chartArea = container.getElementsByTagName('svg')[0].parentNode;
             }
             var svg = chartArea.innerHTML;

             var canvas = document.createElement('canvas');
             canvas.setAttribute('width', chartArea.offsetWidth);
             canvas.setAttribute('height', chartArea.offsetHeight);
             canvas.setAttribute(
               'style',
               'position: absolute; ' +
               'top: ' + (-chartArea.offsetHeight * 2) + 'px;' +
               'left: ' + (-chartArea.offsetWidth * 2) + 'px;');

             if(isIE){
                 window.G_vmlCanvasManager.initElement(canvas);
             }

             document.body.appendChild(canvas);

             canvg(canvas, svg);
             var imgData = canvas.toDataURL("image/jpeg");

             return imgData;
        } 

The issue occur when I try to initiate the canvas using initElement from G_vmlCanvasManager it remains undefined meaning it have not being loaded or something like that. So i need to know how to load it, and for extra information, my js compressed file is declared on the head of the base page. Thanks for your help and attention guys.

0

There are 0 answers