2013-12-19 32 views
3

我有一個使用在Chrome中正常工作的畫布的動畫。我正在使用excanvas polyfill來支持IE 8。根據excanvas instructions,我在頭部使用IE條件標籤附加excanvas。IE 8使用excanvas polyfill的畫布:沒有錯誤,但動畫不在IE上運行8

Working page here | Full code on Github

簡化HTML:

<head> 
    <meta charset="utf-8"> 
    <!--[if gte IE 9]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]--> 
    <!--[if lt IE 9]><meta http-equiv="X-UA-Compatible" content="IE=7" /><![endif]--> 

    <link rel="stylesheet" href="css/normalize.min.css"> 
    <link rel="stylesheet" href="css/ecard.css"> 

    <script src="js/vendor/modernizr-2.6.2.min.js"></script> 
    <!--[if lt IE 9]><script src="js/vendor/excanvas.js"></script><![endif]--> 
</head> 
<body> 
    <canvas id="card" width="1000" height="750"></canvas> 

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
    <script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.1.min.js"><\/script>')</script> 

    <script src="js/ecard.js"></script> 
</body> 

我使用的是三元運算符定義所推薦的答案

var canvas = (typeof(G_vmlCanvasManager) != 'undefined') ? G_vmlCanvasManager.initElement($("canvas#card")[0]) : $("canvas#card")[0]; 
    ctx = canvas.getContext('2d'); 

當我試圖從IE 8運行動畫CTX,它什麼都不做。我在IE 8 Developer Tools中看不到任何錯誤。但是,當我在IE 8 Developer Tools控制檯中鍵入ctx.fillRect(25, 25, 100, 100);並運行它時,出現錯誤「'ctx'未定義」。

從IE 8開發人員工具看來,excanvas.js似乎已正確加載。有任何想法嗎?

回答

3

嘗試通過添加此向頭:

<meta http-equiv="X-UA-Compatible" content="IE=7" /> 

在標準模式下通過VML使用excanvas被關閉。

而不是這個(如果仍然有問題):

ctx = $("canvas#card")[0].getContext('2d'); 

嘗試:

var canvas = (typeof(G_vmlCanvasManager) != 'undefined') ? G_vmlCanvasManager.initElement($("canvas#card")[0]) : $("canvas#card")[0]; 

ctx = canvas.getContext('2d'); 
+0

這https://github.com/mrengy/xmas-ecard/commit/f38c8a634b3dda86bfebc6596c98783dd605be82了擺脫錯誤「對象不支持此屬性或方法」,但是當我在IE 8開發工具中輸入'ctx.fillRect(25,25,100,100);'到控制檯並點擊「運行腳本」時,我得到一個錯誤「'ctx'未定義」 –

+0

@MikeEng最後一個是因爲ctx被定義在匿名趣味的範圍內文檔加載時調用。如果將'var ctx;'移動到全局範圍,您應該可以從控制檯使用它。希望這可以幫助。 – K3N

+0

謝謝,但實際上ctx已經在全球範圍內聲明瞭。我沒有在問題中包含這部分代碼,但請參閱github.com/mrengy/xmas-ecard/blob/master/js/ecard.js –