简体   繁体   English

Chrome 31.0 HTML5 Canvas Draw Image

[英]Chrome 31.0 HTML5 Canvas Draw Image

Since downloading the current version of chrome (Version 31.0.1650.57) I've been completely unable to draw images in a HTML5 Canvas with my code; 由于下载了当前版本的chrome(版本31.0.1650.57),我完全无法使用我的代码在HTML5 Canvas中绘制图像; there are no errors and it's finding the resources, they just aren't drawing. 没有错误,它正在寻找资源,它们只是没有绘制。 I'd really appreciate some help on this! 我真的很感激这方面的一些帮助!

var grass_img = new Image();
grass_img.src = 'grass.gif';
grass_img.onload = draw_here(grass_img, (center_x + base_x + xpos), (center_y + base_y + ypos),1);

Which appears here and there on several different images and calls: 哪个出现在几个不同的图像和调用:

function draw_here(image, x, y, scale){
     draw_canv.drawImage(image, x, y, image.width * scale, image.height * scale);
}

X and Y are correct, as is the scale; X和Y是正确的,规模也是如此; there are no coding errors picked up by the debugger and the program worked perfectly until the latest version of chrome came out. 调试器没有发现编码错误,程序运行完美,直到最新版本的chrome出现。 Downgrading chrome is also not an option. 降级铬也不是一种选择。

The problem is in this line: 问题出在这一行:

grass_img.onload = draw_here( /* ... */ );

You seem to think that you assign the function draw_here as an onload-handler. 您似乎认为您将函数draw_here指定为onload-handler。 But that's not what really happens in this line. 但这不是这一行中真正发生的事情。 What you really do is execute draw_here immediately and assign the return-value of that function (which is undefined ) to grass_img.onload . 你真正做的是立即执行draw_here并将该函数的返回值( undefined )分配给grass_img.onload

Try this instead: 试试这个:

grass_img.onload = function() {
    draw_here(grass_img, (center_x + base_x + xpos), (center_y + base_y + ypos),1);
}

This creates an anonymous function which is assigned to the onload-handler. 这将创建一个匿名函数,该函数将分配给onload-handler。 When that anonymous function is called (which will happen when the load-event is triggered) it calls your draw-handler with your arguments. 当调用该匿名函数时(触发load-event时会发生这种情况),它会使用您的参数调用draw-handler。

I solved the problem. 我解决了这个问题。 It seems that the latest version of Chrome does not like it if you declare your image in every frame iteration. 如果你在每次迭代中声明你的图像,似乎最新版本的Chrome不喜欢它。 (Something it seems to have allowed previously, but I really shouldn't have been doing.) If you declare your images and their sources at the top of your js, outside of any functions, and only draw them in functions then the problem is solved, eg: (它似乎以前允许的东西,但我真的不应该这样做。)如果你在js的顶部声明你的图像及其来源,在任何函数之外,并且在函数中绘制它们然后问题是解决了,例如:

var image = new Image();
image.src='image.jpg';

someFunction() {
    canvas.drawImage(image,0,0);
}

Note: .src should always be called AFTER .onload . 注意: .src应该始终被称为AFTER .onload Otherwise lots of hard-to-track-down buggies (like this one) appear. 否则会出现许多难以追踪的小型车(如此款式)。 Especially if your image loads before onload can be parsed. 特别是如果你的图像加载之前onload可以解析。 Your currently accepted solution is possibly functional due to a race condition with function overhead timing. 由于具有功能开销时序的竞争条件,您当前接受的解决方案可能正常运行。

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

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