admin 管理员组文章数量: 1086019
I'm attempting to position an object in illustrator to the bottom, left of the art board via javascript. I can access the object with javascript and perform function to it like "rotate"
if ( app.documents.length > 0) {
doc = app.activeDocument;
}
function traceImage(doc) {
doc.pageItems[0].rotate (30);
}
traceImage(doc);
But I'm having trouble finding a simple way to position/align the "pageItems[0]" in the bottom/left of the art board. Is there a simpler approach to this other than calculating it's current distance and then moving it? Thanks.
I'm attempting to position an object in illustrator to the bottom, left of the art board via javascript. I can access the object with javascript and perform function to it like "rotate"
if ( app.documents.length > 0) {
doc = app.activeDocument;
}
function traceImage(doc) {
doc.pageItems[0].rotate (30);
}
traceImage(doc);
But I'm having trouble finding a simple way to position/align the "pageItems[0]" in the bottom/left of the art board. Is there a simpler approach to this other than calculating it's current distance and then moving it? Thanks.
Share Improve this question asked Dec 9, 2013 at 19:26 user3084184user3084184 111 silver badge2 bronze badges2 Answers
Reset to default 6Similarly if you're looking to center the item to the artboard, you'd need to handle the height and width a little differently.
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 pensate 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];
}
There probably an easier way than this, but this works.
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 pensate 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];
}
Essentially, we have to set the top left corner of our pageItem to the bottom left of the artboard. Unfortunately, that would put our pageItem below the Artboard, so we adjust the offset by the height of our pageItem:
本文标签: Align Artwork to Artboard in Illustrator via javascriptStack Overflow
版权声明:本文标题:Align Artwork to Artboard in Illustrator via javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744029702a2521271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论