Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我再次绘制立方体时,立方体变得更大了。(画布,Javascript)_Javascript_Html_Css_Canvas - Fatal编程技术网

当我再次绘制立方体时,立方体变得更大了。(画布,Javascript)

当我再次绘制立方体时,立方体变得更大了。(画布,Javascript),javascript,html,css,canvas,Javascript,Html,Css,Canvas,//选择器 canvas=document.querySelector.canvas'; canvas.setAttributetabindex,0; 聚焦; pointerImg=document.querySelector'.pointer'; //变数 常数pi=Math.pi; const c=canvas.getContext'2d'; 变量网格={ “x”:60, y:20岁, “尺寸”:20, } var pointerValues={ 37:{'x':-1,'y':0}, 38:

//选择器 canvas=document.querySelector.canvas'; canvas.setAttributetabindex,0; 聚焦; pointerImg=document.querySelector'.pointer'; //变数 常数pi=Math.pi; const c=canvas.getContext'2d'; 变量网格={ “x”:60, y:20岁, “尺寸”:20, } var pointerValues={ 37:{'x':-1,'y':0}, 38:{'x':0,'y':-1}, 39:{'x':1,'y':0}, 40:{'x':0,'y':1}, } //事件侦听器 window.addEventListener'resize',函数{ //初始化; } var单元格=[]; forlet i=0;你被边界的厚度欺骗了 边界很厚,很容易忘记。如果绘制一个X宽的长方体,其左边框和右边框是X宽之外的

所以,如果你不填充内部,你会看到左边这个漂亮的外观,但是如果你填充,你会看到右边这个丑陋的外观

当您绘制这些方块的网格时,每个方块都覆盖了前面方块的右侧和底部,因此不明显发生了什么

除非你重新画一个不是列表中最后一个的,就像我在中下方做的那样。然后,很明显,有些地方出了问题

下面是重现上图的代码,下面我推荐一个解决方案

var c=document.getElementByIdmyCanvas; var ctx=c.getContext2d; //没有填充,看起来很好 ctx.beginPath; ctx.rect40,40,40,40; ctx.stroke; ctx.closePath; ctx.beginPath; ctx.rect80,80,40,40; ctx.stroke; ctx.closePath; //使用白色填充物时,每个盒子的内半部分会被白色填充物覆盖。 ctx.beginPath; ctx.rect140,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect180,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; //制作网格。这部分最初看起来不错。 var c=document.getElementByIdmyCanvas; var ctx=c.getContext2d; ctx.beginPath; ctx.rect240,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect240,80,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect280,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect280,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect320,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect320,80,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; //重画一:这看起来会一团糟。 ctx.beginPath; ctx.rect280,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; 你被边界的厚度欺骗了 边界很厚,很容易忘记。如果绘制一个X宽的长方体,其左边框和右边框是X宽之外的

所以,如果你不填充内部,你会看到左边这个漂亮的外观,但是如果你填充,你会看到右边这个丑陋的外观

当您绘制这些方块的网格时,每个方块都覆盖了前面方块的右侧和底部,因此不明显发生了什么

除非你重新画一个不是列表中最后一个的,就像我在中下方做的那样。然后,很明显,有些地方出了问题

下面是重现上图的代码,下面我推荐一个解决方案

var c=document.getElementByIdmyCanvas; var ctx=c.getContext2d; //没有填充,看起来很好 ctx.beginPath; ctx.rect40,40,40,40; ctx.stroke; ctx.closePath; ctx.beginPath; ctx.rect80,80,40,40; ctx.stroke; ctx.closePath; //使用白色填充物时,每个盒子的内半部分会被白色填充物覆盖。 ctx.beginPath; ctx.rect140,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect180,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; //制作网格。这部分最初看起来不错。 var c=document.getElementByIdmyCanvas; var ctx=c.getContext2d; ctx.beginPath; ctx.rect240,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect240,80,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect280,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect280,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect320,40,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; ctx.beginPath; ctx.rect320,80,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath; //重画一:这看起来会一团糟。 ctx.beginPath; ctx.rect280,40,40; ctx.stroke; ctx.fillStyle=白色; ctx.fill; ctx.closePath;
网格大小是多少?你的意思是这个.size吗?请用一个演示问题的示例更新你的问题,最好是使用[]工具栏按钮;的堆栈片段运行的问题。它们是一样的,我将其更改为grid.size进行调试,但是
它不起作用。当更新grid.size时,这两种方法都会导致单元格稍微大一点?你的意思是这个.size吗?请用一个演示问题的示例更新你的问题,最好是使用[]工具栏按钮;的堆栈片段运行的问题。它们是一样的,我将其更改为grid.size进行调试,但它不起作用。在更新时,这两种方法都会导致单元稍微大一点