获得IE8与EaselJS和ExplorerCanvas的兼容性

时间:2012-10-04 09:37:58

标签: javascript internet-explorer canvas internet-explorer-8 easeljs

我正在使用EaselJS并希望允许向后兼容ExplorerCanvas。

这应该可以使用以下代码(参见here):

createjs.createCanvas = function () { ... return canvas implementation here ... }

但是,如果我在此函数中发出警报并运行代码,则该函数永远不会运行。

如何让这个工作?

编辑:

以下是我正在使用的代码的简化示例:

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='utf-8' />
    <script src='/Scripts/jquery-1.7.1.js'></script>
    <script src="/Scripts/excanvas/excanvas.compiled.js"></script>
    <script src="/Scripts/easeljs/lib/easeljs-0.5.0.min.js"></script>
    <script src='/Scripts/core/jquery.mousewheel.js'></script>
    <style>
        canvas
        {
            border: 1px solid #ccc;
        }
    </style>
    <script type='text/javascript'>
        $(document).ready(function () {
            // Variables
            var img;
            var stage;
            var bmp;

            // Bindings
            $('#load').click(function () { initialize() }); // DELETE

            // Functions

            function initialize() {
                img = new Image();
                img.onload = imageLoadedEvent;
                img.src = '/Scripts/viewer/June.jpg';
            }

            function imageLoadedEvent() {
                var canvasElement = generateContext('testCanvas', 400, 400);

                stage = new createjs.Stage('testCanvas');
                bmp = new createjs.Bitmap(img);

                stage.autoClear = true;
                stage.addChild(bmp);
                stage.update();
            }

            function generateContext(canvasID, width, height) {
                var canvasElement = document.createElement('canvas');

                if (typeof (G_vmlCanvasManager) != 'undefined')
                    canvasElement = G_vmlCanvasManager.initElement(canvasElement);

                canvasElement.setAttribute("width", width);
                canvasElement.setAttribute("height", height);
                canvasElement.setAttribute("id", canvasID);

                document.getElementById('viewer').appendChild(canvasElement);
            }
        });
    </script>
</head>
<body>
    <div id='viewer'>
        <button id='load'>load</button>
    </div>

</body>
</html>

此示例将在Chrome和IE9中运行,因为创建并使用了本机canvas元素。但是在IE8中失败了。

2 个答案:

答案 0 :(得分:3)

我也试过这个问题,试图让ExCanvas与EaselJS玩得很好。以下是我如何使用它。希望这有助于您的图像问题。

  • 获取EaselJS的源代码:https://github.com/gskinner/EaselJS.git。这将把所有的javascript文件分成各自的部分。
  • 将所有这些文件复制到项目目录中的“画架”文件夹中。
  • 加载文件的顺序很重要,请参阅下面的说明。
  • EaselJS可以选择覆盖createCanvas方法,这是使用ExCanvas所必需的。这在加载SpriteSheet.js文件之后发生,并在加载Graphics.js,DisplayObject.js,Container.js等之前发生。在下面的代码中,我使用jQuery加载了easelJs所需的其余js文件。这一切都发生在$(document).ready()函数中。
  • 如果操作正确,你应该看到一个700 x 700的画布,在IE中从左上角到右下角有一条红线(在8中测试)。

    头&GT;

            <!--
                Load ExCanvas first, and jquery
            -->
            <script type='text/javascript' src='./javascript/excanvas.js'></script>
            <script type='text/javascript' src='./javascript/jquery-1.8.2.min.js'></script>
    
            <!--
                Have to load Easel js files in a certain order, and override the createCanvas
                function in order for it to work in < IE9.
            -->
            <script type='text/javascript' src='./javascript/easel/UID.js'></script>
            <script type='text/javascript' src='./javascript/easel/Ticker.js'></script>
            <script type='text/javascript' src='./javascript/easel/EventDispatcher.js'></script>
            <script type='text/javascript' src='./javascript/easel/MouseEvent.js'></script>
            <script type='text/javascript' src='./javascript/easel/Matrix2D.js'></script>
            <script type='text/javascript' src='./javascript/easel/Point.js'></script>
            <script type='text/javascript' src='./javascript/easel/Rectangle.js'></script>
            <script type='text/javascript' src='./javascript/easel/Shadow.js'></script>
            <script type='text/javascript' src='./javascript/easel/SpriteSheet.js'></script>
    
            <script type='text/javascript'>
                var canvas, stage;
    
                createjs.createCanvas = function () { return getCanvas(); };
    
                function getCanvas() {
                    // This is needed, otherwise it will keep adding canvases, but it only use the last one it creates.
                    canvas = document.getElementById("myCanvas");
                    if (canvas != null) {
                        document.getElementById("container").removeChild(canvas);
                    }
                    canvas = document.createElement("canvas");
                    document.getElementById("container").appendChild(canvas);
                    if (typeof (G_vmlCanvasManager) != 'undefined') {
                        canvas = G_vmlCanvasManager.initElement(canvas);
                        canvas.setAttribute("height", "700");
                        canvas.setAttribute("width", "700");
                        canvas.setAttribute("style", "height:700px; width:700px;");
                        canvas.setAttribute("id", "myCanvas");
                    }
                    return canvas;
                }
            </script>
    
            <script type="text/javascript">
                $(document).ready(function () {
                    loadOtherScripts();
                    stage = new createjs.Stage(canvas);
    
                    // Draw a red line from top left to bottom right
                    var line = new createjs.Shape();
                    line.graphics.clear();
                    line.graphics.setStrokeStyle(2);
                    line.graphics.beginStroke("#FF0000");
                    line.graphics.moveTo(0, 0);
                    line.graphics.lineTo(700, 700);
                    stage.addChild(line);
    
                    stage.update();
                });
    
                function loadOtherScripts() {
                    var jsAr = new Array(13);
    
                    jsAr[0] = './javascript/easel/Graphics.js';
                    jsAr[1] = './javascript/easel/DisplayObject.js';
                    jsAr[2] = './javascript/easel/Container.js';
                    jsAr[3] = './javascript/easel/Stage.js';
                    jsAr[4] = './javascript/easel/Bitmap.js';
                    jsAr[5] = './javascript/easel/BitmapAnimation.js';
                    jsAr[6] = './javascript/easel/Shape.js';
                    jsAr[7] = './javascript/easel/Text.js';
                    jsAr[8] = './javascript/easel/SpriteSheetUtils.js';
                    jsAr[9] = './javascript/easel/SpriteSheetBuilder.js';
                    jsAr[10] = './javascript/easel/DOMElement.js';
                    jsAr[11] = './javascript/easel/Filter.js';
                    jsAr[12] = './javascript/easel/Touch.js';
    
                    for (var i = 0; i < jsAr.length; i++) {
                        var js = jsAr[i];
                        $.ajax({
                            async: false,
                            type: "GET",
                            url: js,
                            data: null,
                            dataType: 'script'
                        });
                    }
                }
        </head>
        <body>
            <div id="container"></div>
        </body>
    </html>
    

答案 1 :(得分:0)

您应该通过引用项目页面example上提供的原始画布源来实例化准canvas元素:

<head>
<!--[if lt IE 9]><script src="excanvas.js"></script><![endif]-->
</head>

var el = document.createElement('canvas');
G_vmlCanvasManager.initElement(el);
var ctx = el.getContext('2d'); 

修改

经过进一步调查后,我得出以下结论! 无法将图像包含在画布中有多种原因:

  1. 1st可能在excanvas代码中存在一个错误,因为它无法模仿本机画布功能。我更成功地使用了修改过的sancha代码,您可以在此处获取:http://dev.sencha.com/playpen/tm/excanvas-patch/excanvas-modified.js。请参阅此处的实时示例:http://jsfiddle.net/aDHKm/在IE上尝试)。
  2. 在Easeljs首先初始化核心对象和类之前的第二个是搜索canvas元素存在。因为IE&lt; 9没有实现画布,显然无法实例化easeljs核心图形API。我认为这些是您的代码无法正常工作的两个主要原因。
  3. 我的建议是尝试在没有画架的情况下重新制作代码。我做了一个必要的修改小提示,告诉你如何在没有easeljs的情况下完成:http://jsfiddle.net/xdXrw/。我不知道你是否绝对有必要使用easeljs,但肯定它在IE8黑客画布方面有一些限制。