For 循环代码在每个循环中将 x 轴上的间距加倍

For loop code doubles spacing on x-axis every loop

我正在尝试在 Photoshop 中创建一个图案脚本,在整个 canvas 上水平和垂直复制图像。但问题是,在 x 轴上,它在每个循环中都会将其值加倍。如果我删除 "j" 循环,它工作正常。

这张照片将向您展示我所指的问题https://imgur.com/a/0x9HhCS

        var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.\nDefault is 0px.", "0"));
        for (var i = 0; i < width / (layerWidth + offset); i++) {
            for (var j = 0; j < 3; j++) {
                app.activeDocument.layers[i, j].duplicate()
                app.activeDocument.layers[i, j].translate(i * (layerWidth + offset), j * (layerHeight + offset));
            }
        }

问题与您如何使用offset有关: Translate 指的是层的边界矩形。 如果将 50 像素宽的图像平移 50 像素,则生成的图层宽度将为 100 像素。 每次迭代尝试仅使用 offset

如火山所述,layers[i, j] 不是访问图层的有效方式。我什至不确定为什么会这样。您应该 select 原始图层,复制并翻译它。像这样:

var width = activeDocument.width.as("px");
var height = activeDocument.height.as("px");
var layer = app.activeDocument.activeLayer;
var layerWidth = layer.bounds[2] - layer.bounds[0];
var layerHeight = layer.bounds[3] - layer.bounds[1];
var copy, i, j;

var offset = parseInt(prompt("Type in the offset (spacing between pics) value here.\nDefault is 0px.", "0"));   

for (i = 0; i < width / (layerWidth + offset); i++)
{
    for (j = 0; j < height / (layerHeight + offset); j++)
    {
        // in the each loop we select the original layer, make a copy and offset it to calculated values
        app.activeDocument.activeLayer = layer;
        copy = layer.duplicate();
        copy.translate(i * (layerWidth + offset), j * (layerHeight + offset));
    }
}

layer.remove(); // remove the original layer

结果: