 901d56f898
			
		
	
	901d56f898
	
	
	
		
			
			Allows for "pixel perfect" liveview instead of diffused view, to better match what's shown at https://kno.wled.ge/features/effects/. This will make it easier to see what the LEDs are doing, although it may not be as accurate a representation for installations with diffused LEDs. Includes fallback to CSS gradient method for browsers that don't support canvas.
		
			
				
	
	
		
			138 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			4.2 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <!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;
 | |
|   }
 | |
|   #canv {
 | |
|     background: black;
 | |
|     filter: brightness(175%);
 | |
|     width: 100%;
 | |
|     height: 100%;
 | |
|     position: absolute;
 | |
|   }
 | |
|   #canvas {
 | |
|     width: 100%;
 | |
|     height: 100%;
 | |
|     position: absolute;
 | |
|   }
 | |
|   </style>
 | |
|   <script>
 | |
|     var ws;
 | |
|     var c = document.getElementById('canvas');
 | |
|     var ctx = c.getContext('2d');
 | |
|     var tmout = null;
 | |
|     function setCanvas() {
 | |
|       c.width  = document.documentElement.clientWidth;
 | |
|     }
 | |
|     setCanvas();
 | |
|     function update() // via HTTP (/json/live)
 | |
|     {
 | |
|       if (document.hidden) {
 | |
|         clearTimeout(tmout);
 | |
|         tmout = setTimeout(update, 250);
 | |
|         return;
 | |
|       }
 | |
|       fetch('./json/live')
 | |
|       .then(res => {
 | |
|         if (!res.ok) {
 | |
|           clearTimeout(tmout);
 | |
|           tmout = setTimeout(update, 2500);
 | |
|         }
 | |
|         return res.json();
 | |
|       })
 | |
|       .then(json => {
 | |
|         var leddata = (data) => "#" + ((data.length > 6) ? data.substring(2) : data);
 | |
|         var str = "linear-gradient(90deg,";
 | |
|         var len = json.leds.length;
 | |
|         if (typeof ctx === "object") { //canvas support
 | |
|           setCanvas();
 | |
|           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);
 | |
|           }
 | |
|         } else { //fallback to gradient method
 | |
|           for (i = 0; i < len; i++) {
 | |
|             str += leddata(json.leds[i]) + ((i < len -1) ? "," : "");
 | |
|           }
 | |
|           str += ")";
 | |
|           document.getElementById("canv").style.background = str;
 | |
|         }
 | |
|         clearTimeout(tmout);
 | |
|         tmout = setTimeout(update, 40);
 | |
|       })
 | |
|       .catch(function (error) {
 | |
|         console.error("Peek HTTP error:",error);
 | |
|         clearTimeout(tmout);
 | |
|         tmout = setTimeout(update, 2500);
 | |
|       })
 | |
|     }
 | |
|     function S() { // Startup function (onload)
 | |
|       let wsOn = (window.location.href.indexOf("?ws") > 0);
 | |
|       if (!wsOn) {update(); return;}
 | |
| 
 | |
|       // Initialize WebSocket connection
 | |
|       try {
 | |
|         ws = top.window.ws;
 | |
|       } catch (e) {}
 | |
|       if (ws && ws.readyState === WebSocket.OPEN) {
 | |
|         //console.info("Peek uses top WS");
 | |
|         ws.send("{'lv':true}");
 | |
|       } else {
 | |
|         //console.info("Peek WS opening");
 | |
|         let l = window.location;
 | |
|         let pathn = l.pathname;
 | |
|         let paths = pathn.slice(1,pathn.endsWith('/')?-1:undefined).split("/");
 | |
|         let url = l.origin.replace("http","ws");
 | |
|         if (paths.length > 1) {
 | |
|           url +=  "/" + paths[0];
 | |
|         }
 | |
|         ws = new WebSocket(url+"/ws");
 | |
|         ws.onopen = function () {
 | |
|           //console.info("Peek WS open");
 | |
|           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'
 | |
|             let len = leds.length;
 | |
|             let start = leds[1]==2 ? 4 : 2; // 1 = 1D, 2 = 1D/2D (leds[2]=w, leds[3]=h)
 | |
|             let rgb = (i) => `rgb(${leds[i]},${leds[i+1]},${leds[i+2]})`;
 | |
|             if (typeof ctx === "object") { //canvas support
 | |
|               setCanvas();
 | |
|               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);
 | |
|               }
 | |
|             } else { //fallback to gradient method
 | |
|               let str = "linear-gradient(90deg,";
 | |
|               for (i = start; i < len; i+=3) {
 | |
|                 str += rgb(i) + ((i < len -3) ? "," : "");
 | |
|               }
 | |
|               str += ")";
 | |
|               document.getElementById("canv").style.background = str;
 | |
|             }
 | |
|           }
 | |
|         } catch (err) {
 | |
|           console.error("Peek WS error:",err);
 | |
|         } 
 | |
|       });
 | |
|     }
 | |
|   </script>
 | |
| </head>
 | |
| <body onload="S()">
 | |
|   <div id="canv"><canvas id="canvas"></canvas></div>
 | |
| </body>
 | |
| </html>
 |