-
Notifications
You must be signed in to change notification settings - Fork 63
/
noiseRotate.jsx
266 lines (235 loc) · 8.44 KB
/
noiseRotate.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
//@include "lib/perlin-noise-simplex.js"
// This script requires "perlin-noise-simplex.js" by Sean McCullough.
// https://gist.github.com/banksean/304522
// The declaration on the first line assumes "perlin-noise-simplex.js" is
// placed under "lib" folder under "Scripts" folder of Adobe Illlustrator.
// ----
// noiseRotate
// changes the rotation of the selected items using Simplex noise.
// USAGE: Select items and run this script.
// NOTE: compound paths are not released
// JavaScript Script for Adobe Illustrator
// Tested with Adobe Illustrator CC2018, Windows 10 (Japanese version).
// Copyright(c) 2018 Hiroyuki Sato
// https://github.com/shspage
// This script is distributed under the MIT License.
// See the LICENSE file for details.
(function(){
var _perlin;
var _n;
var _rangeHalf;
var _opts = {};
var _items = [];
var _rect;
var _fullLength;
// rotate options (by my preference)
var _rotateOpt = {
changePositions:true,
changeFillPatterns:true,
changeFillGradients:true,
changeStrokePattern:true,
rotateAbout:Transformation.CENTER
};
var main = function(){
var script_name = "noiseRotate";
if(documents.length < 1) return;
if(activeDocument.selection.length < 1){
return;
}
// dialog
var previewed = false;
var clearPreview = function(){
if( previewed ){
try{
undo();
} catch(e){
alert(e);
} finally {
previewed = false;
}
}
}
var drawPreview = function(){
var ok = getValues();
if(ok){
try{
noiseMain();
} finally {
previewed = true;
}
}
}
var win = new Window("dialog", script_name);
win.alignChildren = "right";
// default values ---- edit as you prefer
var et_range_max = addEditText(win, "rotate max(degree):", "45");
var et_range_min = addEditText(win, "rotate min(degree):", "-45");
var et_noise_factor = addEditText(win, "noise factor", "3");
var chkGrp = win.add("group");
chkGrp.alignment = "center";
var previewChk = chkGrp.add("checkbox", undefined, "preview");
var extractGroupChk = chkGrp.add("checkbox", undefined, "extract group");
extractGroupChk.value = true;
// buttons
var btnGrp = win.add("group");
var btn_ok = btnGrp.add("button", undefined, "OK");
var btn_cancel = btnGrp.add("button", undefined, "Cancel");
var getValues = function(){
_opts.range_max = parseFloat(et_range_max.text);
_opts.range_min = parseFloat(et_range_min.text);
_opts.noise_factor = parseFloat(et_noise_factor.text);
var ok = verifyOptsValues( _opts );
return ok;
}
var processPreview = function( is_preview ){
_items = [];
if(extractGroupChk.value){
extractPageItems(activeDocument.selection, _items);
} else {
_items = activeDocument.selection;
}
if(_items.length < 1){
alert("ABORT:\rnothing selected");
return;
}
_rect = getSelectedRect(_items);
_fullLength = Math.max(_rect.width, _rect.height);
if( ! is_preview || previewChk.value){
try{
win.enabled = false;
clearPreview();
drawPreview();
if( is_preview ) redraw();
} catch(e){
alert(e);
} finally{
win.enabled = true;
}
}
}
previewChk.onClick = function(){
if( this.value ){
processPreview( true );
} else {
if( previewed ){
clearPreview();
redraw();
}
}
}
btn_ok.onClick = function(){
if(!previewed) processPreview( false );
win.close();
}
btn_cancel.onClick = function(){
try{
win.enabled = false;
clearPreview();
} catch(e){
alert(e);
} finally{
win.enabled = true;
}
win.close();
}
win.show();
};
// -----------------------------------------------
var verifyOptsValues = function(_opts){
var ok = true;
var errmsg = "";
if(isNaN(_opts.range_max)){ // || _opts.range_max <= 0){
errmsg = "max value is invalid";
} else if( isNaN(_opts.range_min)){ // || _opts.range_min <= 0){
errmsg = "min value is invalid";
} else if( isNaN(_opts.noise_factor) || _opts.noise_factor < 0){
errmsg = "noise factor value is invalid";
}
if(errmsg != ""){
alert(errmsg);
ok = false;
}
return ok;
};
// -----------------------------------------------
// ScriptUI utility
var addEditText = function(win, label, defaultvalue){
var gr = win.add("group");
gr.add("statictext", undefined, label);
var et = gr.add("edittext", undefined, defaultvalue);
et.characters = 6;
return et;
};
// -----------------------------------------------
var noiseMain = function(){
_perlin = new SimplexNoise();
_n = Math.random() * 10;
_rangeHalf = (_opts.range_max - _opts.range_min) / 2;
for(var i = 0; i < _items.length; i++){
var v = getNoiseValue(_items[i]);
_items[i].rotate(v,
_rotateOpt.changePositions,
_rotateOpt.changeFillPatterns,
_rotateOpt.changeFillGradients,
_rotateOpt.changeStrokePattern,
_rotateOpt.rotateAbout
);
}
};
// -----------------------------------------------
var Point = function(x_, y_){
this.x = x_;
this.y = y_;
};
// -----------------------------------------------
var Rect = function(l, r, t, b){
this.left = l;
this.right = r;
this.top = t;
this.bottom = b;
this.width = r - l;
this.height = t - b;
};
// -----------------------------------------------
// generates noise value according to the coordinate of "item".
// returns "noiseValue": (_opts.range_min <= noiseValue <= _opts.range.max)
var getNoiseValue = function(item){ // item : PageItem
var center = getCenter(item);
var kx = (center.x - _rect.left) / _fullLength * _opts.noise_factor;
var ky = (center.y - _rect.bottom) / _fullLength * _opts.noise_factor;
var noiseValue = _perlin.noise(_n + kx, _n + ky) + 1; // 0 < . < 2
return noiseValue * _rangeHalf + _opts.range_min;
};
// -----------------------------------------------
var getCenter = function(item){
return new Point(item.left + item.width / 2,
item.top - item.height / 2);
};
// -----------------------------------------------
var getSelectedRect = function(sel){
var left = sel[0].left;
var right = left + sel[0].width;
var top = sel[0].top;
var bottom = sel[0].top - sel[0].height;
for(var i = 1; i < sel.length; i++){
var s = sel[i];
left = Math.min(left, s.left);
right = Math.max(right, s.left + s.width);
top = Math.max(top, s.top);
bottom = Math.min(bottom, s.top - s.height);
}
return new Rect(left, right, top, bottom);
};
// -----------------------------------------------
var extractPageItems = function(s, items){
for(var i = 0; i < s.length; i++){
if(s[i].typename == "GroupItem"){
extractPageItems(s[i].pageItems, items);
} else {
items.push(s[i]);
}
}
};
// -----------------------------------------------
main();
})();