JavaScript 创建网格的公式
JavaScript formula to create grid
我想使用 JavaScript.I 平铺对象网格忘记了公式,似乎无法在任何地方找到它:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
for ( j = 0; j < col; j ++ ) {
var object = new object();
object.position.x = 0 + width * j
// do i nee another loop here?
//add object to....
}
这将 return 一行 10 个按宽度间隔的对象,但我还想要像 10x10 网格这样的列,就在 JavaScript..
中的公式之后
您可以嵌套两个 for
循环,如下所示:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
var space = ...;
// Rows loop
for ( j = 0; j < row; j ++ ) {
// For each column in the row
for ( i = 0; i < col; i ++ ) {
// j is the row index and i is the column index..
var object = new object();
object.position.x = (width + space) * i;
object.position.y = (height + space) * j;
// Add object to...
}
}
我想使用 JavaScript.I 平铺对象网格忘记了公式,似乎无法在任何地方找到它:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
for ( j = 0; j < col; j ++ ) {
var object = new object();
object.position.x = 0 + width * j
// do i nee another loop here?
//add object to....
}
这将 return 一行 10 个按宽度间隔的对象,但我还想要像 10x10 网格这样的列,就在 JavaScript..
中的公式之后您可以嵌套两个 for
循环,如下所示:
var width = 113;
var height = 113;
var col = 10;
var row = 10;
var space = ...;
// Rows loop
for ( j = 0; j < row; j ++ ) {
// For each column in the row
for ( i = 0; i < col; i ++ ) {
// j is the row index and i is the column index..
var object = new object();
object.position.x = (width + space) * i;
object.position.y = (height + space) * j;
// Add object to...
}
}