javascript - 对于循环代码,每个循环在 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));
}
}
解决方案
正如火山所提到的,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
结果:
推荐阅读
- php - 如何重命名已注册的类别?
- python - Pandas fillna 抛出 ValueError:填充值必须在类别中
- jmeter - 如何在 ReportName 中添加时间戳保存 JMeter 报告
- php - 预加载 php 框架并准备数据库连接
- android - Evernote Android_JOB onRunJob() 方法永远不会被调用
- magento2 - 核心模块中的 Magento 从 2.2.6 升级到 2.3.0 问题
- php - 如何将邮件发送到多个地址但按 20-30 分组以避免在 php 中发送垃圾邮件?
- python - 简单的__双下划线作为变量名是什么意思?只是__不跟随另一个字符
- drupal - Drupal 8 多个子域使用相同的数据库和不同的设计
- ckeditor - CKEditor 5 按属性查找模型元素