forked from lionello/nodemcu-httpd
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
166 lines (150 loc) · 5.37 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
<!DOCTYPE html>
<html>
<head>
<title>DSL lights</title>
<meta name="mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width" />
<link rel="icon" sizes="82x99" href="http://www.dimsumlabs.com/wp-content/uploads/2014/08/logo_dsl_text_82x99.png"/>
<link rel="stylesheet" href="gh-fork-ribbon.css" />
</head>
<body style="margin:0">
<canvas id="colorspace" width="1362" height="680"></canvas>
<div style="position:absolute;top:0;left:0">
<select id="host">
<option value="helios.lan">Front (window)</option>
<option value="helios2.lan">Back (screen)</option>
<option value="helios.lan helios2.lan">Both</option>
</select>
<input type="checkbox" id="adc" value="adc"><label for="adc">ADC</label>
</div>
<a class="github-fork-ribbon right-top" href="https://github.com/dimsumlabs/nodemcu-httpd/tree/lights" title="Fork me on GitHub">Fork me on GitHub</a>
</body>
<script type="text/javascript">
(function (){
var host = location.hostname;
document.getElementById('host').selectedIndex = location.hostname.lastIndexOf("helios2") === 0 ? 1 : 0;
document.getElementById('host').focus();
document.getElementById('host').addEventListener('change', function(event) {
host = event.target.value;
});
document.getElementById('adc').addEventListener('click', function(event) {
event.target.disabled = true;
var ms = event.target.checked ? 5 : 0;
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) event.target.disabled = false;
}
// Only helios.lan has ADC hooked up
req.open("POST", '//helios.lan/adc.lua?ms=' + ms, true);
req.send();
}, false);
var canvas = document.getElementById('colorspace');
var ctx = canvas.getContext('2d');
function drawCanvas() {
var colours = ctx.createLinearGradient(30, 0, window.innerWidth, 0);
for(var i=0; i <= 360; i+=10) {
colours.addColorStop(i/360, 'hsl(' + i + ', 100%, 50%)');
}
ctx.fillStyle = colours;
ctx.fillRect(30, 0, window.innerWidth, window.innerHeight);
var luminance = ctx.createLinearGradient(0, 0, 0, ctx.canvas.height);
luminance.addColorStop(0, '#ffffff');
luminance.addColorStop(0.05, '#ffffff');
luminance.addColorStop(0.5, 'rgba(255,255,255,0)');
luminance.addColorStop(0.5, 'rgba(0,0,0,0)');
luminance.addColorStop(0.95, '#000000');
luminance.addColorStop(1, '#000000');
ctx.fillStyle = luminance;
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
var greyscale = ctx.createLinearGradient(0, 30, 0, ctx.canvas.height);
greyscale.addColorStop(0, '#ffffff');
greyscale.addColorStop(1, '#000000');
ctx.fillStyle = greyscale;
ctx.fillRect(0, 0, 30, ctx.canvas.height);
}
var canSend = true;
var pending = false;
var rr=127, gg=127, bb=127;
function sendColour() {
function colourCorrect(v) {
v = Math.max(0, Math.min(255, v));
return Math.round(1023-(v*v)/64);
}
var params = [
'r=' + colourCorrect(rr),
'g=' + colourCorrect(gg),
'b=' + colourCorrect(bb)
].join('&');
canSend = pending = false;
var hosts = host.split(' ');
for (var i=0; i<hosts.length; ++i) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4) {
setTimeout(function() { if (pending) sendColour(); else canSend = true; }, 100);
}
}
req.open("POST", '//'+hosts[i]+'/rgb.lua?' + params, true);
req.send();
}
}
function setColour(r, g, b) {
if (r == rr && g == gg && b == bb) {
return;
}
if (r < 0 && g < 0 && b < 0) {
return;
}
if (r > 255 && g > 255 && b > 255) {
return;
}
rr = r;
gg = g;
bb = b;
pending = true;
if (canSend) {
sendColour();
}
}
function handleEvent(clientX, clientY) {
var data = ctx.getImageData(clientX, clientY, 1, 1).data;
setColour(data[0], data[1], data[2]);
}
canvas.addEventListener("touchmove", function(event){
handleEvent(event.touches[0].clientX, event.touches[0].clientY);
}, false);
canvas.addEventListener('mousewheel', function(event) {
var delta = Math.min(1, Math.max(-1, event.wheelDelta || -event.detail));
setColour(rr - delta, gg - delta, bb - delta);
event.preventDefault();
return false;
}, false);
var mouseDown = false;
canvas.addEventListener('mousedown', function(event) {
if (event.button == 0) {
handleEvent(event.clientX, event.clientY);
mouseDown = true;
}
}, false);
document.addEventListener('mouseup', function(event) {
if (event.button == 0) {
mouseDown = false;
}
}, false);
canvas.addEventListener('mousemove', function(event) {
if (mouseDown) {
handleEvent(event.clientX, event.clientY);
}
}, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawCanvas();
}
window.addEventListener('resize', resizeCanvas, false);
resizeCanvas();
drawCanvas();
document.ontouchmove = function(e) {e.preventDefault()};
})();
</script>
</html>