简体   繁体   English

画布问题

[英]Issues with Canvas

I'm doing some testing with the HTML5 Canvas. 我正在使用HTML5 Canvas做一些测试。 I'm trying to draw an array of images in the canvas using a for loop: 我正在尝试使用for循环在画布上绘制图像数组:

var blocks=["dirt","cactus","carvedSandstone","crackedStoneBrick","diamondOre","furnace","goldOre","grass","lapisOre","leaves","log1","sand","stone","stoneBrick","stoneMoss","woodOak"];
 for(var i;i<=blocks.length;i++){
  var id=document.getElementById(blocks[i]);
  var x=i*32;
  var y=0;
    if(x>=1000){
      y+=32;
   }
   ctx.drawImage(block,x,y);

The above code is an array of the different images I want to draw followed by a for loop which(in theory) is supposed to draw each item in the array with an X value= to the number of times the for has gone through *32. 上面的代码是我要绘制的不同图像的数组,然后是for循环(理论上应该是该循环)以X value =绘制for经历* 32的次数来绘制数组中的每个项目。 The if statement is just there to make a new row once the width is exceeded. 一旦超出宽度,if语句就会出现在新行中。 For all intents and purposes, the canvas is set up correctly. 出于所有目的和目的,画布均已正确设置。 If the above code is removed and replaced with the following code, the image intended is drawn correctly. 如果删除了上面的代码并替换为以下代码,则正确绘制了所需的图像。

dirt=document.getElementById("dirt");
ctx.drawImage(dirt,0,0);

If I'm being vague if you have any questions, please let me know. 如果您有任何疑问,如果我不确定,请告诉我。

Full Code 完整代码

var id=document.getElementById(blocks[i]); should be var block=document.getElementById(blocks[i]); 应该是var block=document.getElementById(blocks[i]); , no? ,不?

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

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