//Start up code document.getElementById('curlUrl').value = location.host; let devMode = false; const urlParams = new URLSearchParams(window.location.search); if(urlParams.has('dev')){ devMode = true; } if(devMode){ console.log('Developer mode active. Experimental and unstable functions active.') } else{ console.log('Developer mode inactive. Append "?dev" to the URL.') } if(devMode){ document.getElementById("fileJSONledbutton").style.display = 'buttonclass' document.getElementById("gap2").style.display = 'gap' } else { document.getElementById("fileJSONledbutton").style.display = 'none' document.getElementById("gap2").style.display = 'none' } let httpArray = []; let fileJSON = ''; //On submit button pressed ======================= document.getElementById("convertbutton").addEventListener("click", function() { let base64Image = document.getElementById('preview').src; if (isValidBase64Gif(base64Image)) { document.getElementById('image').src = base64Image; getPixelRGBValues(base64Image); document.getElementById('image-container').style.display = "block" document.getElementById("button-container").style.display = ""; } else { let infoDiv = document.getElementById('image-info'); let imageInfo = '

WARNING! File does not appear to be a valid image

' infoDiv.innerHTML = imageInfo; infoDiv.style.display = "block" document.getElementById('image-container').style.display = "none"; document.getElementById('JSONled').value = ''; console.log("The string '" + base64Image + "' is not a valid base64 image."); } }); // Code for copying the generated string to clipboard copyJSONledbutton.addEventListener('click', async () => { let JSONled = document.getElementById('JSONled'); JSONled.select(); try { await navigator.clipboard.writeText(JSONled.value); } catch (err) { try { await document.execCommand("copy"); } catch (err) { console.error('Failed to copy text: ', err); } } }); sendJSONledbutton.addEventListener('click', async () => { if (window.location.protocol === "https:") { alert('Will only be available when served over http (or WLED is run over https)'); } else { postPixels(); } }); fileJSONledbutton.addEventListener('click', async () => { if (window.location.protocol === "https:") { alert('Will only be available when served over http (or WLED is run over https)'); } else { let JSONFileName = 'TheName.json' let urlString = 'http://'+document.getElementById('curlUrl').value+'/upload' sendAsFile(fileJSON, JSONFileName, urlString); } }); async function postPixels() { for (let i of httpArray) { try { console.log(i); console.log(i.length); const response = await fetch('http://'+document.getElementById('curlUrl').value+'/json/state', { method: 'POST', headers: { 'Content-Type': 'application/json' //'Content-Type': 'text/html; charset=UTF-8' }, body: i }); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } } } //File uploader code const dropZone = document.getElementById('drop-zone'); const filePicker = document.getElementById('file-picker'); const preview = document.getElementById('preview'); // Listen for dragenter, dragover, and drop events dropZone.addEventListener('dragenter', dragEnter); dropZone.addEventListener('dragover', dragOver); dropZone.addEventListener('drop', dropped); dropZone.addEventListener('click', zoneClicked); // Listen for change event on file picker filePicker.addEventListener('change', filePicked); // Handle zone click function zoneClicked(e) { e.preventDefault(); //this.classList.add('drag-over'); //alert('Hej'); filePicker.click(); } // Handle dragenter function dragEnter(e) { e.preventDefault(); this.classList.add('drag-over'); } // Handle dragover function dragOver(e) { e.preventDefault(); } // Handle drop function dropped(e) { e.preventDefault(); this.classList.remove('drag-over'); // Get the dropped file const file = e.dataTransfer.files[0]; updatePreview(file); } // Handle file picked function filePicked(e) { // Get the picked file const file = e.target.files[0]; updatePreview(file); } // Update the preview image function updatePreview(file) { // Use FileReader to read the file const reader = new FileReader(); reader.onload = function() { // Update the preview image preview.src = reader.result; document.getElementById("submitConvertDiv").style.display = ""; }; reader.readAsDataURL(file); } function isValidBase64Gif(string) { // Use a regular expression to check that the string is a valid base64 string /* const base64gifPattern = /^data:image\/gif;base64,([A-Za-z0-9+/:]{4})*([A-Za-z0-9+/:]{3}=|[A-Za-z0-9+/:]{2}==)?$/; const base64pngPattern = /^data:image\/png;base64,([A-Za-z0-9+/:]{4})*([A-Za-z0-9+/:]{3}=|[A-Za-z0-9+/:]{2}==)?$/; const base64jpgPattern = /^data:image\/jpg;base64,([A-Za-z0-9+/:]{4})*([A-Za-z0-9+/:]{3}=|[A-Za-z0-9+/:]{2}==)?$/; const base64webpPattern = /^data:image\/webp;base64,([A-Za-z0-9+/:]{4})*([A-Za-z0-9+/:]{3}=|[A-Za-z0-9+/:]{2}==)?$/; */ //REMOVED, Any image appear to work as long as it can be drawn to the canvas. Leaving code in for future use, possibly if (1==1 || base64gifPattern.test(string) || base64pngPattern.test(string) || base64jpgPattern.test(string) || base64webpPattern.test(string)) { return true; } else { //Not OK return false; } } document.getElementById("brightnessNumber").oninput = function() { document.getElementById("brightnessValue").textContent = this.value; } document.getElementById("colorLimitNumber").oninput = function() { document.getElementById("colorLimitValue").textContent = this.value; } var formatSelector = document.getElementById("formatSelector"); var hideableRows = document.querySelectorAll(".ha-hide"); for (var i = 0; i < hideableRows.length; i++) { hideableRows[i].classList.add("hide"); } formatSelector.addEventListener("change", function() { for (var i = 0; i < hideableRows.length; i++) { hideableRows[i].classList.toggle("hide", this.value !== "ha"); } }); function switchScale() { let scalePath = document.getElementById("scalePath"); let scaleTogglePath = document.getElementById("scaleTogglePath"); let color = scalePath.getAttribute("fill"); let d = '' if (color === accentColor) { color = accentTextColor; d = scaleToggleOffd document.getElementById("sizeDiv").style.display = "none"; // Set values to actual XY of image, if possible } else { color = accentColor; d = scaleToggleOnd document.getElementById("sizeDiv").style.display = ""; } scalePath.setAttribute("fill", color); scaleTogglePath.setAttribute("fill", color); scaleTogglePath.setAttribute("d", d); } function sendAsFile(jsonStringInput, fileName, urlString) { //var jsonString = JSON.stringify({name: "value"}); var file = new Blob([jsonStringInput], {type: 'application/json'}); console.log(jsonStringInput) console.log(fileName); console.log(urlString); var formData = new FormData(); formData.append('file', file, fileName); var xhr = new XMLHttpRequest(); xhr.open('POST', urlString, true); xhr.onload = function() { if (xhr.status === 200) { console.log('File uploaded successfully!'); } else { console.log('File upload failed!'); } }; xhr.send(formData); } function generateSegmentOptions(array) { //This function is prepared for a name property on each segment for easier selection //Currently the name is generated generically based on index var select = document.getElementById("targetSegment"); select.innerHTML = ""; for (var i = 0; i < array.length; i++) { var option = document.createElement("option"); option.value = array[i].value; option.text = array[i].text; select.appendChild(option); if(i === 0) { option.selected = true; } } } //Initial population of segment selection function generateSegmentArray(noOfSegments) { var arr = []; for (var i = 0; i < noOfSegments; i++) { arr.push({ value: i, text: "Segment index " + i }); } return arr; } //Animate matrix var matrixcircles = document.querySelectorAll("#logomatrix path"); var intervalId; // Function that changes the color of a random circle to a lighter purple function changeColorOfDot() { // Get a random number between 0 and the number of circles var randomIndex = Math.floor(Math.random() * matrixcircles.length); // Get the circle at the random index var randomCircle = matrixcircles[randomIndex]; // Store the current fill color var currentColor = randomCircle.getAttribute("fill"); // Change the color of the circle randomCircle.setAttribute("fill", "#bb8fbc"); setTimeout(() => { randomCircle.setAttribute("fill", currentColor); clearInterval(intervalId); intervalId = setInterval(changeColorOfDot, randomInterval()); }, 500); } function randomInterval() { var interval = Math.floor(Math.random() * (5 - 1 + 1)) + 1; return interval * 1000; } // call the function changeColorOfDot every 10 seconds intervalId = setInterval(changeColorOfDot, randomInterval()); var segmentData = generateSegmentArray(10); generateSegmentOptions(segmentData); document.getElementById("fileJSONledbutton").innerHTML = '  File to device' document.getElementById("convertbutton").innerHTML = '   Convert to WLED JSON '; document.getElementById("copyJSONledbutton").innerHTML = '   Copy to clipboard'; document.getElementById("sendJSONledbutton").innerHTML = '   Send to device';