-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayObjects.js
executable file
·257 lines (219 loc) · 7.15 KB
/
ArrayObjects.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
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
/* ===============================================================================================================================================
ArrayObjects.js
Description
This collection of scripts reproduces the p5.js examples with Adobe Illustrator's ExtendScript.
However, since it is not interesting just to reproduce them, some of them are modified.
p5.js
https://p5js.org/examples/arrays-array-objects.html
Usage
run this script from File > Scripts > Other Script...
Notes
In rare cases, you may not be able to create it.
In that case, restart Illustrator and run this script again.
Requirements
Illustrator CS6 or higher
Version
1.0.0
Homepage
github.com/sky-chaser-high/generative-art-with-illustrator
License
Released under the MIT license.
https://opensource.org/licenses/mit-license.php
=============================================================================================================================================== */
(function () {
main();
})();
/**
* main function
*/
function main() {
var artboard = {
width: 1920,
height: 1080,
center: { x: 0, y: 0 }
};
var title = 'Array Objects';
var aw = 'Artwork';
var bg = 'Background';
var mode = DocumentColorSpace.RGB;
// setup ----------------------------------------------------------------------
if (app.documents.length == 0) {
createDocument(title, artboard.width, artboard.height, mode);
}
else {
artboard.width = app.activeDocument.width;
artboard.height = app.activeDocument.height;
}
artboard.center.x = artboard.width / 2;
artboard.center.y = artboard.height / 2;
if (!existsLayer(bg)) {
createLayer(bg);
createBackground(bg, artboard.width, artboard.height, setRGBColor(26, 32, 38));
}
if (!existsLayer(aw)) {
createLayer(aw);
}
// draw ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var frame = 60;
var diameter = 6;
var unit = 40;
var widthCount = artboard.width / unit;
var heightCount = artboard.height / unit;
var count = widthCount * heightCount;
var mods = [];
var index = 0;
for (var y = 0; y < heightCount; y++) {
for (var x = 0; x < widthCount; x++) {
mods[index++] = {
xOff: x * unit,
yOff: y * unit,
x: unit / 2,
y: unit / 2,
speed: map(Math.random(), 0, 1, 0.05, 0.8),
unit: unit,
xDir: 1,
yDir: 1
};
}
}
for (var j = 0; j < frame; j++) {
for (var i = 0; i < count; i++) {
update(mods[i]);
}
}
var color = setRGBColor(Math.random() * 255, Math.random() * 255, Math.random() * 255);
for (var i = 0; i < count; i++) {
var d = map(diameter * Math.random(), 0, diameter, 2, 10);
if (d < 5) color = setRGBColor(Math.random() * 255, Math.random() * 255, Math.random() * 255);
createEllipse(aw, mods[i].xOff + mods[i].x, (mods[i].yOff + mods[i].y) * -1, d, d, color);
}
app.executeMenuCommand('fitin');
}
/**
* Updating the properties.
* @param {Object} mod
*/
function update(mod) {
mod.x = mod.x + mod.speed * mod.xDir;
if (mod.x >= mod.unit || mod.x <= 0) {
mod.xDir *= -1;
mod.x = mod.x + 1 * mod.xDir;
mod.y = mod.y + 1 * mod.yDir;
}
if (mod.y >= mod.unit || mod.y <= 0) {
mod.yDir *= -1;
mod.y = mod.y + 1 * mod.yDir;
}
}
/**
* Creates a new pathItem in the shape of an ellipse.
* @param {string} layerName The name of the layer.
* @param {number} x The x-coordinate of the center of the ellipse.
* @param {number} y The y-coordinate of the center of the ellipse.
* @param {number} width The width of the PathItem.
* @param {number} height The height of the PathItem.
* @param {Color} color The color of the PathItem.
* @returns {PathItem}
*/
function createEllipse(layerName, x, y, width, height, color) {
var layer = app.activeDocument.layers.getByName(layerName);
var ellipse = layer.pathItems.ellipse(y + height / 2, x - width / 2, width, height);
ellipse.stroked = false;
ellipse.filled = true;
ellipse.fillColor = color;
return ellipse;
}
/**
* Creates a background object in the document.
* @param {string} layerName The name of the layer.
* @param {number} width The width of the PathItem.
* @param {number} height The height of the PathItem.
* @param {Color} color The color of the PathItem.
* @returns {PathItem}
*/
function createBackground(layerName, width, height, color) {
var layer = app.activeDocument.layers.getByName(layerName);
var bg = layer.pathItems.rectangle(0, 0, width, height);
bg.stroked = false;
bg.filled = true;
bg.fillColor = color;
return bg;
}
/**
* Creates a new document.
* @param {string} title The document title.
* @param {number} width The width of this document.
* @param {number} height The height of the document.
* @param {DocumentColorSpace} mode The color space for the new document.
* @returns {Document}
*/
function createDocument(title, width, height, mode) {
var preset = new DocumentPreset();
preset.title = title;
preset.width = width;
preset.height = height;
preset.colorMode = mode;
preset.units = RulerUnits.Points;
var document = app.documents.addDocument(title, preset);
var artboards = document.artboards;
artboards[0].artboardRect = [0, 0, width, -height];
return document;
}
/**
* Creates a new layer in the document.
* @param {string} name The name of the layer.
* @returns {Layer}
*/
function createLayer(name) {
var layer = app.activeDocument.layers.add();
layer.name = name;
layer.zOrder(ZOrderMethod.BRINGTOFRONT);
return layer;
}
/**
* Check if the layer exists.
* @param {string} name The name of the layer.
* @returns {boolean}
*/
function existsLayer(name) {
try {
app.activeDocument.layers.getByName(name);
return true;
}
catch (e) {
return false;
}
}
/**
* Setting a RGB color.
* @param {number} r The red color value.
* @param {number} [g] The green color value. Optional.
* @param {number} [b] The blue color value. Optional.
* @returns {RGBColor} RGBColor
*/
function setRGBColor(r, g, b) {
if (g == undefined) g = r;
if (b == undefined) b = r;
var color = new RGBColor();
color.red = r;
color.green = g;
color.blue = b;
return color;
}
/**
* Re-maps a number from one range to another.
* @param {number} value The incoming value to be converted.
* @param {number} start1 Lower bound of the value's current range.
* @param {number} stop1 Upper bound of the value's current range.
* @param {number} start2 Lower bound of the value's target range.
* @param {number} stop2 Upper bound of the value's target range.
* @returns {number} Remapped number.
*/
function map(value, start1, stop1, start2, stop2) {
var distance1 = stop1 - start1;
var value1 = value - start1;
var ratio = value1 / distance1;
var distance2 = stop2 - start2;
var value2 = distance2 * ratio;
return start2 + value2;
}