excanvas.js not working on IE with dynamic load script

1.5k views Asked by At

I am dynamically loading script to the document by using jQuery $.getScript() method like, $.getScript("excanvas.js", function(data){});

On create canvas I applied following statement for canvas,

var canvasDiv = document.createElement('canvas');
canvasDiv.setAttribute("width", canvasBounds.width+"px");
canvasDiv.setAttribute("height", canvasBounds.height+"px");
if(window.G_vmlCanvasManager){
    canvasDiv = G_vmlCanvasManager.initElement(canvasDiv);
}

and I try to draw a line on canvas, it is not working.

If I load the excanvas.js in script tag its working fine. that is like

<head>
<script src="excanvas.js" type="text/javascript"></script>
</head>

I cant find the reason. Can any one suggest a solution?

2

There are 2 answers

1
Gyandeep On

i have used it and it works for me:

if ($.browser.msie) {
      G_vmlCanvasManager.initElement(canvasDiv);
}

After doing the top step, get the context of the canvas:

context = canvasDiv.getContext('2d');

Now draw the line using the context variable.

0
Tyler On

If you're looking to load excanvas asynchronously/conditionally, it is possible.

First, as the instructions state, the canvas element(s) can't exist on the page until AFTER excanvas has loaded & initialized, so consider generating them or loading them asynchronously afterwards.

Once the excanvas library has loaded (via jQuery, requirejs, whatever), call this:

G_vmlCanvasManager.init_(document);

This calls the manager's internal init function that normally listens for document ready (which has usually already fired).