-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
168 lines (131 loc) · 3.74 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
window.onload = function() {
"use strict";
(function() {
var link = document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
link.href = 'favicon.ico';
document.getElementsByTagName('head')[0].appendChild(link);
}());
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var redFunctionInput = document.getElementById("redFunc");
var greenFunctionInput = document.getElementById("greenFunc");
var blueFunctionInput = document.getElementById("blueFunc");
var input = document.getElementById("popout");
var redFunctionText;
var greenFunctionText;
var blueFunctionText;
var redFunction;
var greenFunction;
var blueFunction;
var spacing = 0;
var amountOfTimesDrawn = 0
var drawIntevalId = 0;
var pointDimensions = {
x: 20,
y: 20,
};
var displayDimensions = {
x: window.innerWidth,
y: window.innerHeight,
};
var colorType = {
red: 1,
green: 2,
blue: 3,
}
var colors = [];
var onResize = function() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
displayDimensions.x = window.innerWidth / (pointDimensions.x);
displayDimensions.y = window.innerHeight / (pointDimensions.y);
};
var defString = "sin = Math.sin;var cos = Math.cos;var tan = Math.tan;var abs = Math.abs;var pow = Math.pow;var pi = Math.PI;var e = Math.E;"
// converts an rgb value to a string '#RRGGBB'
var RGB = function (R, G, B) {
if (R == undefined || G == undefined || B == undefined) {
return '#FFFFFF';
}
var rStr = (R < 0x10 ? "0" : "") + R.toString(16);
var gStr = (G < 0x10 ? "0" : "") + G.toString(16);
var bStr = (B < 0x10 ? "0" : "") + B.toString(16);
return "#" + rStr + gStr + bStr;
};
var getPixelPosition = function(i, j) {
return {
x: i * (pointDimensions.x + spacing) + spacing,
y: j * (pointDimensions.y + spacing) + spacing
};
};
var proccessTime = function(times) {
return times;
};
var calculateColors = function() {
var t = proccessTime(amountOfTimesDrawn++);
for (var i = 0; i < displayDimensions.x; i++) {
for (var j = 0; j < displayDimensions.y; j++) {
var position = getPixelPosition(i, j);
var x = position.x;
var y = position.y;
var r = 100;
var g = 100;
var b = 100;
try {
r = Math.floor(Math.abs(redFunction(x, y, i, j, t))) % 256;
}
catch(e) {
}
try {
g = Math.floor(Math.abs(greenFunction(x, y, i, j, t))) % 256;
}
catch(e) {
}
try {
b = Math.floor(Math.abs(blueFunction(x, y, i, j, t))) % 256;
}
catch(e) {
}
colors[i + j * displayDimensions.y] = RGB(r, g, b);
}
}
};
var draw = function () {
for (var i = 0; i < displayDimensions.x; i++) {
for (var j = 0; j < displayDimensions.y; j++) {
var position = getPixelPosition(i, j);
ctx.fillStyle = colors[i + j * displayDimensions.y];
ctx.fillRect(
position.x,
position.y,
pointDimensions.x,
pointDimensions.y);
}
}
};
var updateFunctions = function() {
redFunctionText = redFunctionInput.value;
greenFunctionText = greenFunctionInput.value;
blueFunctionText = blueFunctionInput.value;
redFunction = createFunction(redFunctionText);
greenFunction = createFunction(greenFunctionText)
blueFunction = createFunction(blueFunctionText)
};
var createFunction = function(functionText) {
return new Function("x", "y", "i", "j", "t", defString + "return " + functionText + ";")
};
document.onkeydown = function(e) {
if (e.keyCode == 27) {
input.style.display = input.style.display == "none" ? "inherit" : "none";
}
};
window.onresize = onResize;
onResize();
updateFunctions();
drawIntevalId = setInterval(function() {
calculateColors();
updateFunctions();
draw();
}, 1);
};