-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidthAndHeight.js
executable file
·190 lines (157 loc) · 5.12 KB
/
WidthAndHeight.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
/* ===============================================================================================================================================
WidthAndHeight.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/structure-width-and-height.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
};
var title = 'Width and Height';
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;
}
if (!existsLayer(bg)) {
createLayer(bg);
createBackground(bg, artboard.width, artboard.height, setRGBColor(26, 32, 38));
}
if (!existsLayer(aw)) {
createLayer(aw);
}
// draw ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (var i = 0; i < artboard.height; i += 20) {
var r = Math.random() * 255;
var g = Math.random() * 255;
var b = Math.random() * 255;
createRect(aw, 0, i * -1, artboard.width, 10, setRGBColor(r, g, b));
if (i < artboard.width) createRect(aw, i, 0, 10, artboard.height, setRGBColor(255));
}
app.executeMenuCommand('fitin');
}
/**
* Creates a new pathItem in the shape of an rectangle.
* @param {string} layerName The name of the layer.
* @param {number} x The left position of the path item.
* @param {number} y The top position of the path item.
* @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 createRect(layerName, x, y, width, height, color) {
var layer = app.activeDocument.layers.getByName(layerName);
var rect = layer.pathItems.rectangle(y, x, width, height);
rect.stroked = false;
rect.filled = true;
rect.fillColor = color;
return rect;
}
/**
* 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(name, width, height, mode) {
var preset = new DocumentPreset();
preset.title = name;
preset.width = width;
preset.height = height;
preset.colorMode = mode;
preset.units = RulerUnits.Points;
var document = app.documents.addDocument(name, 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;
}