led-matrix.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=0.67">
  6. <title>LED Matrix</title>
  7. <style>
  8. #colorPicker {
  9. padding: 4px;
  10. background: #000;
  11. }
  12. #ledMatrix {
  13. width: 320px;
  14. height: 192px;
  15. padding: 16px;
  16. -webkit-border-radius:8px;
  17. -moz-border-radius: 8px;
  18. border-radius: 8px;
  19. background: #C0C0C0;
  20. }
  21. .Table {
  22. display: table;
  23. margin: 2px;
  24. }
  25. .Row {
  26. display: table-row;
  27. }
  28. .Cell {
  29. display: table-cell;
  30. }
  31. .Color {
  32. width: 20px;
  33. height: 20px;
  34. margin: 2px;
  35. }
  36. .Circle {
  37. width: 16px;
  38. height: 16px;
  39. margin: 8px;
  40. -webkit-border-radius: 8px;
  41. -moz-border-radius: 8px;
  42. border-radius: 8px;
  43. background: #333;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <h1>LED Matrix</h1>
  49. <div class="Table">
  50. <div class="Row">
  51. <div class="Cell">
  52. Color: <input type="text" id="colorText" size="10" value="#FFF" disabled>
  53. <div id="colorPicker" class="Table"></div>
  54. </div>
  55. <div class="Cell">
  56. <button id="btnSend">Send dots color to ESP-MINTIA</button>
  57. <div id="ledMatrix" class="Table"></div>
  58. </div>
  59. </div>
  60. </div>
  61. <div id="dataOutput"></div>
  62. <div id="txtResponse"></div>
  63. <script>
  64. var w = 10;
  65. var h = 6;
  66. var colorMin = 3;
  67. var colorMax = 15;
  68. var xhr;
  69. function getRGB(e) {
  70. return window.getComputedStyle(e).backgroundColor.match(/\d+/g).map(function(a){ return Math.round(parseInt(a, 10) / 16.5); });
  71. }
  72. function getColor(e) {
  73. var ct = document.getElementById("colorText");
  74. var rgb = getRGB(e);
  75. ct.value = "#" + rgb[0].toString(16) + rgb[1].toString(16) + rgb[2].toString(16);
  76. ct.style.backgroundColor = ct.value;
  77. }
  78. function setColor(e) {
  79. var ct = document.getElementById("colorText");
  80. e.style.backgroundColor = ct.value;
  81. sendData(e);
  82. }
  83. function sendData() {
  84. var o = document.getElementById("dataOutput");
  85. var text = "";
  86. for (var i = 0; i < (h * w); i++) {
  87. var d = document.getElementById("dot_" + i);
  88. var rgb = getRGB(d);
  89. text += String.fromCharCode(rgb[1] - colorMin);
  90. text += String.fromCharCode(rgb[0] - colorMin);
  91. text += String.fromCharCode(rgb[2] - colorMin);
  92. }
  93. text = "data=" + escape(text)
  94. o.innerHTML = text;
  95. if (!xhr) xhr = new XMLHttpRequest();
  96. xhr.open("POST", "led-matrix.lc", true);
  97. xhr.onreadystatechange=function() {
  98. if (xhr.readyState === 4) {
  99. var r = document.getElementById("txtResponse");
  100. r.innerHTML = xhr.responseText;
  101. }
  102. }
  103. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  104. xhr.send(text);
  105. }
  106. var cp = document.getElementById("colorPicker");
  107. var colorHtml = "";
  108. var i = 0;
  109. for (var r = colorMin; r <= colorMax; r+=6) {
  110. for (var g = colorMin; g <= colorMax; g+=6) {
  111. if ((g % 6) == 3) colorHtml += '<div calss="Row">';
  112. for (var b = colorMin; b <= colorMax; b+=2) {
  113. i++;
  114. colorHtml += '<div class="Cell"><div class="Color" id="color' + i + '" style="background:#' + r.toString(16) + g.toString(16) + b.toString(16) + ';"></div></div>';
  115. }
  116. if ((g % 6) == 3) colorHtml += '</div>';
  117. }
  118. }
  119. cp.innerHTML = colorHtml;
  120. for (var j = 1; j <= i; j++) {
  121. var c = document.getElementById("color" + j);
  122. c.addEventListener("click", function(e) {
  123. getColor(e.target||e.srcElement);
  124. });
  125. }
  126. var lm = document.getElementById("ledMatrix");
  127. var dotHtml = "";
  128. for (var i = 0; i < (h * w); i++) {
  129. if ((i % w) == 0) dotHtml += '<div calss="Row">';
  130. dotHtml += '<div class="Cell"><div class="Circle" id="dot_' + i + '"></div></div>';
  131. if ((i % w) == (w - 1)) dotHtml += '</div>';
  132. }
  133. lm.innerHTML = dotHtml;
  134. for (var i = 0; i < (h * w); i++) {
  135. var d = document.getElementById("dot_" + i);
  136. d.addEventListener("click", function(e) {
  137. setColor(e.target||e.srcElement);
  138. });
  139. }
  140. document.getElementById("btnSend").addEventListener("click", sendData);
  141. </script>
  142. </body>
  143. </html>