Remove legacy CSS gradient code from liveview.htm

Reduce file size by removing legacy CSS gradient code and moving drawing operations to a separate function
This commit is contained in:
zanhecht 2024-01-09 19:27:45 -05:00 committed by GitHub
parent 1333c41811
commit 0a1d82de2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,28 +6,32 @@
<meta name="theme-color" content="#222222"> <meta name="theme-color" content="#222222">
<title>WLED Live Preview</title> <title>WLED Live Preview</title>
<style> <style>
body { body {
margin: 0; margin: 0;
} }
#canv { #canv {
background: black; background: black;
filter: brightness(175%); filter: brightness(175%);
width: 100%; width: 100%;
height: 100%; height: 100%;
position: absolute; position: absolute;
} }
#canvas {
width: 100%;
height: 100%;
position: absolute;
}
</style> </style>
<script> <script>
var d = document; var d = document;
var ws; var ws;
var tmout = null; var tmout = null;
function update() // via HTTP (/json/live) var c;
{ var ctx;
function draw(start, skip, leds, fill) {
c.width = d.documentElement.clientWidth;
w = c.width / (leds.length - start);
for (i = start; i < leds.length; i += skip) {
ctx.fillStyle = fill(leds,i);
ctx.fillRect((i - start) * w, 0, 4 * w, c.height);
}
}
function update() { // via HTTP (/json/live)
if (d.hidden) { if (d.hidden) {
clearTimeout(tmout); clearTimeout(tmout);
tmout = setTimeout(update, 250); tmout = setTimeout(update, 250);
@ -42,25 +46,7 @@
return res.json(); return res.json();
}) })
.then(json => { .then(json => {
var leddata = (data) => "#" + ((data.length > 6) ? data.substring(2) : data); draw(0, 1, json.leds, (a,i) => "#" + ((a[i].length > 6) ? a[i].substring(2) : a[i]));
var len = json.leds.length;
try { //canvas support
var c = d.getElementById('canvas');
var ctx = c.getContext('2d');
c.width = d.documentElement.clientWidth;
ctx.clearRect(0, 0, c.width, c.height);
for (i = 0; i < len; i++) {
ctx.fillStyle = leddata(json.leds[i]);
ctx.fillRect(i * c.width / len, 0, c.width, c.height);
}
} catch (e) { //fallback to gradient method
var str = "linear-gradient(90deg,";
for (i = 0; i < len; i++) {
str += leddata(json.leds[i]) + ((i < len -1) ? "," : "");
}
str += ")";
d.getElementById("canv").style.background = str;
}
clearTimeout(tmout); clearTimeout(tmout);
tmout = setTimeout(update, 40); tmout = setTimeout(update, 40);
}) })
@ -71,8 +57,9 @@
}) })
} }
function S() { // Startup function (onload) function S() { // Startup function (onload)
let wsOn = (window.location.href.indexOf("?ws") > 0); c = d.getElementById('canv');
if (!wsOn) {update(); return;} ctx = c.getContext('2d');
if (window.location.href.indexOf("?ws") == -1) {update(); return;}
// Initialize WebSocket connection // Initialize WebSocket connection
try { try {
@ -102,26 +89,8 @@
if (toString.call(e.data) === '[object ArrayBuffer]') { if (toString.call(e.data) === '[object ArrayBuffer]') {
let leds = new Uint8Array(event.data); let leds = new Uint8Array(event.data);
if (leds[0] != 76) return; //'L' if (leds[0] != 76) return; //'L'
let len = leds.length; // leds[1] = 1: 1D; leds[1] = 2: 1D/2D (leds[2]=w, leds[3]=h)
let start = leds[1]==2 ? 4 : 2; // 1 = 1D, 2 = 1D/2D (leds[2]=w, leds[3]=h) draw(leds[1]==2 ? 4 : 2, 3, leds, (a,i) => `rgb(${a[i]},${a[i+1]},${a[i+2]})`);
let rgb = (i) => `rgb(${leds[i]},${leds[i+1]},${leds[i+2]})`;
try { //canvas support
var c = d.getElementById('canvas');
var ctx = c.getContext('2d');
c.width = d.documentElement.clientWidth;
ctx.clearRect(0, 0, c.width, c.height);
for (i = start; i < len; i+=3) {
ctx.fillStyle = rgb(i);
ctx.fillRect((i - start) * c.width / (len - start), 0, c.width, c.height);
}
} catch (e) { //fallback to gradient method
let str = "linear-gradient(90deg,";
for (i = start; i < len; i+=3) {
str += rgb(i) + ((i < len -3) ? "," : "");
}
str += ")";
d.getElementById("canv").style.background = str;
}
} }
} catch (err) { } catch (err) {
console.error("Peek WS error:",err); console.error("Peek WS error:",err);
@ -131,6 +100,6 @@
</script> </script>
</head> </head>
<body onload="S()"> <body onload="S()">
<div id="canv"><canvas id="canvas"></canvas></div> <canvas id="canv"></canvas>
</body> </body>
</html> </html>