JavaScript画布不会从脚本中绘制

我在画布上用六边形制成了一个六边形,当我在脚本中更改其大小并刷新页面时,它可以完美地工作。当然,它需要随时随地更改大小,因此我放置了一个输入数字字段,但是当我尝试将html的大小提供给脚本时,会发生下一件事:

这里是代码:

	function size()
	{
		return document.getElementById('myNumber').value;
	}
	
	function drawHex(ctx, x, y)
	{ 
		var side = 0;
		var size = 10;
		
		ctx.beginPath();
		ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
		
		ctx.fillStyle = "#D1CBCA";
		ctx.strokeStyle = "#CCCCCC";
		ctx.lineWidth = 1;
		
		for (side; side < 7; side++)
		{
			ctx.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
		}
		
		ctx.fill();
	}
	
	function drawSixHex(ctx, x, y)
	{
		drawHex(ctx, x, y);
		drawHex(ctx, x-16, y+10);
		drawHex(ctx, x-16, y-10);
		drawHex(ctx, x+16, y+10);
		drawHex(ctx, x+16, y-10);
		drawHex(ctx, x, y+2*10);
		drawHex(ctx, x, y-2*10);
	}
	
	function drawBoard(x, y, ctx, m)
	{
		ctx.clearRect(0,0,980,520);
		var oldX = x;
		var oldY = y;
			
		var diffX = 16;
		var diffY = 10;
		
		for (var i = 1; i<=(2*m+1); i++)
		{
			for (var j = 0; j<m+1; j++)
			{
				if(i%2!=0)
				{
					drawHex(ctx, oldX+i*diffX, oldY-diffY+2*j*diffY);
				}
				else
				{
					drawHex(ctx, oldX+i*diffX, oldY+2*j*diffY);
				}
			}	
		}
		
		for(var k = 1; k<m; k=k+2)
		{
		
			drawSixHex(ctx, oldX+(k+2)*diffX, oldY-k*diffY);
			drawSixHex(ctx, oldX+(k+2)*diffX, (oldY+2*m*diffY)+(k-2)*diffY);
			
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, oldY+(k-(m-1))*diffY);
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, (oldY+2*m*diffY)-(k-(m-3))*diffY);
		
		}
	
		for(var g = -2*m; g<2*m-2; g=g+2)
		{
			drawSixHex(ctx, oldX + (m+1)*diffX, oldY+(g)*diffY+(m+1)*diffY);
		}
		
	}
	
	var canvas=document.getElementById("canvas");
	var ctx=canvas.getContext("2d");
	
	
	drawBoard(250, 250, ctx, size());
    <!DOCTYPE html>
	<html>
		<head>
			<meta charset="utf-8" />
			<title>Canvas Hexagonal Map</title>
		</head>
		<body>
			<canvas id="canvas" width="980" height="520" ></canvas>
			<br>
			<br>
			
			
	
			<input type="number" id="myNumber" value="6" min="3" max="8">
			<button onclick="size()">Try it</button>
			
			<script src="script.js"></script>
			
			
		</body>
	</html>
回答如下:

您需要将size()转换为数字。在size()前面添加了+

function size()
	{
		return +(document.getElementById('myNumber').value);
	}
	
	function drawHex(ctx, x, y)
	{ 
		var side = 0;
		var size = 10;
		
		ctx.beginPath();
		ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
		
		ctx.fillStyle = "#D1CBCA";
		ctx.strokeStyle = "#CCCCCC";
		ctx.lineWidth = 1;
		
		for (side; side < 7; side++)
		{
			ctx.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
		}
		
		ctx.fill();
	}
	
	function drawSixHex(ctx, x, y)
	{
		drawHex(ctx, x, y);
		drawHex(ctx, x-16, y+10);
		drawHex(ctx, x-16, y-10);
		drawHex(ctx, x+16, y+10);
		drawHex(ctx, x+16, y-10);
		drawHex(ctx, x, y+2*10);
		drawHex(ctx, x, y-2*10);
	}
	
	function drawBoard(x, y, ctx, m)
	{
		ctx.clearRect(0,0,980,520);
		var oldX = x;
		var oldY = y;
			
		var diffX = 16;
		var diffY = 10;
		
		for (var i = 1; i<=(2*m+1); i++)
		{
			for (var j = 0; j<m+1; j++)
			{
				if(i%2!=0)
				{
					drawHex(ctx, oldX+i*diffX, oldY-diffY+2*j*diffY);
				}
				else
				{
					drawHex(ctx, oldX+i*diffX, oldY+2*j*diffY);
				}
			}	
		}
		
		for(var k = 1; k<m; k=k+2)
		{
		
			drawSixHex(ctx, oldX+(k+2)*diffX, oldY-k*diffY);
			drawSixHex(ctx, oldX+(k+2)*diffX, (oldY+2*m*diffY)+(k-2)*diffY);
			
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, oldY+(k-(m-1))*diffY);
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, (oldY+2*m*diffY)-(k-(m-3))*diffY);
		
		}
	
		for(var g = -2*m; g<2*m-2; g=g+2)
		{
			drawSixHex(ctx, oldX + (m+1)*diffX, oldY+(g)*diffY+(m+1)*diffY);
		}
		
	}
	
	var canvas=document.getElementById("canvas");
	var ctx=canvas.getContext("2d");
	
	function draw(){
	  drawBoard(250, 250, ctx, +size());
  }
<!DOCTYPE html>
	<html>
		<head>
			<meta charset="utf-8" />
			<title>Canvas Hexagonal Map</title>
		</head>
		<body>
			<canvas id="canvas" width="980" height="520" ></canvas>
			<br>
			<br>
			
			
	
			<input type="number" id="myNumber" value="6" min="3" max="8">
			<button onclick="draw()">Try it</button>
			
			<script src="script.js"></script>
			
			
		</body>
	</html>

JavaScript画布不会从脚本中绘制

我在画布上用六边形制成了一个六边形,当我在脚本中更改其大小并刷新页面时,它可以完美地工作。当然,它需要随时随地更改大小,因此我放置了一个输入数字字段,但是当我尝试将html的大小提供给脚本时,会发生下一件事:

这里是代码:

	function size()
	{
		return document.getElementById('myNumber').value;
	}
	
	function drawHex(ctx, x, y)
	{ 
		var side = 0;
		var size = 10;
		
		ctx.beginPath();
		ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
		
		ctx.fillStyle = "#D1CBCA";
		ctx.strokeStyle = "#CCCCCC";
		ctx.lineWidth = 1;
		
		for (side; side < 7; side++)
		{
			ctx.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
		}
		
		ctx.fill();
	}
	
	function drawSixHex(ctx, x, y)
	{
		drawHex(ctx, x, y);
		drawHex(ctx, x-16, y+10);
		drawHex(ctx, x-16, y-10);
		drawHex(ctx, x+16, y+10);
		drawHex(ctx, x+16, y-10);
		drawHex(ctx, x, y+2*10);
		drawHex(ctx, x, y-2*10);
	}
	
	function drawBoard(x, y, ctx, m)
	{
		ctx.clearRect(0,0,980,520);
		var oldX = x;
		var oldY = y;
			
		var diffX = 16;
		var diffY = 10;
		
		for (var i = 1; i<=(2*m+1); i++)
		{
			for (var j = 0; j<m+1; j++)
			{
				if(i%2!=0)
				{
					drawHex(ctx, oldX+i*diffX, oldY-diffY+2*j*diffY);
				}
				else
				{
					drawHex(ctx, oldX+i*diffX, oldY+2*j*diffY);
				}
			}	
		}
		
		for(var k = 1; k<m; k=k+2)
		{
		
			drawSixHex(ctx, oldX+(k+2)*diffX, oldY-k*diffY);
			drawSixHex(ctx, oldX+(k+2)*diffX, (oldY+2*m*diffY)+(k-2)*diffY);
			
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, oldY+(k-(m-1))*diffY);
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, (oldY+2*m*diffY)-(k-(m-3))*diffY);
		
		}
	
		for(var g = -2*m; g<2*m-2; g=g+2)
		{
			drawSixHex(ctx, oldX + (m+1)*diffX, oldY+(g)*diffY+(m+1)*diffY);
		}
		
	}
	
	var canvas=document.getElementById("canvas");
	var ctx=canvas.getContext("2d");
	
	
	drawBoard(250, 250, ctx, size());
    <!DOCTYPE html>
	<html>
		<head>
			<meta charset="utf-8" />
			<title>Canvas Hexagonal Map</title>
		</head>
		<body>
			<canvas id="canvas" width="980" height="520" ></canvas>
			<br>
			<br>
			
			
	
			<input type="number" id="myNumber" value="6" min="3" max="8">
			<button onclick="size()">Try it</button>
			
			<script src="script.js"></script>
			
			
		</body>
	</html>
回答如下:

您需要将size()转换为数字。在size()前面添加了+

function size()
	{
		return +(document.getElementById('myNumber').value);
	}
	
	function drawHex(ctx, x, y)
	{ 
		var side = 0;
		var size = 10;
		
		ctx.beginPath();
		ctx.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
		
		ctx.fillStyle = "#D1CBCA";
		ctx.strokeStyle = "#CCCCCC";
		ctx.lineWidth = 1;
		
		for (side; side < 7; side++)
		{
			ctx.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
		}
		
		ctx.fill();
	}
	
	function drawSixHex(ctx, x, y)
	{
		drawHex(ctx, x, y);
		drawHex(ctx, x-16, y+10);
		drawHex(ctx, x-16, y-10);
		drawHex(ctx, x+16, y+10);
		drawHex(ctx, x+16, y-10);
		drawHex(ctx, x, y+2*10);
		drawHex(ctx, x, y-2*10);
	}
	
	function drawBoard(x, y, ctx, m)
	{
		ctx.clearRect(0,0,980,520);
		var oldX = x;
		var oldY = y;
			
		var diffX = 16;
		var diffY = 10;
		
		for (var i = 1; i<=(2*m+1); i++)
		{
			for (var j = 0; j<m+1; j++)
			{
				if(i%2!=0)
				{
					drawHex(ctx, oldX+i*diffX, oldY-diffY+2*j*diffY);
				}
				else
				{
					drawHex(ctx, oldX+i*diffX, oldY+2*j*diffY);
				}
			}	
		}
		
		for(var k = 1; k<m; k=k+2)
		{
		
			drawSixHex(ctx, oldX+(k+2)*diffX, oldY-k*diffY);
			drawSixHex(ctx, oldX+(k+2)*diffX, (oldY+2*m*diffY)+(k-2)*diffY);
			
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, oldY+(k-(m-1))*diffY);
			drawSixHex(ctx, oldX+(m-1)*diffX+(k+2)*diffX, (oldY+2*m*diffY)-(k-(m-3))*diffY);
		
		}
	
		for(var g = -2*m; g<2*m-2; g=g+2)
		{
			drawSixHex(ctx, oldX + (m+1)*diffX, oldY+(g)*diffY+(m+1)*diffY);
		}
		
	}
	
	var canvas=document.getElementById("canvas");
	var ctx=canvas.getContext("2d");
	
	function draw(){
	  drawBoard(250, 250, ctx, +size());
  }
<!DOCTYPE html>
	<html>
		<head>
			<meta charset="utf-8" />
			<title>Canvas Hexagonal Map</title>
		</head>
		<body>
			<canvas id="canvas" width="980" height="520" ></canvas>
			<br>
			<br>
			
			
	
			<input type="number" id="myNumber" value="6" min="3" max="8">
			<button onclick="draw()">Try it</button>
			
			<script src="script.js"></script>
			
			
		</body>
	</html>