【问题标题】:Align Artwork to Artboard in Illustrator via javascript通过 javascript 在 Illustrator 中将艺术品与画板对齐
【发布时间】:2021-02-09 05:52:33
【问题描述】:

我正在尝试通过 javascript 将 illustrator 中的对象放置在艺术板的底部左侧。我可以使用 javascript 访问对象并对其执行“旋转”等功能

if ( app.documents.length > 0) {
    doc = app.activeDocument;
}


   function traceImage(doc) {

         doc.pageItems[0].rotate (30);


}

traceImage(doc);

但我很难找到一种简单的方法来定位/对齐艺术板底部/左侧的“pageItems[0]”。除了计算它的当前距离然后移动它之外,还有更简单的方法吗?谢谢。

【问题讨论】:

    标签: javascript adobe-illustrator


    【解决方案1】:

    可能有比这更简单的方法,但这可行。

    if ( app.documents.length > 0) {
        doc = app.activeDocument;
    
         // Get the active Artboard index
        var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
    
        // Get the Height of the Artboard
        var artboardBottom = activeAB.artboardRect[3];
    
        // The page item you want to move. Reference it how you will. This just
        // obviously grabs the first pageItem in the document.
        var myPageItem = doc.pageItems[0];
    
        // Here is where the magic happens. Set the poition of the item.
        // [0,0] would be at the top left, so we have to compensate for the artboard
        // height. We add myPageItem's height for offset, or we'd end up BELOW
        // the artboard.
        myPageItem.position = [0, artboardBottom + myPageItem.height];
    }
    

    本质上,我们必须将 pageItem 的左上角设置为画板的左下角。不幸的是,这会使我们的 pageItem 低于画板,因此我们根据 pageItem 的高度调整偏移:

    【讨论】:

      【解决方案2】:

      同样,如果您希望将项目置于画板的中心,则需要稍微不同地处理高度和宽度。

      if ( app.documents.length > 0) {
          doc = app.activeDocument;
      
          // Get the active Artboard index
          var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];
      
          // Get the right most X point of the Artboard
          // artboardX + artboardWidth
          var artboardRight = activeAB.artboardRect[0] + activeAB.artboardRect[2];
          // Get the bottom most Y point of the Artboard
          // artboardY + artboardHeight
          var artboardBottom = activeAB.artboardRect[1] + activeAB.artboardRect[3];
         
          // The page item you want to move. Reference it how you will. This just
          // obviously grabs the first pageItem in the document.
          var myPageItem = doc.pageItems[0];
      
          // Here is where the magic happens. Set the position of the item.
          // [0,0] would be at the top left, so we have to compensate for the    artboard
          // height and width. We add item's height then split it for the vertical
          // offset, or we'd  end up BELOW the artboard.
          // Then we subtract the item's width and split the difference to center the
          // item horizontally.
          var horziontalCenterPosition = (artboardRight - myPageItem.width)/2;
          var verticalCenterPosition = (artboardBottom + myPageItem.height)/2;
      
          myPageItem.position = [horziontalCenterPosition, verticalCenterPosition];
      }
      

      【讨论】:

      • 这是一种很好的居中方法。这甚至考虑如果画板没有设置为 0,0
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2019-09-24
      相关资源
      最近更新 更多