Advertisement
harraps

microm.js

Apr 18th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*\
  2. |*|   ::::    ::::  ::::::::::: ::::::::  :::::::::   ::::::::  ::::    ::::  
  3. |*|   +:+:+: :+:+:+     :+:    :+:    :+: :+:    :+: :+:    :+: +:+:+: :+:+:+
  4. |*|   +:+ +:+:+ +:+     +:+    +:+        +:+    +:+ +:+    +:+ +:+ +:+:+ +:+
  5. |*|   +#+  +:+  +#+     +#+    +#+        +#++:++#:  +#+    +:+ +#+  +:+  +#+
  6. |*|   +#+       +#+     +#+    +#+        +#+    +#+ +#+    +#+ +#+       +#+
  7. |*|   #+#       #+#     #+#    #+#    #+# #+#    #+# #+#    #+# #+#       #+#
  8. |*|   ###       ### ########### ########  ###    ###  ########  ###       ###
  9. \*/
  10.  
  11. /**
  12.  *
  13.  * @author Olivier Schyns
  14.  *
  15.  * Microm is a litle javascript file that allow you to generate a matrix of virtual LED
  16.  * that you can control with a virtual keyboard.
  17.  *
  18.  * To start, use the initDisplay and initKeyboard functions to create the display and the keyboard.
  19.  *
  20.  */
  21.  
  22.  
  23. var microm = {};
  24. microm.cell = {};
  25. microm.input = {};
  26.  
  27. // width of the display
  28. microm.width;
  29.  
  30. // height of the display
  31. microm.height;
  32.  
  33. // the default color of the screen
  34. microm.defaultColor = "#111";
  35.  
  36. // microm's colors preset
  37. microm.red = "#c71313";
  38. microm.orange = "#f58708";
  39. microm.yellow = "#e6c614";
  40. microm.lime = "#b5e514";
  41. microm.green = "#1aa21a";
  42. microm.pine = "#19ce66";
  43. microm.cyan = "#1bd4d4";
  44. microm.blue = "#1c7ed4";
  45. microm.colbat = "#1111b7";
  46. microm.violet = "#3617de";
  47. microm.purple = "#781fc6";
  48. microm.magenta = "#c61cc6";
  49. microm.crimson = "#d60b77";
  50.  
  51. microm.black = "#000000";
  52. microm.dark = "#2c2c2c";
  53. microm.gray = "#606060";
  54. microm.light = "#a2a2a2";
  55. microm.white = "#ebebeb";
  56.  
  57. microm.pink = "#de8ba4";
  58. microm.brown = "#572f12";
  59. microm.beige = "#dda670";
  60. microm.sea = "#071143";
  61.  
  62. /**
  63.  * Generate a virtual screen of LED
  64.  * @param {string} display_id the id of the div in which we want to put our display
  65.  * @param {int}    width      the width of the screen we want to make
  66.  * @param {int}    height     the height of the screen we want to make
  67.  */
  68. microm.initDisplay = function( display_id, width, height ){
  69.    
  70.     microm.width = width;
  71.     microm.height = height;
  72.    
  73.     microm.cell = new Array(width);
  74.     for( var i=0; i<width; ++i ){
  75.         microm.cell[i] = new Array(height);
  76.     }
  77.    
  78.     var div_display = document.getElementById(display_id);
  79.     var table = document.createElement('table');
  80.     div_display.appendChild(table);
  81.    
  82.     for( var i=0; i<height; ++i ){
  83.        
  84.         var tr = document.createElement('tr');
  85.         table.appendChild(tr);
  86.        
  87.         for( var j=0; j<width; ++j ){
  88.            
  89.             var td = document.createElement('td');
  90.             microm.cell[j][i] = td;
  91.             tr.appendChild(td);
  92.         }
  93.     }
  94. };
  95.  
  96. /**
  97.  * Generate a virtual keyboard
  98.  * @param {string} keyboard_id the id of the div in which we want to put our keyboard
  99.  * @param {array}  keys_array  a array of arrays of string, each string will be the name of a button
  100.  */
  101. microm.initKeyboard = function( keyboard_id, keys_array ){
  102.    
  103.     var div_keyboard = document.getElementById(keyboard_id);
  104.     var table = document.createElement('table');
  105.     div_keyboard.appendChild(table);
  106.    
  107.     for( var i=0; i<keys_array.length; ++i ){
  108.        
  109.         var tr = document.createElement('tr');
  110.         table.appendChild(tr);
  111.        
  112.         for( var j=0; j<keys_array[i].length; ++j ){
  113.            
  114.             var td = document.createElement('td');
  115.             tr.appendChild(td);
  116.            
  117.             if( keys_array[i][j] != null ){
  118.                 var btn = document.createElement('button');
  119.                 btn.innerHTML = keys_array[i][j];
  120.                 microm.input[""+keys_array[i][j]] = btn;
  121.                 td.appendChild(btn);
  122.             }
  123.         }
  124.     }
  125. };
  126.  
  127. /**
  128.  * Set the color of the targeted LED
  129.  * @param {int}    x     position of the LED on the x-axis
  130.  * @param {int}    y     position of the LED on the y-axis
  131.  * @param {string} color the color we want to give to the LED
  132.  */
  133. microm.set = function( x, y, color ){
  134.     x = ~~x;
  135.     y = ~~y;
  136.     if( x >= 0 && x < microm.cell.length ){
  137.         if( y >= 0 && y < microm.cell[x].length ){
  138.             microm.cell[x][y].style = "background:".concat(color);
  139.         }
  140.     }
  141. };
  142.  
  143. /**
  144.  * Reset the color of the targeted LED
  145.  * @param {int}    x     position of the LED on the x-axis
  146.  * @param {int}    y     position of the LED on the y-axis
  147.  */
  148. microm.reset = function( x, y ){
  149.     microm.set( x, y, microm.defaultColor );
  150. };
  151.  
  152. /**
  153.  * Fill a rectangle in the screen with the given color
  154.  * @param {int}    x      position of the top-left corner on the x-axis
  155.  * @param {int}    y      position of the top-left corner on the y-axis
  156.  * @param {int}    width  width of the rectangle
  157.  * @param {int}    height height of the rectangle
  158.  * @param {string} color  the color of the rectangle
  159.  */
  160. microm.drawRect = function( x, y, width, height, color ){
  161.    
  162.     var w = x+width;
  163.     if( w > microm.cell.length ){
  164.         w = microm.cell.length;
  165.     }
  166.     for( var i=x; i<w; ++i ){
  167.        
  168.         var h = y+height;
  169.         if( h > microm.cell[i].length ){
  170.             h = microm.cell[i].length;
  171.         }
  172.         for( var j=y; j<h; ++j ){
  173.            
  174.             microm.set( i, j, color );
  175.         }
  176.     }
  177. };
  178.  
  179. /**
  180.  * Paint the full screen with the given color
  181.  * @param {string} color the color to paint the screen
  182.  */
  183. microm.paint = function( color ){
  184.     for( var i=0; i<microm.cell.length; ++i ){
  185.         for( var j=0; j<microm.cell[i].length; ++j ){
  186.             microm.cell[i][j].style = "background:".concat( color );
  187.         }
  188.     }
  189. };
  190.  
  191. /**
  192.  * Reset the color of the full screen to the default color
  193.  */
  194. microm.clear = function(){
  195.     for( var i=0; i<microm.cell.length; ++i ){
  196.         for( var j=0; j<microm.cell[i].length; ++j ){
  197.             microm.cell[i][j].style = "background:".concat( microm.defaultColor );
  198.         }
  199.     }
  200. };
  201.  
  202. /**
  203.  * Apply a regular stamp at the given position
  204.  * @param {int}   x     position of the top-left corner on the x-axis
  205.  * @param {int}   y     position of the top-left corner on the y-axis
  206.  * @param {array} stamp an array of arrays of color codes
  207.  */
  208. microm.stamp = function( x, y, stamp ){
  209.     x = ~~x;
  210.     y = ~~y;
  211.    
  212.     var w = microm.getStampWidth(stamp);
  213.     if( w+x > microm.cell.length ){
  214.         w = microm.cell.length - x;
  215.     }
  216.     for( var i=0; i<w; ++i ){
  217.        
  218.         if( i+x >= 0 ){
  219.             var h = stamp.length;
  220.             if( h+y > microm.cell[i+x].length ){
  221.                 h = microm.cell[i+x].length - y;
  222.             }
  223.             for( var j=0; j<h; ++j ){
  224.                 if( stamp[j][i] != null ){
  225.                     microm.set( i+x, j+y, stamp[j][i] );
  226.                 }
  227.             }
  228.         }
  229.     }
  230.  
  231. };
  232.  
  233. /**
  234.  * Apply a smart stamp at the given position
  235.  * @param {int}   x     position of the top-left corner on the x-axis
  236.  * @param {int}   y     position of the top-left corner on the x-axis
  237.  * @param {array} stamp an array of strings, with each character representing a color
  238.  * @param {map}   map   a map with characters as keys and color codes as values
  239.  */
  240. microm.smartStamp = function( x, y, stamp, map ){
  241.     x = ~~x;
  242.     y = ~~y;
  243.    
  244.     var w = microm.getStampWidth(stamp);
  245.     if( w+x > microm.cell.length ){
  246.         w = microm.cell.length - x;
  247.     }
  248.     for( var i=0; i<w; ++i ){
  249.        
  250.         if( i+x >= 0 ){
  251.             var h = stamp.length;
  252.             if( h+y > microm.cell[i+x].length ){
  253.                 h = microm.cell[i+x].length - y;
  254.             }
  255.             for( var j=0; j<h; ++j ){
  256.                
  257.                 if( i < stamp[j].length ){
  258.                     if( stamp[j].charAt(i) != ' ' ){
  259.                         microm.set( i+x, j+y, map[stamp[j].charAt(i)] );
  260.                     }
  261.                 }
  262.             }
  263.         }
  264.     }
  265. };
  266.  
  267. /**
  268.  * Gives the width of the given regular or smart stamp
  269.  * @param   {array} stamp an array of arrays or an array of strings
  270.  * @returns {int}         the width of the stamp
  271.  */
  272. microm.getStampWidth = function( stamp ){
  273.     var stamp_max = 0;
  274.     for( var k=0; k<stamp.length; ++k ){
  275.         if( stamp_max < stamp[k].length ){
  276.             stamp_max = stamp[k].length;
  277.         }
  278.     }
  279.     return stamp_max;
  280. };
  281.  
  282. /**
  283.  * Gives the height of the given regular or smart stamp
  284.  * @param   {array} stamp an array of arrays or an array of strings
  285.  * @returns {int}         the height of the stamp
  286.  */
  287. microm.getStampHeight = function( stamp ){
  288.     return stamp.length;
  289. };
  290.  
  291. /**
  292.  * Reverse the given regular stamp on the y-axis (mirror the stamp)
  293.  * @param   {array} stamp an array of arrays
  294.  * @returns {array}       the reversed array of arrays with the necessary paddings
  295.  */
  296. microm.reverseStamp = function( stamp ){
  297.     var result = [];
  298.     var w = microm.getStampWidth(stamp);
  299.    
  300.     for( var i=0; i<stamp.length; ++i ){
  301.         for( var j=stamp[i].length; j<w; ++j ){
  302.             stamp[i][j] = null;
  303.         }
  304.         result[i] = stamp[i].reverse();
  305.     }
  306.     return result;
  307. };
  308.  
  309. /**
  310.  * Reverse the given smart stamp on the y-axis (mirror the stamp)
  311.  * @param   {array} stamp an array of strings
  312.  * @returns {array}       the reversed array of strings with the necessary paddings
  313.  */
  314. microm.reverseSmartStamp = function( stamp ){
  315.     var result = [];
  316.     var w = microm.getStampWidth(stamp);
  317.    
  318.     for( var i=0; i<stamp.length; ++i ){
  319.         for( var j=stamp[i].length; j<w; ++j ){
  320.             stamp[i] += " ";
  321.         }
  322.         result[i] = stamp[i].split("").reverse().join("");
  323.     }
  324.     return result;
  325. };
  326.  
  327. /**
  328.  * Flip the regular or smart stamp on the x-axis
  329.  * @param   {array} stamp an array of arrays or an array of strings
  330.  * @returns {array}       the flipped array of arrays or array of strings
  331.  */
  332. microm.flipStamp = function( stamp ){
  333.     var result = [];
  334.    
  335.     for( var i=0; i<stamp.length; ++i ){
  336.         result[ stamp.length-i-1 ] = stamp[i];
  337.     }
  338.     return result;
  339. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement