gpt4 book ai didi

javascript - 使 IE8 与 EaselJS 和 ExplorerCanvas 兼容

转载 作者:行者123 更新时间:2023-11-30 13:14:13 25 4
gpt4 key购买 nike

我正在使用 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 中运行,因为会创建和使用 native Canvas 元素。但是在 IE8 中它失败了。

最佳答案

我也遇到过这个问题,试图让 ExCanvas 与 EaselJS 配合得很好。这是我如何让它工作的。希望这有助于解决您的形象问题。

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

    头>

            <!--
    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>

关于javascript - 使 IE8 与 EaselJS 和 ExplorerCanvas 兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12724404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com