-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
286 lines (247 loc) · 6.24 KB
/
main.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
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
console.log("Starting procedural shape infinity generation...");
//Modules
const {createCanvas} = require("canvas");
const fs = require("fs");
const Gradient = require("gradient2");
const randomColor = require("randomcolor");
//Fill in a shape with any number of sides
function fillShape(context, x, y, sides, size, color, strokeWidth = 1, isFill = false)
{
context.beginPath();
context.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));
for(let i = 1; i < sides; i++)
{
context.lineTo(x + size * Math.cos(i * 2 * Math.PI / sides), y + size * Math.sin(i * 2 * Math.PI / sides));
}
context.closePath();
context.strokeStyle = color;
context.lineWidth = strokeWidth;
context.stroke();
if(isFill)
{
context.fillStyle = color;
context.fill();
}
}
//random integer between min (included) and max (included)
function randomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//random float between min (included) and max (excluded):
function randomFloat(min, max)
{
return Math.random() * (max - min) + min;
}
/*function randomColor()
{
const letters = "0123456789ABCDEF";
let color = '#';
for (let i = 0; i < 6; i++)
{
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}*/
class ShapeInfinity
{
canvasSize;
repetitions;
scale;
rotation;
translate;
backgroundColor;
backgroundColorUseGradient;
startRotation;
gradientRotation;
shapeSides;
shapeSize;
strokeSize;
isAlternativeGradientType;
isFill;
gradientColors;
constructor({canvasSize, repetitions, scale, rotation, translate, backgroundColor, backgroundColorUseGradient, startRotation, gradientRotation, shapeSides, shapeSize, strokeSize, isAlternativeGradientType, isFill, gradientColors})
{
if(repetitions < 50 || repetitions > 1000)
{
throw new RangeError("The repetitions varible must be between 50 and 1000.");
}
this.canvasSize = canvasSize;
this.repetitions = repetitions;
this.scale = scale;
this.rotation = rotation;
this.translate = translate;
this.backgroundColor = backgroundColor;
this.backgroundColorUseGradient = backgroundColorUseGradient;
this.startRotation = startRotation;
this.gradientRotation = gradientRotation;
this.shapeSides = shapeSides;
this.shapeSize = shapeSize;
this.strokeSize = strokeSize;
this.isAlternativeGradientType = isAlternativeGradientType;
this.isFill = isFill;
this.gradientColors = gradientColors;
}
generate()
{
//Setup
const canvas = createCanvas(this.canvasSize, this.canvasSize);
const context = canvas.getContext("2d");
//Gradient
let gradient;
let colors;
if(this.isAlternativeGradientType)
{
gradient = new Gradient({
colors: this.gradientColors,
steps: this.repetitions,
model: "rgb"
});
colors = gradient.toArray();
}
else
{
gradient = context.createLinearGradient(0, 0, Math.cos(this.gradientRotation) * canvas.width, Math.sin(this.gradientRotation) * canvas.height);
this.gradientColors.forEach(color => gradient.addColorStop(color.pos, color.color));
}
//Background
context.fillStyle = (this.isAlternativeGradientType || this.backgroundColorUseGradient) ? this.gradientColors[0].color : this.backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
//Start matrix
context.translate(canvas.width / 2, canvas.height / 2)
context.rotate(this.startRotation)
if(this.isAlternativeGradientType)
{
colors = gradient.toArray();
}
for(let i = 0; i < this.repetitions; i++)
{
const color = this.isAlternativeGradientType ? colors[i] : gradient;
fillShape(context, 0, 0, this.shapeSides, this.shapeSize, color, this.strokeSize, this.isAlternativeGradientType && this.isFill);
if(this.isAlternativeGradientType && this.repetitions > 300)
{
context.scale(0.999 + this.scale.x, 0.999 + this.scale.y);
}
else
{
context.scale(0.9 + this.scale.x + ((((this.repetitions - 50) * 100) / (1000 - 10)) / 960), 0.9 + this.scale.y + ((((this.repetitions - 50) * 100) / (1000 - 10)) / 960));
}
context.rotate(this.rotation);
context.translate(this.translate.x, this.translate.y);
}
return canvas;
}
}
function createRandomShapeInfinity()
{
const scaleXorY = Math.random() >= 0.5;
const scale = {
x: 0,
y: 0
}
if(scaleXorY)
{
scale.x = randomFloat(-0.1, 0);
}
else
{
scale.y = randomFloat(-0.1, 0);
}
let shapeSides = randomInt(3, 8);
if(shapeSides == 8)
{
//Circle
shapeSides = 200;
}
const gradientColorPossibilities = [
[
{
color: randomColor(),
pos: 0
},
{
color: randomColor(),
pos: 40
},
{
color: randomColor(),
pos: 80
}
],
[
{
color: randomColor(),
pos: 0
},
{
color: randomColor(),
pos: 30
},
{
color: randomColor(),
pos: 60
},
{
color: randomColor(),
pos: 80
}
],
[
{
color: randomColor(),
pos: 0
},
{
color: randomColor(),
pos: 20
},
{
color: randomColor(),
pos: 40
},
{
color: randomColor(),
pos: 60
},
{
color: randomColor(),
pos: 80
}
]
]
const gradientColors = gradientColorPossibilities[Math.floor(Math.random() * gradientColorPossibilities.length)];
const shapeInfinity = new ShapeInfinity({
canvasSize: 1000,
repetitions: randomInt(50, 1000),
scale: scale,
rotation: randomFloat(0, 2 * Math.PI),
translate: {
x: randomInt(-80, 80),
y: randomInt(-80, 80)
},
backgroundColor: randomColor(),
backgroundColorUseGradient: Math.random() >= 0.5,
startRotation: randomFloat(0, 2 * Math.PI),
gradientRotation: randomFloat(0, 2 * Math.PI),
shapeSides: shapeSides,
shapeSize: 1000 * randomFloat(0.7, 1),
strokeSize: randomInt(10, 50),
isAlternativeGradientType: Math.random() >= 0.5,
isFill: Math.random() >= 0.5,
gradientColors: gradientColors
});
return shapeInfinity;
}
const directory = "\\output";
if(!fs.existsSync(directory))
{
fs.mkdirSync(directory);
}
const filePath = `${directory}\\${new Date().getTime()}.png`;
const canvas = createRandomShapeInfinity().generate();
const out = fs.createWriteStream(__dirname + filePath);
out.on("finish", () => {
console.log(`Saved procedurally generated shape infinity as ${filePath}`);
});
const stream = canvas.createPNGStream();
stream.pipe(out);