首页 > 解决方案 > 对于循环代码,每个循环在 x 轴上的间距加倍

问题描述

我正在尝试在 Photoshop 中创建一个模式脚本,该脚本在整个画布上水平和垂直复制图像。但问题是,在 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));
            }
        }

标签: javascriptjsxphotoshop-script

解决方案


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

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

结果:

在此处输入图像描述


推荐阅读