-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathalgVines.js
More file actions
267 lines (249 loc) · 10.9 KB
/
algVines.js
File metadata and controls
267 lines (249 loc) · 10.9 KB
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
var algVines = {};
algVines.distance = function (p1, p2) {
return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1]));
}
algVines.getRandomColor = function() {
let r = Math.floor(256 * Math.random());
let g = Math.floor(256 * Math.random());
let b = Math.floor(256 * Math.random());
return "rgb(" + r + "," + g + "," + b + ")";
}
algVines.drawFilledCircle = function(x, y, radius, shape_color, shape_opacity) {
ctx.fillStyle = shape_color;
ctx.globalAlpha = shape_opacity;
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
algVines.drawTriangle = function (x, y, length, lineWidth, radians, shape_color, shape_opacity) {
let height = length * Math.sqrt(3) / 2;
let triangle_radius = length * Math.sqrt(3) / 3;
ctx.lineWidth = lineWidth;
ctx.strokeStyle = shape_color;
ctx.globalAlpha = shape_opacity;
ctx.beginPath();
if(algVines.rotation) {
// console.log(algVines.rotationRads);
let moveto_x = x + triangle_radius * Math.cos(-radians);
let moveto_y = y + triangle_radius * Math.sin(-radians);
let lineto1_x = x + triangle_radius * Math.cos(-(radians + 2 * Math.PI / 3));
let lineto1_y = y + triangle_radius * Math.sin(-(radians + 2 * Math.PI / 3));
let lineto2_x = x + triangle_radius * Math.cos(-(radians + 4 * Math.PI / 3));
let lineto2_y = y + triangle_radius * Math.sin(-(radians + 4 * Math.PI / 3));
ctx.moveTo(moveto_x, moveto_y);
ctx.lineTo(lineto1_x, lineto1_y);
ctx.lineTo(lineto2_x, lineto2_y);
ctx.lineTo(moveto_x, moveto_y);
} else {
ctx.moveTo(x, y - height / 2);
ctx.lineTo(x - length / 2, y + height / 2);
ctx.lineTo(x + length / 2, y + height / 2);
ctx.lineTo(x, y - height / 2);
}
ctx.closePath();
ctx.stroke();
}
algVines.drawSquare = function (x, y, radius, length, lineWidth, shape_color, shape_opacity, radians) {
ctx.lineWidth = lineWidth;
ctx.strokeStyle = shape_color;
ctx.globalAlpha = shape_opacity;
ctx.beginPath();
if(!algVines.rotation) {
ctx.rect(x, y, length, length);
} else {
let angle = (2 * Math.PI / 4);
ctx.moveTo(x + radius * Math.cos(0 * angle + radians), y + radius * Math.sin(0 * angle + radians));
for(i = 1; i < 4; i++) {
ctx.lineTo(x + radius * Math.cos(i * angle + radians), y + radius * Math.sin(i * angle + radians));
}
ctx.lineTo(x + radius * Math.cos(0 * angle + radians), y + radius * Math.sin(0 * angle + radians));
}
ctx.closePath();
ctx.stroke();
}
algVines.drawPentagon = function (x, y, radius, lineWidth, shape_color, shape_opacity, radians) {
ctx.lineWidth = lineWidth;
ctx.strokeStyle = shape_color;
ctx.globalAlpha = shape_opacity;
ctx.beginPath();
if(!algVines.rotation) {
let angle = 2 * Math.PI / 5;
ctx.moveTo(x + radius * Math.cos(0 * angle), y + radius * Math.sin(0 * angle));
for(i = 1; i < 5; i++) {
ctx.lineTo(x + radius * Math.cos(i * angle), y + radius * Math.sin(i * angle));
}
ctx.lineTo(x + radius * Math.cos(0 * angle), y + radius * Math.sin(0 * angle));
} else {
// Rotation code goes here
let angle = 2 * Math.PI / 5;
ctx.moveTo(x + radius * Math.cos(-(0 * angle + radians)), y + radius * Math.sin(-(0 * angle + radians)));
for(i = 1; i < 5; i++) {
ctx.lineTo(x + radius * Math.cos(-(i * angle + radians)), y + radius * Math.sin(-(i * angle + radians)));
}
ctx.lineTo(x + radius * Math.cos(-(0 * angle + radians)), y + radius * Math.sin(-(0 * angle + radians)));
}
ctx.closePath();
ctx.stroke();
}
algVines.drawLine = function(p1, p2, lineWidth, shape_color, shape_opacity) {
ctx.lineWidth = lineWidth;
ctx.strokeStyle = shape_color;
ctx.globalAlpha = shape_opacity;
ctx.beginPath();
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
ctx.stroke();
}
algVines.setBackgroundColor = function(color) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.globalAlpha = 1;
ctx.rect(0, 0, width, height);
ctx.fill();
}
algVines.lineIsValid = function(p1, p2) {
return p1[0] >= 0 && p1[1] >= 0 && p2[0] >= 0 && p2[1] >= 0 && algVines.distance(p1, p2) < algVines.spikeMaxLength;
}
/* Start at a random pixel on the canvas. Randomly choose which of the surrounding pixels to draw on next.
This is how the alg will draw the main branch of the "vines". For every pixel that the algorithm chooses to draw on,
a winding series of slowly shrinking circles will be drawn from that pixel at random angles to mimic the curly,
waviness of grapevines. */
algVines.drawNextShape = function(x, y, radians, color, opacity, numOfSides){
if(algVines.useColorList && algVines.changingColors) {
let colorIndex = Math.floor(Math.random() * algVines.colorList.length);
color = algVines.colorList[colorIndex];
} else if(algVines.useColorList && !algVines.changingColors) {
color = algVines.initialColor;
} else if(algVines.randomColor && algVines.changingColors) {
color = algVines.getRandomColor();
} else if(algVines.randomColor && !algVines.changingColors) {
color = algVines.initialColor;
}
// console.log(numOfSides);
if(numOfSides == 3) {
algVines.drawTriangle(x, y, algVines.sideLength, algVines.lineWidth, radians, color, opacity);
} else if(numOfSides == 4) {
algVines.drawSquare(x, y, Math.sqrt(2) * algVines.sideLength / 2, algVines.sideLength, algVines.lineWidth, color, opacity, radians);
} else {
// x, y, numOfSides, radius, lineWidth, shape_color, shape_opacity, radians
algVines.drawPentagon(x, y, algVines.sideLength / (2 * Math.sin(Math.PI / 5)), algVines.lineWidth, color, opacity, radians);
}
}
algVines.drawOneStep = function() {
// Reached the end
if(algVines.numOfSteps > algVines.maxNumOfSteps) {
clearInterval(algVines.loop);
return false;
}
// Increment iterations
algVines.currentIterations += 1;
// Path and movement
let prev_x = algVines.current_x;
let prev_y = algVines.current_y;
let step_x = algVines.stepX;
let step_y = algVines.stepY;
if(algVines.randomPath) {
let x_index = Math.floor(Math.random() * algVines.stepChoicesX.length);
let y_index = Math.floor(Math.random() * algVines.stepChoicesY.length);
step_x = algVines.stepChoicesX[x_index];
step_y = algVines.stepChoicesY[y_index];
} else if(algVines.curvedPath) {
let angle = 2 * Math.PI * (algVines.currentIterations % algVines.pathCycle) / algVines.pathCycle;
let radius = algVines.pathRadius;
if(algVines.changingRadius) {
radius = algVines.pathRadius + Math.floor(algVines.currentIterations / algVines.radiusIncrementCycle);
}
if(algVines.alsoLinear) {
step_x = radius * Math.cos(angle) + algVines.stepX;
step_y = algVines.stepY;
} else {
step_x = radius * Math.cos(angle);
step_y = radius * Math.sin(angle);
}
}
if(algVines.noisyPath) {
step_x += Math.floor(Math.random() * algVines.noiseX);
step_y += Math.floor(Math.random() * algVines.noiseY);
}
algVines.current_x = (algVines.current_x + step_x + width) % width;
algVines.current_y = (algVines.current_y + step_y + height) % height;
// Drawing variation
algVines.currentOpacity = - 0.9 * Math.sin(0.004 * Math.PI * (algVines.currentIterations % 250)) + 1;
algVines.currentRadius = algVines.circleRadius * Math.sin(0.01 * Math.PI * (algVines.currentIterations % 100)) + 2;
algVines.sideNum = Math.round(2 * Math.sin(0.01 * Math.PI * (algVines.currentIterations % 100)) + 3);
algVines.rotationDegs = 180 * Math.sin(0.01 * Math.PI * (algVines.currentIterations % 100));
algVines.rotationRads = algVines.rotationDegs * (Math.PI / 180);
// Select color
let color = algVines.defaultColor;
if(algVines.useColorList && algVines.changingColors) {
let colorIndex = Math.floor(Math.random() * algVines.colorList.length);
color = algVines.colorList[colorIndex];
} else if(algVines.useColorList && !algVines.changingColors) {
color = algVines.initialColor;
} else if(algVines.randomColor && algVines.changingColors) {
color = algVines.getRandomColor();
} else if(algVines.randomColor && !algVines.changingColors) {
color = algVines.initialColor;
}
// Draw shape or texture
if(algVines.drawCircles) {
let radius = algVines.circleRadius;
let opacity = algVines.circleOpacity;
if(algVines.pressureVariation && algVines.circleRandomness) {
let pressure = algVines.penPressureMin + (1.0 - algVines.penPressureMin) * Math.random();
radius = Math.floor(pressure * algVines.circleRadius);
opacity = algVines.currentOpacity;
} else if(algVines.pressureVariation && !algVines.circleRandomness){
radius = algVines.currentRadius;
opacity = algVines.currentOpacity;
}
algVines.drawFilledCircle(algVines.current_x, algVines.current_y, radius, color, opacity);
} else if(algVines.drawShapes && !algVines.rotation) {
// x, y, radians, color, opacity, numOfSides
algVines.drawNextShape(algVines.current_x, algVines.current_y, 0, color, algVines.currentOpacity, algVines.sideNum);
} else if(algVines.drawShapes && algVines.rotation) {
// console.log(algVines.rotationDegs);
algVines.drawNextShape(algVines.current_x, algVines.current_y, algVines.rotationRads, color, algVines.currentOpacity, algVines.sideNum);
} else {
for(let i = 0; i < algVines.numOfSpikes; i++) {
let x_offset = Math.floor(algVines.spikeOffsetRangeX * Math.random()) - Math.floor(algVines.spikeOffsetRangeX / 2);
let y_offset = Math.floor(algVines.spikeOffsetRangeY * Math.random()) - Math.floor(algVines.spikeOffsetRangeY / 2);
let p1 = [prev_x, prev_y];
let p2 = [algVines.current_x + x_offset, algVines.current_y + y_offset];
if(algVines.lineIsValid(p1, p2)) {
algVines.drawLine(p1, p2, algVines.spikeLineWidth, color, algVines.spikeAlpha);
}
}
}
algVines.numOfSteps++;
return true;
}
algVines.reset = function () {
algVines.pause();
algVines.currentIterations = 0;
algVines.current_x = Math.floor(Math.random() * width);
algVines.current_y = Math.floor(Math.random() * height);
if(!algVines.changingColors) {
algVines.initialColor = algVines.getRandomColor();
}
algVines.numOfSteps = 0;
}
algVines.initialize = function() {
algVines.reset();
}
algVines.pause = function () {
if("loop" in algVines) {
clearInterval(algVines.loop);
}
}
algVines.start = function () {
if(algVines.isBackgroundSet) {
algVines.setBackgroundColor(algVines.backgroundColor);
} else {
algVines.backgroundChoice = Math.floor(Math.random() * algVines.backgroundColors.length);
algVines.backgroundColor = algVines.backgroundColors[algVines.backgroundChoice];
algVines.setBackgroundColor(algVines.backgroundColor);
}
algVines.loop = setInterval(algVines.drawOneStep, algVines.speed);
}