animator.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>👾 Useless Robot Animator</title>
  7. <script src="./jquery.min.js"></script>
  8. <style>
  9. .circle {
  10. width: 20px;
  11. height: 20px;
  12. background-color: #525050;
  13. border-radius: 50%;
  14. display: inline-block;
  15. margin: 2px;
  16. cursor: pointer;
  17. }
  18. .circle.centerleft{
  19. border-right: 1px solid white;
  20. }
  21. .circle.centerright{
  22. border-left: 1px solid white;
  23. }
  24. .circle.active{
  25. background-color: #f55142;
  26. }
  27. #grid-container {
  28. display: flex;
  29. position: relative;
  30. flex-wrap: wrap;
  31. width: calc(32 * (20px + 2px * 2));
  32. }
  33. #dotmatrix{
  34. display: flex;
  35. flex-wrap: wrap;
  36. }
  37. html, body {
  38. height: 100%;
  39. margin: 0;
  40. display: flex;
  41. align-items: center;
  42. justify-content: center;
  43. background-color: #242424;
  44. font-family: Verdana, sans-serif;
  45. color: white;
  46. }
  47. .matrixModule{
  48. position: absolute;
  49. border: 1px solid #a3a3a3;
  50. width: calc(24px * 8);
  51. height: calc(24px * 8);
  52. pointer-events: none;
  53. }
  54. .matrixModule.one {
  55. top: 0;
  56. left: 0;
  57. }
  58. .matrixModule.two {
  59. top: 0;
  60. left: 192px;
  61. }
  62. .matrixModule.three {
  63. top: 0;
  64. left: 384px;
  65. }
  66. .matrixModule.four {
  67. top: 0;
  68. left: 576px;
  69. }
  70. .matrixModule.five {
  71. top: 192px;
  72. left: 0px;
  73. }
  74. .matrixModule.six {
  75. top: 192px;
  76. left: 192px;
  77. }
  78. .matrixModule.seven {
  79. top: 192px;
  80. left: 384px;
  81. }
  82. .matrixModule.eight {
  83. top: 192px;
  84. left: 576px;
  85. }
  86. button{
  87. background-color: white;
  88. color: #3b3b3b;
  89. border-radius: 0em;
  90. box-shadow: none;
  91. padding-top: 0.4em;
  92. padding-bottom: 0.4em;
  93. padding-left: 1em;
  94. padding-right: 1em;
  95. cursor: pointer;
  96. margin: 0.2em;
  97. margin-top: 0.6em;
  98. }
  99. .rotate-message {
  100. font-family: Arial, Helvetica, sans-serif;
  101. display: none;
  102. position: fixed;
  103. top: 0;
  104. left: 0;
  105. width: calc(100% - 1em);
  106. height: 100%;
  107. padding-left: 0.5em;
  108. background-color: #242424;
  109. color: white;
  110. display: flex;
  111. justify-content: center;
  112. align-items: center;
  113. font-size: 1.3em;
  114. text-align: center;
  115. z-index: 9999;
  116. }
  117. </style>
  118. </head>
  119. <body>
  120. <div id="grid-container">
  121. <div id="layout">
  122. <div class="matrixModule one"></div>
  123. <div class="matrixModule two"></div>
  124. <div class="matrixModule three"></div>
  125. <div class="matrixModule four"></div>
  126. <div class="matrixModule five"></div>
  127. <div class="matrixModule six"></div>
  128. <div class="matrixModule seven"></div>
  129. <div class="matrixModule eight"></div>
  130. </div>
  131. <div id="dotmatrix"></div>
  132. <br>
  133. <div style="width: 100%; padding: 1em; ">
  134. <div style="float: right;">
  135. <button onclick="clearScreen();">Clear</button>
  136. <button onclick="loadDefault();">Load Default</button>
  137. <button onclick="downloadBinary();">Export Binary</button>
  138. <button onclick="openFilePicker()">Import Binary</button>
  139. <button onclick="saveToSD();">Save to SD</button>
  140. <button onclick="exit()">Back</button>
  141. <input type="file" id="fileInput" style="display: none;" accept=".bin" onchange="handleFile(event)">
  142. </div>
  143. <div style="padding-top: 0.8em;">
  144. 👾 Robot Animator
  145. </div>
  146. </div>
  147. </div>
  148. <div id="requireLandscapeMode" class="rotate-message">
  149. <div>
  150. <p style="font-size: 2em;">(´・ω・`)</p>
  151. <p>Please rotate your device to landscape mode.</p>
  152. </div>
  153. </div>
  154. <script>
  155. let defaultTemplate = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  156. let previousSaveSDName = "";
  157. function generateCircleGrid(containerId) {
  158. const container = document.getElementById(containerId);
  159. if (!container) {
  160. console.error(`No element found with id "${containerId}"`);
  161. return;
  162. }
  163. // Clear the container if it already has content
  164. container.innerHTML = '';
  165. // Create a grid of circles
  166. const rows = 16;
  167. const cols = 32;
  168. for (let i = 0; i < rows; i++) {
  169. for (let j = 0; j < cols; j++) {
  170. $(container).append('<div class="circle" onClick="toggleThis(this);"></div>');
  171. }
  172. }
  173. }
  174. function getFrameAsIntArray(){
  175. let results = [];
  176. $(".circle").each(function(){
  177. let isActive = $(this).hasClass("active");
  178. if (isActive){
  179. results.push(1);
  180. }else{
  181. results.push(0);
  182. }
  183. });
  184. return results;
  185. }
  186. function drawFrameFromIntArray(frameArray){
  187. let counter = 0;
  188. $(".circle").each(function(){
  189. if (frameArray[counter] != undefined){
  190. let isActive = frameArray[counter] == 1;
  191. if (isActive){
  192. $(this).addClass('active');
  193. }else{
  194. $(this).removeClass('active');
  195. }
  196. }
  197. counter++;
  198. });
  199. }
  200. function loadDefault(){
  201. drawFrameFromIntArray(defaultTemplate);
  202. }
  203. function downloadBinary(){
  204. const bitArray = getFrameAsIntArray();
  205. const byteArray = [];
  206. for (let i = 0; i < bitArray.length; i += 8) {
  207. let byte = 0;
  208. for (let j = 0; j < 8; j++) {
  209. if (bitArray[i + j]) {
  210. byte |= 1 << (7 - j);
  211. }
  212. }
  213. byteArray.push(byte);
  214. }
  215. const blob = new Blob([new Uint8Array(byteArray)], { type: 'application/octet-stream' });
  216. const url = URL.createObjectURL(blob);
  217. const a = document.createElement('a');
  218. a.href = url;
  219. a.download = 'frame.bin';
  220. document.body.appendChild(a);
  221. a.click();
  222. document.body.removeChild(a);
  223. URL.revokeObjectURL(url);
  224. }
  225. function toggleThis(led){
  226. if (!$(led).hasClass('active')){
  227. $(led).addClass('active');
  228. }else{
  229. $(led).removeClass('active');
  230. }
  231. }
  232. // Call the function to generate the grid
  233. generateCircleGrid('dotmatrix');
  234. function clearScreen(){
  235. $(".circle.active").removeClass("active");
  236. }
  237. function exit(){
  238. window.location.href = "index.html";
  239. }
  240. //Import functions
  241. function openFilePicker() {
  242. document.getElementById('fileInput').click();
  243. }
  244. function handleFile(event) {
  245. const file = event.target.files[0];
  246. if (file) {
  247. const reader = new FileReader();
  248. reader.onload = function(e) {
  249. const arrayBuffer = e.target.result;
  250. const byteArray = new Uint8Array(arrayBuffer);
  251. const bitArray = [];
  252. for (let byte of byteArray) {
  253. for (let i = 7; i >= 0; i--) {
  254. bitArray.push((byte >> i) & 1);
  255. }
  256. }
  257. renderBitArray(bitArray);
  258. };
  259. reader.readAsArrayBuffer(file);
  260. }
  261. }
  262. function renderBitArray(bitArray) {
  263. drawFrameFromIntArray(bitArray);
  264. }
  265. /* RWD and mobile interface */
  266. let isMobile = false;
  267. if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  268. isMobile = true;
  269. }
  270. function calculateScale(windowWidth, windowHeight, minWidth=768, minHeight=460) {
  271. // Calculate the scale ratios for both width and height
  272. const widthRatio = windowWidth / minWidth;
  273. const heightRatio = windowHeight / minHeight;
  274. // Choose the smaller ratio to ensure the entire div fits within the screen
  275. return Math.min(widthRatio, heightRatio);
  276. }
  277. function updateRWD(){
  278. if (isMobile){
  279. if (window.innerHeight > window.innerWidth){
  280. $("#grid-container").hide();
  281. $("#requireLandscapeMode").show();
  282. }else{
  283. $("#grid-container").show();
  284. $("#requireLandscapeMode").hide();
  285. }
  286. //Scale down the grids to size
  287. //768 * 460
  288. const scale = calculateScale(window.innerWidth, window.innerHeight - 24);
  289. $("#grid-container").css("scale", scale);
  290. $("#grid-container").css("margin-top", "24px");
  291. }
  292. }
  293. if (isMobile){
  294. updateRWD();
  295. }else{
  296. $("#requireLandscapeMode").hide();
  297. }
  298. $(window).on("resize", function(){
  299. updateRWD();
  300. })
  301. function saveToSD(){
  302. let anicode = prompt("Emoji animation code (1 char + 1 digit only, e.g. \"k0\")", previousSaveSDName);
  303. if (anicode != null) {
  304. previousSaveSDName = anicode;
  305. const bitArray = getFrameAsIntArray();
  306. const byteArray = [];
  307. for (let i = 0; i < bitArray.length; i += 8) {
  308. let byte = 0;
  309. for (let j = 0; j < 8; j++) {
  310. if (bitArray[i + j]) {
  311. byte |= 1 << (7 - j);
  312. }
  313. }
  314. byteArray.push(byte);
  315. }
  316. const blob = new Blob([new Uint8Array(byteArray)], { type: 'application/octet-stream' });
  317. const anifile = new File([blob], anicode + ".bin");
  318. uploadFiles([anifile])
  319. }
  320. }
  321. //Upload file to server
  322. function uploadFiles(files) {
  323. if (files.length === 0) {
  324. alert('No files selected.');
  325. return;
  326. }
  327. // Create a FormData object to send files as multipart/form-data
  328. var formData = new FormData();
  329. // Append each file to the FormData object
  330. for (var i = 0; i < files.length; i++) {
  331. formData.append('files[]', files[i]);
  332. }
  333. // Send the FormData object via XMLHttpRequest
  334. var xhr = new XMLHttpRequest();
  335. xhr.open('POST', '/upload?dir=/anime', true); // Replace '/upload' with your ESP32 server endpoint
  336. // Track upload progress
  337. xhr.upload.addEventListener('progress', function(event) {
  338. if (event.lengthComputable) {
  339. var percentComplete = (event.loaded / event.total) * 100;
  340. console.log('Upload progress: ' + percentComplete.toFixed(2) + '%');
  341. } else {
  342. console.log('Upload progress: unknown');
  343. }
  344. });
  345. xhr.onload = function() {
  346. if (xhr.status === 200) {
  347. alert('Animation frame saved');
  348. $.get(`/api/ctr/emoji?anicode=${previousSaveSDName}`);
  349. } else {
  350. alert('Error writing files to disk.');
  351. }
  352. };
  353. xhr.onerror = function() {
  354. alert('Error uploading files.');
  355. };
  356. xhr.send(formData);
  357. }
  358. </script>
  359. </body>
  360. </html>