Search code examples
javascriptgeometryphotoshopextendscriptphotoshop-script

Photoshop scripting circular selection on new layer


I'm trying to dynamically add layers in a Photoshop script via JavaScript. The layers have a circular selection, not rectangular or triangular.

For example rectangular:

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
);

I want to do the same now with a circular selection, maybe with a function like this? I'm stuck how to define the function:

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

And return it in a selection which I can then place on a new layer:

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

The docs don't have an answer and this answer I find difficult for my use case.


Solution

  • You can utilize the following custom makeCircleSelection function. As you can see its signature has three parameters, namely x, y and 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;
    }
    

    Then you can do the following to meet your requirement:

    // Example usage
    
    var document = app.activeDocument;
    
    var circleSelection = makeCircleSelection(200, 100, 100);
    circleSelection.copy();
    
    document.artLayers.add();
    document.paste();
    

    Note: You'll need to ensure that pixels exist within the circular selection on the active art layer that you copy from. The area cannot be transparent.