【发布时间】:2020-10-22 05:08:21
【问题描述】:
所以我正在研究一种 javascript 中的寻路算法,为了好玩和练习,我需要一个网格,我试图让用户决定画布的宽度和高度,并让他们决定有多少列和网格具有使用提示方法的行。我有这段代码,它只绘制列而不是行,无论我给网站提供什么输入
let grid = {
width: 0,
height: 0,
}
grid.width = Number(prompt("how many columns do you want? don't type zero if you care about your computer's health"));
grid.height = Number(prompt("how many rows do you want? don't type zero if you care about your computer's health"));
const canvasWidth = Number(prompt("how many pixels wide do you want the screen? i recomend around 1000 but it depends on your computer (i think)"));
const canvasHeight = Number(prompt("how many pixels high do you want the screen? i recomend aroun 500 but it depends on your computer (i think)"));
let canvas = document.createElement("CANVAS");
let ctx = canvas.getContext("2d");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.border = "1px solid black"
ctx.beginPath();
ctx.lineWidth = 1;
ctx.lineStyle = "black";
for (let x = 0; x < canvasWidth; x += canvasWidth / grid.width) {
ctx.moveTo(x, 0)
ctx.lineTo(x, canvasHeight)
}
for (let y = 0; y < canvasHeight; y += canvasHeight / grid.heigth) {
ctx.moveTo(0, y)
ctx.lineTo(canvasWidth, y)
}
ctx.stroke();
document.body.appendChild(canvas);
【问题讨论】:
-
我更新了下面回复的第一部分,以解决模糊线问题。
标签: javascript html dom