通过 Modernizr.load 加载 excanvas polyfill 失败

发布于 2024-12-21 15:12:56 字数 1468 浏览 7 评论 0原文

我正在尝试在我的页面的特定于页面的 js 文件中加载 excanvas polyfill。该脚本文件插入在我的页面上的正文标记之后。

奇怪的是,如果我

<script type='text/javascript' src='/Scripts/polyfills/excanvas.compiled.js'></script>

在 head 标签中使用,一切都会很好,但如果不需要的话,我不一定想为 HTML5 兼容的浏览器加载这个脚本。

所以我很自然地尝试了使用 Modernizr 有选择地加载它。这是我完美执行但不起作用的 JavaScript 代码:

<!-- language: lang-js -->
$(function () {
    Modernizr.load({
        test: Modernizr.canvas,
        nope: '/Scripts/polyfills/excanvas.compiled.js',
        complete: function () {
            setImage();
        }
    });

});

这似乎工作正常。 excanvas 脚本似乎已成功加载。 setImage 函数动态创建canvas 元素并将其添加到页面上的div 中。 这在 IE9 中工作正常,但在 IE8 中无法呈现。

<!-- language: lang-js -->
function setImage() {

    var canvasHello = document.createElement('canvas');
    canvasHello.id = 'canvasHello';
    $('#divContent').append(canvasHello);

    if (!Modernizr.canvas) {
        G_vmlCanvasManager.initElement(canvasHello);
    }

    var canvasContext = canvasHello.getContext('2d');
    canvasContext.width = 800;
    canvasContext.height = 600;
    canvasContext.fillStyle = "#000000";
    canvasContext.fillRect(0, 0, 600, 800);

    var img = document.createElement('img');
    img.src = '/Content/images/hello.png';
    img.onload = function () {
        canvasContext.drawImage(img, 100, 25, 100, 100);
    }
}

我是否错过了某些内容,或者 excanvas 脚本在 head 标签之外无法运行?

I'm attempting to load the excanvas polyfill in the page-specific js file for my page. This script file is inserted after the body tag on my page.

The odd bit is that if I use

<script type='text/javascript' src='/Scripts/polyfills/excanvas.compiled.js'></script>

in my head tag, everything works great, but I don't necessarily want to load this script for HTML5 compliant browsers if I don't have to.

So naturally I tried to use Modernizr to load it selectively. This is my perfectly executing, but non-functioning javascript code:

<!-- language: lang-js -->
$(function () {
    Modernizr.load({
        test: Modernizr.canvas,
        nope: '/Scripts/polyfills/excanvas.compiled.js',
        complete: function () {
            setImage();
        }
    });

});

This seems to work fine. The excanvas script appears to load successfully.
The setImage function creates the canvas element dynamically and adds it to a div on the page.
This works fine in IE9 but fails to render in IE8.

<!-- language: lang-js -->
function setImage() {

    var canvasHello = document.createElement('canvas');
    canvasHello.id = 'canvasHello';
    $('#divContent').append(canvasHello);

    if (!Modernizr.canvas) {
        G_vmlCanvasManager.initElement(canvasHello);
    }

    var canvasContext = canvasHello.getContext('2d');
    canvasContext.width = 800;
    canvasContext.height = 600;
    canvasContext.fillStyle = "#000000";
    canvasContext.fillRect(0, 0, 600, 800);

    var img = document.createElement('img');
    img.src = '/Content/images/hello.png';
    img.onload = function () {
        canvasContext.drawImage(img, 100, 25, 100, 100);
    }
}

Did I miss something or does the excanvas script not function outside of the head tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-12-28 15:12:57

在给定的要求中,您可以使用像这样的 IE 条件语句...

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

就足够了...

该语句只能被 IE 版本低于 9 的版本理解,并且附加脚本标记。

in the given requirement you could use the IE conditional statements like this...

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

would suffice....

the statement will only be understood by IE version less than 9 and the script tag gets attached.

今天小雨转甜 2024-12-28 15:12:57

您唯一错过的是关于如何使用 excanvas 的说明

excanvas.js 文件必须包含在页面中标记中出现任何 canvas 元素之前。这是由于 IE 的限制,我们需要在 IE 看​​到标记中的任何实例之前施展魔法。建议放在头部。

The only thing you missed is the instructions on how to use excanvas:

The excanvas.js file must be included in the page before any occurrences of canvas elements in the markup. This is due to limitations in IE and we need to do our magic before IE sees any instance of in the markup. It is recommended to put it in the head.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文