gpt4 book ai didi

javascript - 创建并填充 3D 数组 JavaScript

转载 作者:行者123 更新时间:2023-11-30 20:00:23 24 4
gpt4 key购买 nike

我正在尝试用“rgb(255, 48, 144)”填充一个 3d 数组 (16x16x16),然后通过在数组中找到它来绘制一个具有这种颜色的 Rect (arr[15][3][9] ).

这是我目前所得到的:

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

var arr = new Array(16)
for(var i = 0; i < arr.length; i++){

arr[i] = new Array(16);

for(var j = 0; j < arr[i].length; j++){


arr[i][j] = "rgb(255, 48, 144)";

for(var k = 0; k < arr[i][j].length; k++){
arr[i][j][k] = "rgb(255, 48, 144)";
}

}
}
ctx.fillstyle = arr[15][3][9];
ctx.fillRect(0, 0, 100, 100);
<!doctype HTML>
<html>

<head>
<meta charset="UTF-8">
<title> Fargekube </title>
</head>


<body>

<canvas id="canvas" width="200" height="200"></canvas>

</body>
</html>

最佳答案

我认为您对 3 维数组的初始化不当。以下是您尝试执行的操作吗?

此外,它是 fillStyle 而不是 fillstyle

var ctx = document.getElementById('canvas').getContext('2d');
var canvas = document.getElementById('canvas');

var dimensionSize = 16;

var arr = [];

for (var i = 0; i < dimensionSize; i += 1) {
arr[i] = [];

for (var j = 0; j < dimensionSize; j += 1) {
arr[i][j] = [];

for (var k = 0; k < dimensionSize; k += 1) {
arr[i][j][k] = 'rgb(255, 48, 144)';
}
}
}

console.log(arr[15][3][9]);

ctx.fillStyle = arr[15][3][9];
ctx.fillRect(0, 0, 100, 100);
<!doctype HTML>
<html>

<head>
<meta charset="UTF-8">
<title> Fargekube </title>
</head>


<body>

<canvas id="canvas" width="200" height="200"></canvas>

</body>
</html>

关于javascript - 创建并填充 3D 数组 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53443326/

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