我试图通过JavaScript在Photoshop脚本中动态添加层.层具有圆形 Select ,而不是矩形或三角形.

例如矩形:

var RectangleSelection = Array(
   Array(x, y), // start position
   Array(x + width, y), // right top
   Array(x + width, y + height), // right bottom            
   Array(x, y + height), // left bottom
   Array(x, y) // left top
);

我现在想用循环 Select 做同样的事情,也许用这样的函数?我一直在思考如何定义函数:

var circleSelection = makeCircleSelection(x,y,radius); 

并将其返回到一个 Select 中,然后我可以将其放置在一个新层上:

var layer = document.artLayers.add();
document.selection.select(circleSelection);
document.selection.copy();
document.paste();

文档没有答案,我发现我的用例很难回答.

非常感谢你!

推荐答案

您可以使用以下自定义makeCircleSelection函数.如您所见,其签名有三个参数,即xyradius.

/**
 * Make a circular selection.
 * @param {Number} x The center of the circular selection on the x-axis.
 * @param {Number} y The center of the circular selection on the y-axis.
 * @param {Number} radius The radius of the circular selection.
 * @returns {Object} A reference to the newly created circular selection.
 */
function makeCircleSelection(x, y, radius) {
  var d1 = new ActionDescriptor();
  var d2 = new ActionDescriptor();
  var ref = new ActionReference();

  d1.putUnitDouble(charIDToTypeID('Top '), charIDToTypeID('#Pxl'), y - radius);
  d1.putUnitDouble(charIDToTypeID('Left'), charIDToTypeID('#Pxl'), x - radius);
  d1.putUnitDouble(charIDToTypeID('Btom'), charIDToTypeID('#Pxl'), y + radius);
  d1.putUnitDouble(charIDToTypeID('Rght'), charIDToTypeID('#Pxl'), x + radius);

  ref.putProperty(charIDToTypeID('Chnl'), charIDToTypeID('fsel'));

  d2.putReference(charIDToTypeID('null'), ref);
  d2.putObject(charIDToTypeID('T   '), charIDToTypeID('Elps'), d1);
  d2.putUnitDouble(charIDToTypeID('Fthr'), charIDToTypeID('#Pxl'), 0);
  d2.putBoolean(charIDToTypeID('AntA'), true);

  executeAction(charIDToTypeID('setd'), d2, DialogModes.NO);
  return document.selection;
}

然后,您可以执行以下操作以满足您的要求:

// Example usage

var document = app.activeDocument;

var circleSelection = makeCircleSelection(200, 100, 100);
circleSelection.copy();

document.artLayers.add();
document.paste();

Note:您需要确保像素存在于从中复制的活动艺术层上的圆形 Select 中.该区域不能透明.

Javascript相关问答推荐

使用useup时,React-Redux无法找到Redux上下文值

当在select中 Select 选项时,我必须禁用不匹配的 Select

React 17与React 18中的不同setState行为

使用JavaScript重新排序行

当点击注册页面上的注册按钮时,邮箱重复

如何添加绘图条形图图例单击角形事件

如何从隐藏/显示中删除其中一个点击?

如何根据当前打开的BottomTab Screeb动态加载React组件?

如何解决useState错误—setSelect Image不是函数''

我怎么在JS里连续加2个骰子的和呢?

Next.js服务器端组件请求,如何发送我的cookie token?

如何在Node.js中排除导出的JS文件

如何使用JS创建一个明暗功能按钮?

当我在Reaction中创建一个输入列表时,我的输入行为异常

面对代码中的错误作为前端与后端的集成

与svg相反;S getPointAtLength(D)-我想要getLengthAtPoint(x,y)

Google脚本数组映射函数横向输出

如何使用<;Link>;执行与JS Reaction中的";window.Location=/GameOverDied;";类似的操作?

我的NavLink活动类在REACT-ROUTER-V6中出现问题

如何阻止外部脚本进入顶层导航