Problem getting excanvas to use getContext in IE8

Phillip picture Phillip · Jun 15, 2011 · Viewed 13.8k times · Source

I am trying to implement excanvas in order to ensure canvas tags work in IE8 as well as all the other browsers we use here. I am having an issue getting getContext to work in IE8. I have read about the need to us the G_vmlCanvasManager.initElement routine when dynamically creating canvas objects in the DOM, however I cannot even get statically created objects to work in IE8. I can tell the canvas is create properly because the border appears. All other browsers have no issues. Here is the code:

<div align="right">
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>

<script type="text/javascript">

var c=document.getElementById("myCanvas");
if (typeof window.G_vmlCanvasManager!="undefined") { 
    c=window.G_vmlCanvasManager.initElement(c);
    var cxt=c.getContext("2d");
   }else 
    var cxt=c.getContext("2d");

cxt.fillStyle="#FF0000";
cxt.fillRect(0,0,150,75);

</script>
</div>

Thanks for any help you can give.

Answer

FoREacH picture FoREacH · Aug 16, 2011

window.onload or onLoad="drawSomething()" should work, same thing

    <head>
    <!--[if lt IE 9]>
        <script type="text/javascript" src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <script type="text/javascript" src="/assets/site/excanvas.min.js"></script>
    <![endif]-->
    </head>

    <figure id="logo" class="body">
        <canvas id="logo-canvas" width="490" height="135"></canvas>
        <script src="canvas.js" type="text/javascript"></script>
        <script type="text/javascript">
        window.onload = function() {
            var c=document.getElementById("logo-canvas");
            var cxt=c.getContext("2d");
            drawLogo( cxt );
        }
        </script>
    </figure><!-- /#logo -->';