Ausgabe
Ich versuche, Ebenen in einem Photoshop-Skript dynamisch über JavaScript hinzuzufügen. Die Ebenen haben eine kreisförmige Auswahl, nicht rechteckig oder dreieckig.
Zum Beispiel rechteckig:
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
);
Ich möchte das gleiche jetzt mit einer kreisförmigen Auswahl machen, vielleicht mit einer Funktion wie dieser? Ich stecke fest, wie ich die Funktion definieren soll:
var circleSelection = makeCircleSelection(x,y,radius);
Und geben Sie es in einer Auswahl zurück, die ich dann auf einer neuen Ebene platzieren kann:
var layer = document.artLayers.add();
document.selection.select(circleSelection);
document.selection.copy();
document.paste();
Die Dokumentation hat keine Antwort und diese Antwort finde ich für meinen Anwendungsfall schwierig.
Lösung
Sie können die folgende benutzerdefinierte makeCircleSelection
Funktion verwenden. Wie Sie sehen können, hat seine Signatur drei Parameter, nämlich x
, y
und radius
.
/**
* 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;
}
Dann können Sie Folgendes tun, um Ihre Anforderung zu erfüllen:
// Example usage
var document = app.activeDocument;
var circleSelection = makeCircleSelection(200, 100, 100);
circleSelection.copy();
document.artLayers.add();
document.paste();
Hinweis: Sie müssen sicherstellen, dass Pixel innerhalb der kreisförmigen Auswahl auf der aktiven Grafikebene vorhanden sind, von der Sie kopieren. Der Bereich darf nicht transparent sein.
Beantwortet von – RobC
Antwort geprüft von – Robin (FixError Admin)