2022-07-26 11:08:26 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2022-08-03 14:23:24 +02:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="theme-color" content="#222222">
|
|
|
|
<title>WLED Live Preview</title>
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
</style>
|
2022-07-26 11:08:26 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
2022-08-03 14:23:24 +02:00
|
|
|
<canvas id="liveviewCanvas">LiveView</canvas>
|
|
|
|
<script>
|
|
|
|
var c = document.getElementById('liveviewCanvas');
|
|
|
|
var leds = "";
|
|
|
|
var mW = 0, mH = 0, pPL = 0, lOf = 0;
|
|
|
|
var throttled = false;
|
|
|
|
function setCanvas() {
|
|
|
|
c.width = window.innerWidth * 0.98; //remove scroll bars
|
|
|
|
c.height = window.innerHeight * 0.98; //remove scroll bars
|
|
|
|
}
|
|
|
|
setCanvas();
|
|
|
|
// Check for canvas support
|
2022-08-06 11:37:48 +02:00
|
|
|
var ctx = c.getContext('2d');
|
|
|
|
if (ctx) { // Access the rendering context
|
2022-08-03 14:23:24 +02:00
|
|
|
// use parent WS or open new
|
|
|
|
var ws;
|
|
|
|
try {
|
|
|
|
ws = top.window.ws;
|
|
|
|
} catch (e) {}
|
|
|
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
|
|
ws.send("{'lv':true}");
|
|
|
|
} else {
|
|
|
|
ws = new WebSocket((window.location.protocol == "https:"?"wss":"ws")+"://"+document.location.host+"/ws");
|
|
|
|
ws.onopen = ()=>{
|
|
|
|
ws.send("{'lv':true}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.binaryType = "arraybuffer";
|
|
|
|
ws.addEventListener('message',(e)=>{
|
|
|
|
try {
|
|
|
|
if (toString.call(e.data) === '[object ArrayBuffer]') {
|
|
|
|
let leds = new Uint8Array(event.data);
|
2022-08-06 11:37:48 +02:00
|
|
|
if (leds[0] != 76 || !ctx) return; //'L', set in ws.cpp
|
|
|
|
if (!mW) { //only once as it doesn't change
|
|
|
|
mW = leds[2]; // matrix width
|
|
|
|
mH = leds[3]; // matrix height
|
|
|
|
pPL = Math.min(c.width / mW, c.height / mH); // pixels per LED (width of circle)
|
|
|
|
lOf = Math.floor((c.width - pPL*mW)/2); //left offeset (to center matrix)
|
|
|
|
}
|
|
|
|
var i = 4;
|
|
|
|
for (y=0.5;y<mH;y++) for (x=0.5; x<mW; x++) {
|
|
|
|
ctx.fillStyle = `rgb(${leds[i]},${leds[i+1]},${leds[i+2]})`;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.arc(x*pPL+lOf, y*pPL, pPL*0.4, 0, 2 * Math.PI);
|
|
|
|
ctx.fill();
|
|
|
|
i+=3;
|
|
|
|
}
|
2022-08-03 14:23:24 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Peek WS error:",err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// window.resize event listener
|
|
|
|
window.addEventListener('resize', (e)=>{
|
|
|
|
if (!throttled) { // only run if we're not throttled
|
|
|
|
setCanvas(); // actual callback action
|
|
|
|
throttled = true; // we're throttled!
|
|
|
|
setTimeout(()=>{ // set a timeout to un-throttle
|
|
|
|
throttled = false;
|
|
|
|
}, 250);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
2022-07-26 11:08:26 +02:00
|
|
|
</body>
|
|
|
|
</html>
|