简体   繁体   English

为什么画布无法正确渲染?

[英]Why is canvas not rendering properly?

I am rather familiar with making games using the HTML5 <canvas> tag. 我对使用HTML5 <canvas>标签制作游戏非常熟悉。 When I try to run my games from Komodo Edit 9 it works perfectly. 当我尝试从Komodo Edit 9运行游戏时,它可以完美运行。

However, when I try to upload my game to ANY web hosting service, it never works properly. 但是,当我尝试将游戏上传到任何网络托管服务时,它永远无法正常运行。 Like the following garridpunching.neocities.org. 像下面的garridpunching.neocities.org。

The problem is that once the canvas renders, everything appears for a split second before disappearing instantly, leaving only a blank black canvas. 问题在于,画布渲染后,所有内容都会出现一秒钟,然后立即消失,仅留下一块空白的黑色画布。

Google debugger returns the following error for all five hostings I tried: Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state. Google调试器针对我尝试的所有五个托管服务器返回以下错误: Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state.

The error is thrown on the line ctx.drawImage(sprite.cursor, cursor.x, cursor.y, 40, 40); 该错误引发在ctx.drawImage(sprite.cursor, cursor.x, cursor.y, 40, 40); , which works perfectly when I run the html file directly from localhost. ,当我直接从localhost运行html文件时,该方法非常有效。

What does this mean and how do I fix it? 这是什么意思,我该如何解决? Thanks in advance. 提前致谢。

This could be related to an img not loading properly. 这可能与img无法正确加载有关。 Check the paths to your images, maybe you didn't copy some of them when deploying to the hosting service of you have a path somewhere that works on your local computer but not on the remote site. 检查图像的路径,也许在部署到托管服务时您没有复制其中的某些路径,但是您的路径在本地计算机上有效,但在远程站点上无效。

Also: Do you load all your images from the same domain as the js? 另外:您是否从与js相同的域中加载所有图像? If this isn't the case it could also be triggered by a Cross-Origin Resource Sharing (CORS) issue. 如果不是这种情况,也可能由跨域资源共享(CORS)问题触发。

This could be because you are calling step , ie beginning the 'game' before all the images have loaded. 这可能是因为您正在调用step ,即在加载所有图像之前开始“游戏”。 The solution would be to wait for sprite's images to be loaded before calling step . 解决的办法是在调用step之前等待sprite的图像加载。

load();
window.onload = step; // may need inline .onload() calls on sprite's imgs

The reason this wouldn't be happening locally is because it will load the requested images instantly, as it doesn't have to download them or anything like that. 之所以不会在本地发生,是因为它会立即加载请求的图像,因为它无需下载图像或类似的东西。

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

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