-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
143 lines (121 loc) · 3.73 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
<style>
body {
background-color: black;
text-align: center;
}
</style>
<html>
<head>
<script src="jquery-1.10.2.js"></script>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript">
$(window).bind('load', function() {
// Parameters
var WIDTH = 1101;
var HEIGHT = 1000;
var BACKGROUND_COLOUR = [0, 0, 0];
var TIME_PERIOD_MS = 1;
var INITIAL_DELAY_MS = 1;
// Variables
var currentRowIndex = 1;
var hue = 50;
var luminescence = 50;
var saturation = 50;
// Execution
initializePage();
initializeDrawing();
setTimeout(drawOverTime, INITIAL_DELAY_MS);
function initializePage() {
$('#content').append('<canvas id="canvas" width="300" height="225"></canvas>');
$('#canvas').attr('width', WIDTH);
$('#canvas').attr('height', HEIGHT);
}
function initializeDrawing() {
var context = getCanvasContext();
setColour(context);
drawPoint(context, WIDTH / 2, 0);
}
function drawOverTime() {
setInterval(drawNextLine, TIME_PERIOD_MS);
}
function drawNextLine() {
drawLine(currentRowIndex);
currentRowIndex++;
hue++;
// luminescence = (luminescence + 1) % 101 + 1;
// saturation = (saturation + 1) % 101 + 1;
}
function drawLine(y) {
var context = getCanvasContext();
setColour(context);
for (var x = 1; x < WIDTH - 1; x++) {
var isPointOn = shouldPopulatePoint(context, x, y);
if (isPointOn) {
drawPoint(context, x, y);
}
}
}
function getCanvasContext() {
var canvas = document.getElementById("canvas");
return canvas.getContext("2d");
}
function drawPoint(context, x, y) {
context.fillRect(x, y, 1, 1);
}
function isPointPopulated(context, x, y) {
var point = context.getImageData(x, y, 1, 1).data;
return point[0] !== BACKGROUND_COLOUR[0] || point[1] !== BACKGROUND_COLOUR[1] || point[2] !== BACKGROUND_COLOUR[2];
}
function shouldPopulatePoint(context, x, y) {
if (y === 0 || x === 0 || x === WIDTH - 1) {
return false;
}
var isLeftParentOn = isPointPopulated(context, x-1, y-1);
var isRightParentOn = isPointPopulated(context, x+1, y-1);
return isLeftParentOn !== isRightParentOn;
}
function setColour(context) {
context.fillStyle = "hsl(" + hue + ", " + saturation + "%, " + luminescence + "%)";
}
/**
* Copied from: https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
*
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param {number} h The hue
* @param {number} s The saturation
* @param {number} l The lightness
* @return {Array} The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
var hue2rgb = function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
});
</script>
<div id="content">
</div>
</body>
</html>