-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal_js.js
307 lines (274 loc) · 11.6 KB
/
fractal_js.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
function newFractals() {
'use strict';
var raf_ID = 0;
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;
var FILL_PREFIX = 'hsl(';
var LIGHTING = ', ' + (Math.random() * 15 + 45) + '%, ' + (Math.random() * 15 + 45) + '%)';
var FPS = 30;
var FPR = (Math.random() * 550) + 250;
var IS_STATIC = true;
var SIN_SIXTY = Math.sqrt(3) / 2;
var TAN_THIRTY = 1 / Math.sqrt(3);
var SQRT_TWO = Math.sqrt(2);
var initialRadiusRatio;
var prevColor;
var fractalShape;
var currentTheta = Math.PI;
var STROKE_COLOR = 'rgb(255,255,255)';
var FRACTAL_OPACITY;
var BACKGROUND_OPACITY;
var HAS_FILL;
var HAS_INTERIOR_CHILDREN;
var ROTATION_DIRECTION;
var TRANSLATION_THETA;
var MIN_RADIUS;
var RADIUS_COEFFICIENT;
var shapes = {
0 : "circle",
1 : "square",
2 : "triangle"
};
var direction = {
"initial": 0,
"up": 1,
"right": 2,
"down": 3,
"left": 4
};
function init(parent) {
randomize();
resize();
parent.appendChild(canvas);
startRender();
window.onresize = function() {
resize();
rescaleIcons();
}
document.addEventListener('keyup', function(evt) {
if (evt.code === 'Space'){
if (IS_STATIC) {
IS_STATIC = false;
} else {
randomize();
IS_STATIC = true;
currentTheta = Math.PI;
}
}
});
canvas.addEventListener('click', function(evt) {
if (IS_STATIC) {
IS_STATIC = false;
} else {
randomize();
IS_STATIC = true;
currentTheta = Math.PI;
}
}, false);
}
function randomize() {
FPR = (Math.random() * 500) + 250;
var smallerThan = Math.random() * 0.2 + 0.1;
var largerThan = Math.random() * 0.25 + 1;
initialRadiusRatio = smallerThan;
if (Math.random() > 0.75) {
initialRadiusRatio = largerThan;
FPR = FPR * 2;
}
//STROKE_COLOR = Math.random() < 0.5 ? 'rgb(255,255,255)' : 'rgb(0,0,0)';
prevColor = (360 * Math.random());
FRACTAL_OPACITY = Math.random() * 0.5 + 0.35;
BACKGROUND_OPACITY = 1;
HAS_FILL = Math.random() < 0.5 ? false : true;
HAS_INTERIOR_CHILDREN = Math.random() < 0.5 ? true : false;
ROTATION_DIRECTION = Math.random() < 0.5 ? -1 : 1;
TRANSLATION_THETA = ((2 * Math.PI) * ROTATION_DIRECTION) / FPR;
MIN_RADIUS = (Math.random() * 10) + 9;
RADIUS_COEFFICIENT = (Math.random() * 0.125) + 0.35;
fractalShape = Math.round(Math.random() * 1);
}
function render() { // Recrusive Animation loop
setTimeout(function() {
raf_ID = window.requestAnimationFrame(render);
ctx.globalAlpha = BACKGROUND_OPACITY;
ctx.fillStyle = "rgba(25,25,25)";
ctx.fillRect(0, 0, width, height);
renderFractals();
if (Math.abs(currentTheta + TRANSLATION_THETA) < Math.PI * 2) {
currentTheta += TRANSLATION_THETA;
} else {
currentTheta = 0;
}
}, 1000/FPS);
}
function startRender() {
render();
}
function stopRender() {
window.cancelAnimationFrame(raf_ID);
}
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
}
function renderFractals() {
ctx.lineWidth = 3;
ctx.globalAlpha = FRACTAL_OPACITY;
ctx.strokeStyle = STROKE_COLOR;
var smallerDimension = width < height ? width : height;
var initialRadius = smallerDimension * initialRadiusRatio;
prevColor = drawFractal(width / 2, (height / 2) + (height * 0.05), 0, 0, initialRadius, direction["initial"], prevColor);
}
function drawFractal(oldX, oldY, parentX, parentY, radius, drawnInDirection, prevColor) {
var currentX;
var currentY;
if (radius > MIN_RADIUS) {
if (drawnInDirection != 0 && !IS_STATIC) {
var newPos = circularTranslation(oldX, oldY, parentX, parentY, drawnInDirection);
currentX = newPos[0];
currentY = newPos[1];
} else {
currentX = oldX;
currentY = oldY;
}
if (currentX > -radius && currentX < width + radius && currentY > -radius && currentY < height + radius) {
if (HAS_FILL) {
prevColor += (Math.random() * 2) + 1;
if (prevColor == 360) {
prevColor = 0;
}
ctx.fillStyle = FILL_PREFIX + prevColor + LIGHTING;
}
if (shapes[fractalShape] == "circle") {
drawCircle(currentX, currentY, radius);
} else if (shapes[fractalShape] == "square") {
drawSquare(currentX, currentY, radius);
} else if (shapes[fractalShape] == "triangle") {
drawTriangle(currentX, currentY, radius);
}
if (HAS_FILL) {
ctx.fill();
}
}
if (HAS_INTERIOR_CHILDREN) {
drawFractal(currentX + radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["right"], prevColor);
drawFractal(currentX - radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["left"], prevColor);
drawFractal(currentX, currentY + radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["down"], prevColor);
drawFractal(currentX, currentY - radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["up"], prevColor);
} else {
switch(drawnInDirection) {
case 1: // Drawn upwards
drawFractal(currentX + radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["right"], prevColor);
drawFractal(currentX - radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["left"], prevColor);
drawFractal(currentX, currentY - radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["up"], prevColor);
break;
case 2: // Drawn rightwards
drawFractal(currentX + radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["right"], prevColor);
drawFractal(currentX, currentY + radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["down"], prevColor);
drawFractal(currentX, currentY - radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["up"], prevColor);
break;
case 3: // Drawn downwards
drawFractal(currentX + radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["right"], prevColor);
drawFractal(currentX - radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["left"], prevColor);
drawFractal(currentX, currentY + radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["down"], prevColor);
break;
case 4: // Drawn leftwards
drawFractal(currentX - radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["left"], prevColor);
drawFractal(currentX, currentY + radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["down"], prevColor);
drawFractal(currentX, currentY - radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["up"], prevColor);
break;
default: // First fractal OR Fractals w/interior children
drawFractal(currentX + radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["right"], prevColor);
drawFractal(currentX - radius, currentY, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["left"], prevColor);
drawFractal(currentX, currentY + radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["down"], prevColor);
drawFractal(currentX, currentY - radius, oldX, oldY, radius * RADIUS_COEFFICIENT, direction["up"], prevColor);
break;
}
}
}
if (HAS_FILL) {
return prevColor;
} else {
return 0;
}
}
function circularTranslation(xPos, yPos, parentX, parentY, drawnInDirection) {
var offsetX = Math.abs(parentX - xPos);
var offsetY = Math.abs(parentY - yPos);
var radialDistance = Math.sqrt(Math.pow(offsetX,2) + Math.pow(offsetY, 2));
var angleToPoint;
switch (drawnInDirection) {
case 1:
angleToPoint = Math.PI * 0.5;
break;
case 2:
angleToPoint = Math.PI;
break;
case 3:
angleToPoint = Math.PI * 1.5;
break;
case 4:
angleToPoint = Math.PI * 2;
break;
}
var newAngle = angleToPoint + currentTheta;
if (parentX - (width / 2) < 10){
}
var newX = (Math.cos(newAngle) * radialDistance + parentX);
var newY = (Math.sin(newAngle) * radialDistance + parentY);
return [newX, newY];
}
function drawCircle(x, y, radius) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.closePath();
ctx.stroke();
}
function drawSquare(x, y, halfWidth) {
ctx.moveTo(x - halfWidth, y - halfWidth);
ctx.beginPath();
ctx.lineTo(x + halfWidth, y - halfWidth);
ctx.lineTo(x + halfWidth, y + halfWidth);
ctx.lineTo(x - halfWidth, y + halfWidth);
ctx.lineTo(x - halfWidth, y - halfWidth);
ctx.closePath();
ctx.stroke();
}
function drawTriangle(x, y, halfHeight) {
var sideLength = (halfHeight * 2) / SIN_SIXTY;
var pointOne = [x, y - halfHeight];
var pointTwo = [x + sideLength / 2, y + (2 * halfHeight)];
var pointThree = [pointTwo[0] - sideLength, pointTwo[1]];
ctx.beginPath();
ctx.lineTo(pointOne[0], pointOne[1]);
ctx.lineTo(pointTwo[0], pointTwo[1]);
ctx.lineTo(pointThree[0], pointThree[1]);
ctx.closePath();
ctx.stroke();
}
return {
init: init,
startRender: startRender,
stopRender: stopRender
};
}
window.onload = function() {
newFractals().init(document.body);
rescaleIcons();
}
function rescaleIcons(){
var icons = document.getElementsByName('icon-svg');
var icon_boxes = document.getElementsByClassName("icon-box");
var heightBasedSize = Math.floor(window.innerHeight * 0.065);
var widthBasedSize = Math.floor(((window.innerWidth * 0.45) / 3) - 20);
var size = ((0.425 * window.innerWidth) / 3) < (window.innerHeight * 0.1) ? widthBasedSize : heightBasedSize;
for (var i = 0; i < icons.length; i++){
icons[i].style.fontSize = size + 'px';
icon_boxes[i].style.padding = "5px 7px 5px 7px";
}
var icons = document.getElementsByName('fractal-svg');
icons[0].style.height = size + 'px';
icons[0].style.padding = "0px";
}