简体   繁体   English

HTML5 Canvas无法在外部JavaScript文件中使用

[英]HTML5 Canvas not working in external JavaScript file

I have written this code in JavaScript and works perfectly fine when I include it on my index.html page: 我已经用JavaScript编写了这段代码,当我将它包含在index.html页面上时,它可以正常工作:

<canvas id="bannerCanvas" width="960" height="200">
    Your browser does not support the canvas element.
</canvas>

<script type="text/javascript">

   var canvas = document.getElementById("bannerCanvas");
   var context = canvas.getContext("2d");
     context.beginPath();
     context.moveTo(25,25);
     context.lineTo(355,55);
     context.lineTo(355,125);
     context.lineTo(25,125);
     context.moveTo(465,25);
     context.fill();
};  
</script>

...however when I place it in an external JavaScript file, it won't work! ...但是当我将它放在外部JavaScript文件中时,它将无法正常工作! In the head of the index page, I have declared this: 在索引页面的头部,我已声明:

 <script type="text/javascript" src="JavaScript/functionality.js">
 </script>

And I save this functionality.js file in the JavaScript directory, I've tried doing other JS functions in this file to check the index page and the JS are connected and they are...The answer is probably staring me in the face but for some reason I cannot see it! 我在JavaScript目录中保存了这个functional.js文件,我已经尝试在这个文件中做其他JS函数来检查索引页面并且JS已连接并且它们是......答案可能正在盯着我但是由于某种原因,我看不到它!

Any help is much appreciated, thank you. 非常感谢任何帮助,谢谢。

EDIT: I've been using Firebug and it is giving me no errors, when I use the code on the index page, I am seeing the shape I want yet when using the external JS file I am just seeing a big black rectangle. 编辑:我一直在使用Firebug并且它没有给我任何错误,当我在索引页面上使用代码时,我在使用外部JS文件时看到了我想要的形状我只看到一个大的黑色矩形。

the reason for this is that the script is being run before the canvas element is rendered. 原因是脚本在canvas元素呈现之前运行。

To fix it, add this in your external file. 要修复它,请在外部文件中添加它。

document.addEventListener('DOMContentLoaded',domloaded,false);
function domloaded(){
    // your code here.
}

or with jquery 或者使用jquery

$(function(){
    // your code here.    
});

Within functionality.js , try wrapping your code in functionality.js ,尝试将代码包装在中

window.onload = function() {
// code here
}

so that it won't execute until after the page has loaded. 这样在页面加载之前它才会执行。

If it is in the head, you are loading it before the canvas element is rendered. 如果它在头部,则在渲染canvas元素之前加载它。 Look at the JavaScript console and you will see a Null or undefined error. 查看JavaScript控制台,您将看到Null或未定义的错误。

Add the script tag in the same place it works with the inline code. 将脚本标记添加到与内联代码一起使用的相同位置。 JavaScript does not have to live in the head and some developers will recommend it should always be the last thing in the body. JavaScript不必生活在头脑中,一些开发人员会建议它应该始终是身体中的最后一件事。

Other solution is to run the script on document ready or window onload. 其他解决方案是在文档就绪或window onload上运行脚本。

不要放在头脑中,当你把代码放在头部时,它会在页面中没有canvas元素时运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM