-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
187 lines (160 loc) · 7.4 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>MegaPixels</title>
<style>
body {
font-family: arial;
}
</style>
</head>
<body>
<h2>Mega Pixels!</h2>
<table>
<tr>
<td><strong>Color list (comma separated): </strong></td>
<td><input id="colorList" type="text" value="#041E37, #267257, #1D1959" /></td>
</tr>
<tr>
<td><strong>Height: </strong></td>
<td><input id="pixelRows" type="text" value="20" /></td>
</tr>
<tr>
<td><strong>Width: </strong></td>
<td><input id="pixelCols" type="text" value="20" /></td>
</tr>
<tr>
<td><strong>Pixel size: </strong></td>
<td><input id="pixelSize" type="text" value="40" /></td>
</tr>
<tr>
<td><strong>Animate colors: </strong></td>
<td><input type="checkbox" id="animateColors"></td>
</tr>
</table>
<button id="generateImage">Generate Image</button>
<br>
<canvas id="outCanvas"></canvas>
<script src="./ImageUtils.js"></script>
<script>
"use strict";
var mOutCanvas = document.getElementById("outCanvas");
var mOutCtx = mOutCanvas.getContext("2d");
var mColorAnimationInterval = 0;
var mImageData;
var mPixelSize;
var COLOR_LIST_ID = "colorList";
var PIXEL_ROWS_ID = "pixelRows";
var PIXEL_COLS_ID = "pixelCols";
var PIXEL_SIZE_ID = "pixelSize";
function onGeneratePressed(e) {
// Reset the color animation checkbox and cancel the interval.
clearInterval(mColorAnimationInterval);
document.getElementById("generateImage").checked = false;
var rows = parseInt(document.getElementById(PIXEL_ROWS_ID).value, 10);
var cols = parseInt(document.getElementById(PIXEL_COLS_ID).value, 10);
mPixelSize = parseInt(document.getElementById(PIXEL_SIZE_ID).value, 10);
var colors = document.getElementById(COLOR_LIST_ID).value.split(",");
var colorList = new Array();
for (var i = 0; i < colors.length; i++) {
colorList[i] = createColorFromHex(colors[i].trim());
}
// Resize the output canvas.
mOutCanvas.width = mPixelSize * cols;
mOutCanvas.height = mPixelSize * rows;
mImageData = generateImageData(rows, cols, colorList);
doRowSwap(mImageData, 0.50);
renderImage(mImageData, mPixelSize);
}
function renderImage(imageData, pixelSize) {
for (var row = 0; row < imageData.length; row++) {
for (var col = 0; col < imageData[row].length; col++) {
mOutCtx.fillStyle = colorToHex(imageData[row][col]);
mOutCtx.fillRect(col * pixelSize, row * pixelSize, pixelSize, pixelSize);
}
}
}
function doRowSwap(imageData, swapProbability) {
for (var row = 0; row < imageData.length - 1; row++) {
for (var col = 0; col < imageData[row].length; col++) {
if (Math.random() < swapProbability) continue;
var temp = imageData[row][col];
imageData[row][col] = imageData[row + 1][col];
imageData[row + 1][col] = temp;
}
}
}
function colorToHex(color) {
var r = color.R.toString(16);
var g = color.G.toString(16);
var b = color.B.toString(16);
return "#" + (r.length < 2 ? "0" : "") + "" + r +
"" + (g.length < 2 ? "0" : "") + "" + g +
"" + (b.length < 2 ? "0" : "") + "" + b;
}
function generateImageData(rows, cols, colorList) {
if (colorList.length < 2) {
alert("More than two colors are required!");
throw "More than two colors are required!";
}
var rowsPerColor = rows / (colorList.length - 1);
// Create pixel array to work on.
var pixelArray = new Array(rows);
for (var i = 0; i < rows; i++) {
var colorShiftAmount = (i % rowsPerColor) / rowsPerColor;
var color1 = colorList[Math.floor(i / rowsPerColor)];
var color2 = colorList[Math.floor(i / rowsPerColor) + 1];
pixelArray[i] = new Array(cols);
for (var j = 0; j < cols; j++) {
pixelArray[i][j] = createColor(
color2.R * colorShiftAmount + color1.R * (1 - colorShiftAmount),
color2.G * colorShiftAmount + color1.G * (1 - colorShiftAmount),
color2.B * colorShiftAmount + color1.B * (1 - colorShiftAmount));
}
}
return pixelArray;
}
function createColorFromHex(hexString) {
if (hexString.charAt(0) != "#" && hexString.length != 7) {
alert("Invalid color: " + colorList[i]);
throw "Invalid color: " + colorList[i];
}
var r = parseInt(hexString.substring(1, 3), 16);
var g = parseInt(hexString.substring(3, 5), 16);
var b = parseInt(hexString.substring(5), 16);
return createColor(r, g, b)
}
function createColor(r, g, b) {
var hsl = ImageUtils.rgbToHsl(r, g, b);
return {"R":Math.floor(r), "G":Math.floor(g), "B":Math.floor(b),
"H": hsl.H, "S": hsl.S, "L": hsl.L};
}
function onAnimateChecked(e) {
if (!mImageData) return;
if (!e.target.checked) {
clearInterval(mColorAnimationInterval);
return;
}
mColorAnimationInterval = setInterval(rotateColors, 100);
}
function rotateColors() {
for (var row = 0; row < mImageData.length; row++) {
for (var col = 0; col < mImageData[row].length; col++) {
mImageData[row][col].H += 2;
mImageData[row][col].H %= 360;
var newRgb = ImageUtils.hslToRgb(
mImageData[row][col].H,
mImageData[row][col].S,
mImageData[row][col].L);
mImageData[row][col].R = newRgb.R;
mImageData[row][col].G = newRgb.G;
mImageData[row][col].B = newRgb.B;
}
}
renderImage(mImageData, mPixelSize);
}
document.getElementById("generateImage").addEventListener("click", onGeneratePressed);
document.getElementById("animateColors").addEventListener("click", onAnimateChecked);
</script>
</body>
</html>