gpt4 book ai didi

javascript - HTML5 Canvas - For 循环将元素放置在网格中

转载 作者:行者123 更新时间:2023-12-02 15:29:06 25 4
gpt4 key购买 nike

我正在 HTML5 Canvas 中构建一个简单的关卡放置脚本,但在实际放置每个元素时遇到了困难。我编写了一个创建网格的 for 循环,然后尝试创建一个新循环,该循环遍历“level”数组中的每个语句来设置每个图像的位置。我之前并没有对 Canvas 做过很多工作;因此,我不确定在放置这些图像时我做错了什么;我在 jQuery 中编写了与此非常相似的内容,效果很好 - 事实上,我首先复制并粘贴了代码,但它似乎在 Canvas 中不起作用。任何帮助将不胜感激!

这是代码片段;请原谅过度评论,这只是当我无法理解为什么某些东西不起作用时我会做的事情。它就像一个内联橡皮鸭。

var $levelArray = [
[0, 0, 0, "blue", "blue"],
[0, "gray", 0, 0, 0],
["blue", "blue", "green", 0, "blue"],
["blue", 0, "yellow", 0, 0],
[0, 0, 0, "gray", 0],
["red", 0, 0, 0, 0]];

var border = 5, // set grid details
spaceWidth = 80,
spaceAmount = 5;

// create a tiled image
function makeTile(imageUrl, horizontalPosition, verticalPosition) {
var tile = new Image();

tile.onload = function() {
context.drawImage(tile, horizontalPosition, verticalPosition);
}
tile.src = imageUrl;
}

// place the image tiles on the board
for (var i=0; i < ($levelArray.length - 1); i++) {
var row = $levelArray[i]; // set each row's iterative position
var rowHeight = 5;

for (var j=0; j < row[j].length; j++) {
var rowPosition = 5; // set the left margin of each element

if (row[j] == 0) {
rowPosition += (spaceWidth + 5); // if an element does not exist, jump forwards to the next space
} else {
//if one DOES exist, place an image in this space
makeTile("http://lorempixel.com/80/80", rowPosition, rowHeight);
rowPosition += (spaceWidth + 5); // then move to the next space
};
};
rowHeight += (spaceWidth + 5); // once a row is complete, drop to the next row's positions
};

我在代码笔中有这个:http://codepen.io/sarsparillo/pen/vNrWQG

我不知道为什么它一次只加载一张图像并将其放在网格上的 0,0 空间中;在 jQuery 中使用非常相似的代码(当前代码,到处都是而且有点不干净,在这里 - http://codepen.io/sarsparillo/pen/GpdjYY )将元素放在正确的位置就好了。

此外,当我向这些 for 循环添加 console.log 语句时,我真的无法弄清楚它到底从哪里获取数据。比如,一次迭代给了我“绿绿黄”作为 row[j] 中的项目,另一个“蓝灰蓝”——Canvas 在迭代数组时是否做了一些绝对奇怪的事情?我不明白这是怎么回事,因为这只是 Javascript,但是......?

有谁知道为什么会发生这种情况,或者有任何关于如何解决此问题的提示吗?理论上,它应该只是将空间宽度+边距宽度添加到每个“正方形”的起点,所以我不确定为什么它只是......不是。

最佳答案

jsFiddle:https://jsfiddle.net/CanvasCode/4gr9apqm/

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");

var border = 5, // set grid details
spaceWidth = 80,
spaceAmount = 5;

var $levelArray = [
["blue", 0, 0, "blue", "blue"],
[0, "gray", 0, 0, 0],
["blue", "blue", "green", 0, "blue"],
["blue", 0, "yellow", 0, 0],
[0, 0, 0, "gray", 0],
["red", 0, 0, 0, 0]
];

canvas.width = (spaceWidth * spaceAmount) + (5 * spaceAmount) + 5; // and here's the canvas size
canvas.height = (spaceWidth * spaceAmount) + (5 * spaceAmount) + 5;

// make a rounded corner square; using a sizing hack to make sure that strokes don't effect the full size of the item
function square(originX, originY, size, corner, fill) {
var startFromX = originX + (corner / 2);
var startFromY = originY + (corner / 2);
var extentsX = startFromX + (size - corner);
var extentsY = startFromY + (size - corner);
context.lineJoin = "round";
context.lineWidth = corner;
context.fillStyle = "#513574";
context.strokeStyle = fill;

context.beginPath();
context.moveTo(startFromX, startFromY);
context.lineTo(startFromX, extentsY);
context.lineTo(extentsX, extentsY);
context.lineTo(extentsX, startFromY);
context.closePath();
context.stroke();
context.fill();
}

// build a grid of said squares
function squareGrid(spacing, size, corner, color, amount) {
for (var x = 0; x < amount; x++) {
// build rows
for (var y = 0; y < amount; y++) {
// build column spacing in each row
square(5 + (size * x) + (spacing * x), 5 + (size * y) + (spacing * y), size, corner, color);
// build each square
}
};
};

// actually parse the arguments for said square
squareGrid(border, spaceWidth, (border * 2), "#f13574", spaceAmount);

// create a tiled image
function makeTile(tile, horizontalPosition, verticalPosition) {
switch (tile) {
case "blue":
context.fillStyle = "#00F";
context.fillRect(horizontalPosition, verticalPosition, 80, 80);
break;
case "green":
context.fillStyle = "#0F0";
context.fillRect(horizontalPosition, verticalPosition, 80, 80);
break;
case "red":
context.fillStyle = "#F00";
context.fillRect(horizontalPosition, verticalPosition, 80, 80);
break;
case "gray":
context.fillStyle = "#999";
context.fillRect(horizontalPosition, verticalPosition, 80, 80);
break;
case "yellow":
context.fillStyle = "#FF0";
context.fillRect(horizontalPosition, verticalPosition, 80, 80);
break;
}
};

var gapHeight = 5;
var gapWidth = 5;

for (var y = 0; y < $levelArray.length - 1; y++) {
var row = $levelArray[y];

for (var x = 0; x < row.length; x++) {
var newXPos = (gapWidth * (x + 1)) + (80 * x);
var newYPos = (gapHeight * (y + 1)) + (80 * y)

makeTile($levelArray[y][x], newXPos, newYPos);
}
}

我所做的更改基本上是您访问二维数组和位置计算的方式。基本上,您首先访问的是 Y 位置,然后从 Y 位置查看该行上的所有 block 。所以我们首先找到“在我的例子中”blue, 0, 0, blue, blue。所以所有这些的 y 位置都是 0,然后它会从 0、80、160 等开始。但是,因为你想要 block 之间有一个间隙,所以你还必须将间隙乘以当时的 x 和 y 值:)

关于javascript - HTML5 Canvas - For 循环将元素放置在网格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33484437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com