Guest User

Untitled

a guest
Jul 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. // 旋回2
  2. // ・選択パスのアンカーポイントを一定の方法で回転させます。
  3. // ・結果は折れ線になります。元のパス上にアンカーポイントをたくさん追加
  4. //  したうえで使うような感じになると思います。
  5. // ・回転の中心は選択範囲の中心、基準となる半径は選択範囲の幅または高さ
  6. //  のうち大きいほうの半分です。
  7.  
  8. // ・基準半径に掛ける定数を設定できます。(RADIUS_MULTIPLIER)
  9. // ・GET_RECT_FROM_FRONTMOST_PATH を true にすると、回転の中心と半径を
  10. //  最前面のパスから取得します。
  11. // ・パス以外が選択されているとエラーになります。
  12.  
  13. // Adobe Illustrator script
  14. // 2018.07.16
  15.  
  16. var GET_RECT_FROM_FRONTMOST_PATH = false;
  17. var RADIUS_MULTIPLIER = 1.0; // (!=0)
  18.  
  19. // x, y: float
  20. var Point = function(x, y){
  21. this.x = x;
  22. this.y = y;
  23. }
  24. Point.prototype = {
  25. add: function(p){ // p:Point
  26. return new Point(this.x + p.x, this.y + p.y);
  27. },
  28. sub: function(p){ // p:Point
  29. return new Point(this.x - p.x, this.y - p.y);
  30. },
  31. // Point o までの距離の2乗を返す
  32. dist2: function(o){ // o:Point
  33. var dx = this.x - o.x;
  34. var dy = this.y - o.y;
  35. return dx*dx + dy*dy;
  36. },
  37. // Point o までの距離を返す
  38. dist: function(o){ // o:Point
  39. return Math.sqrt(this.dist2(o));
  40. },
  41. // Point o を中心に角度 t だけ回転する
  42. rot: function(o, t){ // o:Point, t:float (radian)
  43. var s = Math.sin(t);
  44. var c = Math.cos(t);
  45. var p = this.sub(o);
  46. return new Point(p.x * c - p.y * s,
  47. p.x * s + p.y * c).add(o);
  48. },
  49. toArray: function(){
  50. return [ this.x, this.y ];
  51. }
  52. }
  53.  
  54. // pp の位置を p に設定する
  55. // pp: PathPoint
  56. // p: Point
  57. function setPathPoint(pp, p){
  58. pp.anchor = p.toArray();
  59. pp.leftDirection = pp.anchor;
  60. pp.rightDirection = pp.anchor;
  61. }
  62.  
  63. // item の各 PathPoint の位置を、center を中心に回転させる処理。
  64. // item: PathItem
  65. // radius: float
  66. // center: Point
  67. // returns: err : エラー発生時 true
  68. function rotatePoints(item, radius, center){
  69. var err = false;
  70. var pp = item.pathPoints;
  71. var o = new Point(0,0);
  72. for(var i = 0; i < pp.length; i++){
  73. var anc = pp[i].anchor;
  74. var p = new Point(anc[0], anc[1]);
  75. var t = p.dist(center)/(2*radius);
  76. setPathPoint(pp[i], p.rot(center, t));
  77. }
  78. return err;
  79. }
  80.  
  81. // sels の各PateItemを囲む矩形の情報を返す
  82. // sels: PageItems[]
  83. function findRect(sels){
  84. var gb = sels[0].geometricBounds; // left, top, right, bottom
  85. var rect = { left:gb[0], top:gb[1], right:gb[2], bottom:gb[3],
  86. center: undefined };
  87.  
  88. if(!GET_RECT_FROM_FRONTMOST_PATH){
  89. for(var i = 1; i < sels.length; i++){
  90. gb = sels[i].geometricBounds;
  91. if(gb[0] < rect.left) rect.left = gb[0];
  92. if(gb[2] > rect.right) rect.right = gb[2];
  93. if(gb[1] > rect.top) rect.top = gb[1];
  94. if(gb[3] < rect.bottom) rect.bottom = gb[3];
  95. }
  96. }
  97. rect.center = new Point((rect.left + rect.right)/2,
  98. (rect.top + rect.bottom)/2);
  99. return rect;
  100. }
  101.  
  102. function main(){
  103. var err = false;
  104. var sels = activeDocument.selection;
  105. if(sels.length < 1) return;
  106.  
  107. var rect = findRect(sels);
  108. var real_radius = Math.max(rect.right - rect.left,
  109. rect.top - rect.bottom) / 2;
  110.  
  111. var radius = real_radius * RADIUS_MULTIPLIER;
  112. if(radius == 0){
  113. alert("半径が不適切です");
  114. return;
  115. }
  116.  
  117. for(var i = 0; i < sels.length; i++){
  118. err = rotatePoints(sels[i], radius, rect.center);
  119. if(err) break;
  120. }
  121. }
  122. main();
Add Comment
Please, Sign In to add comment