1

我在这段代码中遇到了问题(我在本地 PC 上工作)。

谢谢你的帮助。

我的代码如下:

<html>
    <head>
        <title>My Test</title>
        <script src="../jquery/js/jquery-1.9.0.min.js"></script>  
    </head>
    <body>
        <script>
            var _canvas = document.createElement('canvas');
            _canvas.width="100";
            _canvas.height="100"; 
            var _context = _canvas.getContext("2d");
            var _img = new Image();
            _img.src = "image/myImgTest.png";
            var _url;

            _img.onload = function() {
            DrawScreen(); // just some process: drawing image
            _url = _canvas.toDataURL(); 
            alert("test"); **// this alert is printed on FF not on Chrome ???**
            // window.location = url; **// With this command on FF, I show my Image**
            document.write("<img src='"+url+"' >"); **// on FF, I show my image but when I would like to see the source code on my current page, there is nothing just blank ?? And on Chrome, I never show my image.** 

            **// In fact, I am expected to generate something like this: <img src="data:image/png;base64,iVBORw0KGgo.....">  at this place **

            };
            function DrawScreen() {
                // my process
            }

        </script>

    </body>
</html>

* // 事实上,我预计会生成这样的东西:在这个地方 *

4

1 回答 1

0

当您“在本地 PC 上工作”时……您的本地 PC 上是否有网络服务器,或者您是否直接从硬盘驱动器打开 HTML 文件?

您可能希望.src在事件处理程序之后移动 to 的分配.onload,否则可能.onload永远不会被触发。

var _img = new Image();
var _url;

_img.onload = function() {
   ...
};
_img.src = "image/myImgTest.png";
于 2013-02-28T07:44:57.160 回答