WLED/wled00/data/liveviewws2D.htm

81 lines
2.4 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
<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
if (c.getContext) {
// Access the rendering context
var ctx = c.getContext('2d');
function drawCircle(x, y, r, g, b, a) {
ctx.fillStyle = `rgb(${r},${g},${b})`;
ctx.beginPath();
ctx.arc(x*pPL+pPL*0.5+lOf, y*pPL+pPL*0.5, pPL*0.4, 0, 2 * Math.PI);
ctx.fill();
}
// 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);
if (leds[0] != 76) return; //'L', set in ws.cpp
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)
for (i = 4; i < leds.length; i+=3) {
let ff = (i-4)/3; // pixel index
drawCircle(ff%mW, Math.floor(ff/mW), leds[i], leds[i+1], leds[i+2], 255);
}
}
} 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>
</body>
</html>