-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaball.jsx
311 lines (260 loc) · 8.93 KB
/
Metaball.jsx
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Metaball
// This script may help to create Metaball-like shapes
// USAGE : Draw some circles and select them, then run this script.
// Adjust the values in the dialog. Then click OK.
// (it doesn't check in the script whether each path is really a circle)
// # Combining the shapes using Pathfinder may results several overlapping
// anchor points on the path. if it occurs, it may help to solve it to
// use my another script "Merge Overlapped Anchors.js".
// This is a simple sample script that merges overlapping anchors on the path.
// test env: Adobe Illustrator CC (Win/Mac)
// Copyright(c) 2004-2015 Hiroyuki Sato
// https://github.com/shspage
// This script is distributed under the MIT License.
// See the LICENSE file for details.
// Tue, 07 Jul 2015 20:08:48 +0900
main();
function main(){
var conf = {
rate : "50.0",
maxSliderValue : 100
}
var paths = [];
getPathItemsInSelection(1, paths);
if(paths.length < 2) return;
activateEditableLayer(paths[0]);
var preview_paths = [];
var previewed = false;
var clearPreview = function(){
if( previewed ){
try{
undo();
redraw();
} catch(e){
alert(e);
} finally {
preview_paths = [];
previewed = false;
}
}
}
var drawPreview = function(){
if(conf.rate > 0){
try{
var v = conf.rate / 100;
var pitem, j;
for(var i = paths.length - 1; i >= 1; i--){
for(j = i - 1; j >= 0; j--){
pitem = metaball(paths[i], paths[j], v);
if(pitem != null) preview_paths.push( pitem );
}
}
} finally {
previewed = true;
}
}
}
// show a dialog
var win = new Window("dialog", "Metaball" );
win.alignChildren = "fill";
win.rateSliderPanel = win.add("panel", undefined, "rate");
win.rateSliderPanel.orientation = "row";
win.rateSliderPanel.alignChildren = "fill";
win.rateSliderPanel.rateSlider = win.rateSliderPanel.add("slider", undefined, conf.rate, 0, conf.maxSliderValue);
win.rateSliderPanel.txtBox = win.rateSliderPanel.add("edittext", undefined, conf.rate);
win.rateSliderPanel.txtBox.justify = "right";
win.rateSliderPanel.txtBox.characters = 5;
win.rateSliderPanel.txtBox.helpTip = "hit TAB to set the input value temporarily";
win.chkGroup = win.add("group");
win.chkGroup.alignment = "center";
win.chkGroup.previewChk = win.chkGroup.add("checkbox", undefined, "preview");
win.btnGroup = win.add("group", undefined );
win.btnGroup.alignment = "center";
win.btnGroup.okBtn = win.btnGroup.add("button", undefined, "OK");
win.btnGroup.cancelBtn = win.btnGroup.add("button", undefined, "Cancel");
var getValues = function(){
conf.rate = win.rateSliderPanel.txtBox.text; //.text;
}
var processPreview = function( is_preview ){
if( ! is_preview || win.chkGroup.previewChk.value){
win.enabled = false;
getValues();
clearPreview();
drawPreview();
if( is_preview ) redraw();
win.enabled = true;
}
}
win.rateSliderPanel.txtBox.onChange = function(){
var v = parseFloat(this.text);
if(isNaN(v)){
v = conf.slider_defaultvalue;
} else if(v < 0){
v = 0;
} else if(v > conf.maxSliderValue){
v = conf.maxSliderValue;
}
this.text = v;
win.rateSliderPanel.rateSlider.value = v;
processPreview( true );
}
win.rateSliderPanel.rateSlider.onChanging = function(){
win.rateSliderPanel.txtBox.text = this.value.toFixed( 1 );
}
win.rateSliderPanel.rateSlider.onChange = function(){
win.rateSliderPanel.txtBox.text = this.value.toFixed( 1 );
processPreview( true );
}
win.chkGroup.previewChk.onClick = function(){
if( this.value ){
processPreview( true );
} else {
if( previewed ){
clearPreview();
redraw();
}
}
}
win.btnGroup.okBtn.onClick = function(){
processPreview( false );
win.close();
}
win.btnGroup.cancelBtn.onClick = function(){
win.enabled = false;
clearPreview();
win.enabled = true;
win.close();
}
win.show();
if(previewed) app.activeDocument.selection = paths.concat( preview_paths );
}
// ---------------------------------------------
function metaball(s0, s1, v){
var arr = getGBCenterWidth(s0);
o1 = arr[0]; // o:center, r:radius
r1 = arr[1] / 2;
arr = getGBCenterWidth(s1);
o2 = arr[0];
r2 = arr[1] / 2;
if(r1 == 0 || r2 == 0) return;
var pi2 = Math.PI / 2;
var d = dist(o1, o2);
var u1, u2;
if(d <= Math.abs(r1 - r2)){
return;
} else if(d < r1 + r2){ // case circles are overlapping
u1 = Math.acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d));
u2 = Math.acos((r2 * r2 + d * d - r1 * r1) / (2 * r2 * d));
} else {
u1 = 0;
u2 = 0;
}
var t1 = getRad(o1, o2);
var t2 = Math.acos((r1 - r2) / d);
var t1a = t1 + u1 + (t2 - u1) * v;
var t1b = t1 - u1 - (t2 - u1) * v;
var t2a = t1 + Math.PI - u2 - (Math.PI - u2 - t2) * v;
var t2b = t1 - Math.PI + u2 + (Math.PI - u2 - t2) * v;
var p1a = setPnt(o1, t1a, r1);
var p1b = setPnt(o1, t1b, r1);
var p2a = setPnt(o2, t2a, r2);
var p2b = setPnt(o2, t2b, r2);
// define handle length by the distance between both ends of the curve to draw
var handle_len_rate = 2;
var d2 = Math.min(v * handle_len_rate, dist(p1a, p2a) / (r1 + r2));
d2 *= Math.min(1, d * 2 / (r1 + r2)); // case circles are overlapping
r1 *= d2;
r2 *= d2;
var pitem = s0.duplicate();
with(pitem){
var pt = pathPoints;
while( pt.length < 4 ) pt.add();
while( pt.length > 4 ) pt[ pt.length - 1 ].remove();
with(pt[0]){
anchor = p1a;
leftDirection = anchor;
rightDirection = setPnt(p1a, t1a - pi2, r1);
pointType = PointType.CORNER;
}
with(pt[1]){
anchor = p2a;
rightDirection = anchor;
leftDirection = setPnt(p2a, t2a + pi2, r2);
pointType = PointType.CORNER;
}
with(pt[2]){
anchor = p2b;
leftDirection = anchor;
rightDirection = setPnt(p2b, t2b - pi2, r2);
pointType = PointType.CORNER;
}
with(pt[3]){
anchor = p1b;
rightDirection = anchor;
leftDirection = setPnt(p1b, t1b + pi2, r1);
pointType = PointType.CORNER;
}
closed = true;
}
return pitem;
}
// ------------------------------------------------
function getGBCenterWidth(pi){
var gb = pi.geometricBounds; // left, top, right, bottom
var w = gb[2] - gb[0];
var h = gb[1] - gb[3];
return [[gb[0] + w / 2, gb[3] + h / 2], w];
}
// ------------------------------------------------
function setPnt(pnt, rad, dis){
return [pnt[0] + Math.cos(rad) * dis,
pnt[1] + Math.sin(rad) * dis];
}
// ------------------------------------------------
function dist(p1, p2) {
return Math.sqrt(Math.pow(p1[0] - p2[0], 2)
+ Math.pow(p1[1] - p2[1], 2));
}
// ------------------------------------------------
function getRad(p1,p2) {
return Math.atan2(p2[1] - p1[1],
p2[0] - p1[0]);
}
// ----------------------------------------------
function activateEditableLayer(pi){
var lay = activeDocument.activeLayer;
if(lay.locked || ! lay.visible) activeDocument.activeLayer = pi.layer;
}
// ------------------------------------------------
// extract PathItems from the selection which length of PathPoints
// is greater than "n"
function getPathItemsInSelection(n, paths){
if(documents.length < 1) return;
var s = activeDocument.selection;
if (!(s instanceof Array) || s.length < 1) return;
extractPaths(s, n, paths);
}
// --------------------------------------
// extract PathItems from "s" (Array of PageItems -- ex. selection),
// and put them into an Array "paths". If "pp_length_limit" is specified,
// this function extracts PathItems which PathPoints length is greater
// than this number.
function extractPaths(s, pp_length_limit, paths){
for(var i = 0; i < s.length; i++){
if(s[i].typename == "PathItem"
&& !s[i].guides && !s[i].clipping){
if(pp_length_limit
&& s[i].pathPoints.length <= pp_length_limit){
continue;
}
paths.push(s[i]);
} else if(s[i].typename == "GroupItem"){
// search for PathItems in GroupItem, recursively
extractPaths(s[i].pageItems, pp_length_limit, paths);
} else if(s[i].typename == "CompoundPathItem"){
// searches for pathitems in CompoundPathItem, recursively
// ( ### Grouped PathItems in CompoundPathItem are ignored ### )
extractPaths(s[i].pathItems, pp_length_limit , paths);
}
}
}