-
Notifications
You must be signed in to change notification settings - Fork 4
/
alignment_functions.js
176 lines (141 loc) · 4.64 KB
/
alignment_functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//author: William Dowling
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this library: https://community.adobe.com/t5/illustrator/library-for-aligning-objects-via-script/m-p/11925954#M269613
//*******//
//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3
//*******//
//Alignment Functions
//
//given a key object and an an array of path items
//align all objects to the key object
//function also accounts for any invisible "clipped" artwork which normally
//messes up positioning because the invisible art is included in the bounds of the art.
//arguments
//key : any pageItem or artboard object
//otherObjects: Array of page items to align
//Dependencies
//
//object_bounds_data.js : https://github.com/wdjsdev/public_illustrator_scripts/blob/master/object_bounds_data.js
#include "object_bounds_data.js";
Array.prototype.forEach = function ( callback, startPos, inc )
{
inc = inc || 1;
startPos = startPos || 0;
for ( var i = startPos; i < this.length; i += inc )
callback( this[ i ], i, this );
};
//customize these "alignType" arguments to your hearts desire
//whatever tickles your brain just right.
function align ( key, otherObjects, alignType )
{
switch ( alignType )
{
//center horizontally and vertically
case "center":
doTheAligning( key, otherObjects, "vc", "hc" );
break;
//center vertically by centerpoints
case "vcenter":
doTheAligning( key, otherObjects, "vc", undefined );
break;
//align top of objects to top of key
case "vtop":
doTheAligning( key, otherObjects, "t", undefined );
break;
//align bottom of objects to bottom of key
case "vbottom":
doTheAligning( key, otherObjects, "b", undefined );
break;
//center horizontally by centerpoints
case "hcenter":
doTheAligning( key, otherObjects, undefined, "hc" );
break;
//align left of objects to left of key
case "hleft":
doTheAligning( key, otherObjects, undefined, "l" );
break;
//align right of objects to right of key
case "hright":
doTheAligning( key, otherObjects, undefined, "r" );
break;
//align bottom left of objects bottom left of key
case "botleft":
doTheAligning( key, otherObjects, "b", "l" );
break;
//align bottom right of objects bottom right of key
case "botright":
doTheAligning( key, otherObjects, "b", "r" );
break;
//align top left of objects top left of key
case "topleft":
doTheAligning( key, otherObjects, "t", "l" );
break;
//align top right of objects top right of key
case "topright":
doTheAligning( key, otherObjects, "t", "r" );
break;
//align left of objects to left of key, and vertical center of object to verical center of key
case "leftcenter":
doTheAligning( key, otherObjects, "vc", "l" );
break;
//align right of objects to right of key, and vertical center of object to verical center of key
case "rightcenter":
doTheAligning( key, otherObjects, "vc", "r" );
break;
//align top of objects to top of key, and horizontal center of object to horizontal center of key
case "topcenter":
doTheAligning( key, otherObjects, "t", "hc" );
break;
//align bottom of objects to bottom of key, and horizontal center of object to horizontal center of key
case "botcenter":
doTheAligning( key, otherObjects, "b", "hc" );
break;
default:
$.writeln( "Error: Invalid alignType argument.\nValid arguments are: center, vcenter, vtop, vbottom, hcenter, hleft, hright, botleft, botright, topleft, topright, leftcenter, rightcenter, topcenter, botcenter" );
}
}
function doTheAligning ( key, otherObjects, va, ha )
{
var destBounds = getBoundsData( key );
if ( !destBounds )
{
$.writeln( "Error: No bounds data found for key object" );
return undefined;
}
otherObjects.forEach( function ( item )
{
var itemBounds = getBoundsData( item );
if ( !itemBounds.w ) return;
if ( va ) //vertical alignment
{
switch ( va )
{
case "vc":
item.top = destBounds.vc + itemBounds.hh; //vertical center point of key + half height of item
break;
case "t":
item.top = destBounds.t; //top of key
break;
case "b":
item.top = destBounds.b + itemBounds.h; //bottom of key + height of item
}
}
if ( ha ) //horizontal alignment
{
switch ( ha )
{
case "hc":
item.left = destBounds.hc - itemBounds.hw; //horizontal center point of key - half width of item
break;
case "l":
item.left = destBounds.l; //left of key
break;
case "r":
item.left = destBounds.r - itemBounds.w; //right of key - width of item
}
}
} );
}