编程语言
首页 > 编程语言> > javascript – 获得IE8与EaselJS和ExplorerCanvas的兼容性

javascript – 获得IE8与EaselJS和ExplorerCanvas的兼容性

作者:互联网

我正在使用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中失败了.

解决方法:

我也遇到了这个问题,试图让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>

标签:easeljs,javascript,canvas,internet-explorer-8,internet-explorer
来源: https://codeday.me/bug/20191007/1867269.html