Skip to content

Instantly share code, notes, and snippets.

@shspage
Created July 16, 2018 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shspage/ca7b54a3e0b5f115b9e9e847610c890a to your computer and use it in GitHub Desktop.
Save shspage/ca7b54a3e0b5f115b9e9e847610c890a to your computer and use it in GitHub Desktop.
Adobe Illustrator script
// 旋回1
// ・選択パスのアンカーポイントを一定の方法で回転させます。
// ・結果は折れ線になります。元のパス上にアンカーポイントをたくさん追加
//  したうえで使うような感じになると思います。
// ・回転の中心は選択範囲の中心、基準となる半径は選択範囲の幅または高さ
//  のうち大きいほうの半分です。
// ・基準半径に掛ける定数を設定できます。(RADIUS_MULTIPLIER)
// ・GET_RECT_FROM_FRONTMOST_PATH を true にすると、回転の中心と半径を
//  最前面のパスから取得します。
// ・パス以外が選択されているとエラーになります。
// Adobe Illustrator script
// 2018.07.16
var GET_RECT_FROM_FRONTMOST_PATH = false;
var RADIUS_MULTIPLIER = 1.0; // !=0
// x, y: float
var Point = function(x, y){
this.x = x;
this.y = y;
}
Point.prototype = {
add: function(p){ // p:Point
return new Point(this.x + p.x, this.y + p.y);
},
sub: function(p){ // p:Point
return new Point(this.x - p.x, this.y - p.y);
},
mul: function(m){ // m:float
return new Point(this.x * m, this.y * m);
},
// Point o までの距離の2乗を返す
dist2: function(o){ // o:Point
var dx = this.x - o.x;
var dy = this.y - o.y;
return dx*dx + dy*dy;
},
// Point o までの距離を返す
dist: function(o){ // o:Point
return Math.sqrt(this.dist2(o));
},
// Point o を中心に角度 t だけ回転する
rot: function(o, t){ // o:Point, t:float (radian)
var s = Math.sin(t);
var c = Math.cos(t);
var p = this.sub(o);
return new Point(p.x * c - p.y * s,
p.x * s + p.y * c).add(o);
},
toArray: function(){
return [ this.x, this.y ];
}
}
// pp の位置を p に設定する
// pp: PathPoint
// p: Point
function setPathPoint(pp, p){
pp.anchor = p.toArray();
pp.leftDirection = pp.anchor;
pp.rightDirection = pp.anchor;
}
// item の各 PathPoint の位置を、center を中心に回転させる処理。
// item: PathItem
// radius: float
// center: Point
// returns: err : エラー発生時 true
function rotatePoints(item, radius, center){
var err = false;
var pp = item.pathPoints;
var o = new Point(0,0);
for(var i = 0; i < pp.length; i++){
var anc = pp[i].anchor;
var p = new Point(anc[0], anc[1]);
var t = p.dist(center)/(2 * radius);
setPathPoint(pp[i], p.rot(center, t));
}
return err;
}
// sels の各PateItemを囲む矩形の情報を返す
// sels: PageItems[]
function findRect(sels){
var gb = sels[0].geometricBounds; // left, top, right, bottom
var rect = { left:gb[0], top:gb[1], right:gb[2], bottom:gb[3],
center: undefined };
if(!GET_RECT_FROM_FRONTMOST_PATH){
for(var i = 1; i < sels.length; i++){
gb = sels[i].geometricBounds;
if(gb[0] < rect.left) rect.left = gb[0];
if(gb[2] > rect.right) rect.right = gb[2];
if(gb[1] > rect.top) rect.top = gb[1];
if(gb[3] < rect.bottom) rect.bottom = gb[3];
}
}
rect.center = new Point((rect.left + rect.right)/2,
(rect.top + rect.bottom)/2);
return rect;
}
function main(){
var err = false;
var sels = activeDocument.selection;
if(sels.length < 1) return;
var rect = findRect(sels);
var real_radius = Math.max(rect.right - rect.left,
rect.top - rect.bottom) / 2;
var radius = real_radius * RADIUS_MULTIPLIER;
if(radius == 0){
alert("半径が不適切です");
return;
}
for(var i = 0; i < sels.length; i++){
err = rotatePoints(sels[i], radius, rect.center);
if(err) break;
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment