Skip to main content
Participating Frequently
September 7, 2022
Question

how to get visible position with Clipped is true

  • September 7, 2022
  • 2 replies
  • 450 views

🙂

when Clipped is true, get position  contain hidden path;

This topic has been closed for replies.

2 replies

jduncan
Community Expert
Community Expert
September 27, 2022

Not sure exactly what you are asking, but if you are looking to get the visible bounds of a clipped object then here's a function I worked on with @Disposition_Dev to go through every object inside of a clipping mask or compound path to determine the bounds of what is actually visible.

 

/**
 * figure out the actual "visible" bounds for an object
 * if clipping mask or compound path items are found
 */
function getVisibleBounds(object) {
  var bounds, clippedItem, sandboxItem, sandboxLayer;
  var curItem;
  if (object.typename == "GroupItem") {
    // if the object is clipped
    if (object.clipped) {
      // check all sub objects to find the clipping path
      for (var i = 0; i < object.pageItems.length; i++) {
        curItem = object.pageItems[i];
        if (curItem.clipping) {
          clippedItem = curItem;
          break;
        } else if (curItem.typename == "CompoundPathItem") {
          if (!curItem.pathItems.length) {
            // catch compound path items with no pathItems via william dowling @ github.com/wdjsdev
            sandboxLayer = app.activeDocument.layers.add();
            sandboxItem = curItem.duplicate(sandboxLayer);
            app.activeDocument.selection = null;
            sandboxItem.selected = true;
            app.executeMenuCommand("noCompoundPath");
            sandboxLayer.hasSelectedArtwork = true;
            app.executeMenuCommand("group");
            clippedItem = app.activeDocument.selection[0];
            break;
          } else if (curItem.pathItems[0].clipping) {
            clippedItem = curItem;
            break;
          }
        } else {
          clippedItem = curItem;
          break;
        }
      }
      bounds = clippedItem.geometricBounds;
      if (sandboxLayer) {
        // eliminate the sandbox layer since it's no longer needed
        sandboxLayer.remove();
        sandboxLayer = undefined;
      }
    } else {
      // if the object is not clipped
      var subObjectBounds;
      var allBoundPoints = [[], [], [], []];
      // get the bounds of every object in the group
      for (var i = 0; i < object.pageItems.length; i++) {
        curItem = object.pageItems[i];
        subObjectBounds = getVisibleBounds(curItem);
        allBoundPoints[0].push(subObjectBounds[0]);
        allBoundPoints[1].push(subObjectBounds[1]);
        allBoundPoints[2].push(subObjectBounds[2]);
        allBoundPoints[3].push(subObjectBounds[3]);
      }
      // determine the groups bounds from it sub object bound points
      bounds = [
        Math.min.apply(Math, allBoundPoints[0]),
        Math.max.apply(Math, allBoundPoints[1]),
        Math.max.apply(Math, allBoundPoints[2]),
        Math.min.apply(Math, allBoundPoints[3]),
      ];
    }
  } else {
    bounds = object.geometricBounds;
  }
  return bounds;
}

 

Disposition_Dev
Legend
September 27, 2022

to piggyback on this. getVisibleBounds will give you the actual visible bounds. If you then want to get the amount of art that is clipped, you can simply do the calculations to compare the standard bounds from illustrator to the calculated visibleBounds. like this:

 

var myItem = //some clip group

var trueBounds = myItem.visibleBounds;

var visBounds = getVisibleBounds(myItem);

 

var leftSideClippedArtDimension = visBounds[0] - trueBounds[0];

etc...

Omar.Fathy
Community Expert
Community Expert
September 27, 2022

Could you explain more or provide screenshots for what you need to do?