harraps

microm.1.0.1.js

Jun 15th, 2015
234
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.  * @version 1.0.1
  15.  *
  16.  * Microm is a litle javascript file that allow you to generate a matrix of virtual LED
  17.  * that you can control with a virtual keyboard.
  18.  *
  19.  * To start, use the initDisplay and initKeyboard functions to create the display and the keyboard.
  20.  *
  21.  */
  22.  
  23.  
  24. var microm = {};
  25. microm.cell = {};
  26. microm.input = {};
  27. microm.animation;
  28.  
  29. // width of the display
  30. microm.width;
  31.  
  32. // height of the display
  33. microm.height;
  34.  
  35. // number of frame per second
  36. microm.fps = 1;
  37.  
  38. // the default color of the screen
  39. microm.defaultColor = "#111";
  40.  
  41. // microm's colors preset
  42. microm.red = "#c71313";
  43. microm.orange = "#f58708";
  44. microm.yellow = "#e6c614";
  45. microm.lime = "#b5e514";
  46. microm.green = "#1aa21a";
  47. microm.pine = "#19ce66";
  48. microm.cyan = "#1bd4d4";
  49. microm.blue = "#1c7ed4";
  50. microm.colbat = "#1111b7";
  51. microm.violet = "#3617de";
  52. microm.purple = "#781fc6";
  53. microm.magenta = "#c61cc6";
  54. microm.crimson = "#d60b77";
  55.  
  56. microm.black = "#000000";
  57. microm.dark = "#2c2c2c";
  58. microm.gray = "#606060";
  59. microm.light = "#a2a2a2";
  60. microm.white = "#ebebeb";
  61.  
  62. microm.pink = "#de8ba4";
  63. microm.brown = "#572f12";
  64. microm.beige = "#dda670";
  65. microm.sea = "#071143";
  66.  
  67. /**
  68.  * Generate a virtual screen of LED
  69.  * @param {string} display_id the id of the div in which we want to put our display
  70.  * @param {int}    width      the width of the screen we want to make
  71.  * @param {int}    height     the height of the screen we want to make
  72.  */
  73. microm.initDisplay = function( display_id, width, height ){
  74.     width = ~~width;
  75.     height = ~~height;
  76.    
  77.     microm.width = width;
  78.     microm.height = height;
  79.    
  80.     microm.cell = new Array(width);
  81.     for( var i=0; i<width; ++i ){
  82.         microm.cell[i] = new Array(height);
  83.     }
  84.    
  85.     var div_display = document.getElementById(display_id);
  86.     var table = document.createElement('table');
  87.     div_display.appendChild(table);
  88.    
  89.     for( var i=0; i<height; ++i ){
  90.        
  91.         var tr = document.createElement('tr');
  92.         table.appendChild(tr);
  93.        
  94.         for( var j=0; j<width; ++j ){
  95.            
  96.             var td = document.createElement('td');
  97.             microm.cell[j][i] = td;
  98.             tr.appendChild(td);
  99.         }
  100.     }
  101. };
  102.  
  103. /**
  104.  * Generate a virtual keyboard
  105.  * @param {string} keyboard_id the id of the div in which we want to put our keyboard
  106.  * @param {array}  keys_array  a array of arrays of string, each string will be the name of a button
  107.  */
  108. microm.initKeyboard = function( keyboard_id, keys_array ){
  109.    
  110.     var div_keyboard = document.getElementById(keyboard_id);
  111.     var table = document.createElement('table');
  112.     div_keyboard.appendChild(table);
  113.    
  114.     for( var i=0; i<keys_array.length; ++i ){
  115.        
  116.         var tr = document.createElement('tr');
  117.         table.appendChild(tr);
  118.        
  119.         for( var j=0; j<keys_array[i].length; ++j ){
  120.            
  121.             var td = document.createElement('td');
  122.             tr.appendChild(td);
  123.            
  124.             if( keys_array[i][j] != null ){
  125.                 var btn = document.createElement('button');
  126.                 btn.innerHTML = keys_array[i][j];
  127.                 microm.input[""+keys_array[i][j]] = btn;
  128.                 td.appendChild(btn);
  129.             }
  130.         }
  131.     }
  132. };
  133.  
  134. /**
  135.  * Set the color of the targeted LED
  136.  * @param {int}    x     position of the LED on the x-axis
  137.  * @param {int}    y     position of the LED on the y-axis
  138.  * @param {string} color the color we want to give to the LED
  139.  */
  140. microm.set = function( x, y, color ){
  141.     x = ~~x;
  142.     y = ~~y;
  143.     if( x >= 0 && x < microm.cell.length ){
  144.         if( y >= 0 && y < microm.cell[x].length ){
  145.             microm.cell[x][y].style = "background:".concat(color);
  146.         }
  147.     }
  148. };
  149.  
  150. /**
  151.  * Reset the color of the targeted LED
  152.  * @param {int}    x     position of the LED on the x-axis
  153.  * @param {int}    y     position of the LED on the y-axis
  154.  */
  155. microm.reset = function( x, y ){
  156.     microm.set( x, y, microm.defaultColor );
  157. };
  158.  
  159. /**
  160.  * Fill a rectangle in the screen with the given color
  161.  * @param {int}    x      position of the top-left corner on the x-axis
  162.  * @param {int}    y      position of the top-left corner on the y-axis
  163.  * @param {int}    width  width of the rectangle
  164.  * @param {int}    height height of the rectangle
  165.  * @param {string} color  the color of the rectangle
  166.  */
  167. microm.drawRect = function( x, y, width, height, color ){
  168.     x = ~~x;
  169.     y = ~~y;
  170.     width = ~~width;
  171.     height = ~~height;
  172.    
  173.     var w = x+width;
  174.     if( w > microm.cell.length ){
  175.         w = microm.cell.length;
  176.     }
  177.     for( var i=x; i<w; ++i ){
  178.        
  179.         var h = y+height;
  180.         if( h > microm.cell[i].length ){
  181.             h = microm.cell[i].length;
  182.         }
  183.         for( var j=y; j<h; ++j ){
  184.            
  185.             microm.set( i, j, color );
  186.         }
  187.     }
  188. };
  189.  
  190. /**
  191.  * Paint the full screen with the given color
  192.  * @param {string} color the color to paint the screen
  193.  */
  194. microm.paint = function( color ){
  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( color );
  198.         }
  199.     }
  200. };
  201.  
  202. /**
  203.  * Reset the color of the full screen to the default color
  204.  */
  205. microm.clear = function(){
  206.     for( var i=0; i<microm.cell.length; ++i ){
  207.         for( var j=0; j<microm.cell[i].length; ++j ){
  208.             microm.cell[i][j].style = "background:".concat( microm.defaultColor );
  209.         }
  210.     }
  211. };
  212.  
  213. /**
  214.  * Apply a smart stamp at the given position
  215.  * @param {int}   x     position of the top-left corner on the x-axis
  216.  * @param {int}   y     position of the top-left corner on the x-axis
  217.  * @param {array} stamp an array of arrays or an array of strings, with each character representing a color
  218.  * @param {map}   map   (optionnal) a map with characters as keys and color codes as values
  219.  */
  220. microm.stamp = function( x, y, stamp, map ){
  221.     x = ~~x;
  222.     y = ~~y;
  223.    
  224.     var w = microm.getStampWidth(stamp);
  225.     if( w+x > microm.cell.length ){
  226.         w = microm.cell.length - x;
  227.     }
  228.     for( var i=0; i<w; ++i ){
  229.        
  230.         if( i+x >= 0 ){
  231.             var h = stamp.length;
  232.             if( h+y > microm.cell[i+x].length ){
  233.                 h = microm.cell[i+x].length - y;
  234.             }
  235.             for( var j=0; j<h; ++j ){
  236.                 if( typeof map !== 'undefined' && (typeof stamp[j] === 'string' || stamp[j] instanceof String) ){
  237.                     var key = stamp[j].charAt(i);
  238.                    
  239.                     if( map[key] !== undefined && i < stamp[j].length ){
  240.                         if( stamp[j].charAt(i) != ' ' ){
  241.                             microm.set( i+x, j+y, map[key] );
  242.                         }
  243.                     }
  244.                 }else{
  245.                     if( stamp[j][i] != null ){
  246.                         microm.set( i+x, j+y, stamp[j][i] );
  247.                     }
  248.                 }
  249.             }
  250.         }
  251.     }
  252. };
  253.  
  254. /**
  255.  * Gives the width of the given regular or smart stamp
  256.  * @param   {array} stamp an array of arrays or an array of strings
  257.  * @returns {int}         the width of the stamp
  258.  */
  259. microm.getStampWidth = function( stamp ){
  260.     var stamp_max = 0;
  261.     for( var k=0; k<stamp.length; ++k ){
  262.         if( stamp_max < stamp[k].length ){
  263.             stamp_max = stamp[k].length;
  264.         }
  265.     }
  266.     return stamp_max;
  267. };
  268.  
  269. /**
  270.  * Gives the height of the given regular or smart stamp
  271.  * @param   {array} stamp an array of arrays or an array of strings
  272.  * @returns {int}         the height of the stamp
  273.  */
  274. microm.getStampHeight = function( stamp ){
  275.     return stamp.length;
  276. };
  277.  
  278. /**
  279.  * Pad the given stamp so it can be reversed or turned
  280.  * @param   {array} stamp the stamp to pad
  281.  * @returns {array}       the padded stamp
  282.  */
  283. microm.padStamp = function( stamp ){
  284.     var w = microm.getStampWidth(stamp);
  285.    
  286.     for( var i=0; i<stamp.length; ++i ){
  287.  
  288.         if( typeof stamp[i] === 'string' || stamp[i] instanceof String ){
  289.             for( var j=stamp[i].length; j<w; ++j ){
  290.                 stamp[i] += " ";
  291.             }
  292.         }else{
  293.             for( var j=stamp[i].length; j<w; ++j ){
  294.                 stamp[i][j] = null;
  295.             }
  296.         }
  297.     }
  298.     return stamp;
  299. };
  300.  
  301. /**
  302.  * Apply the given map on the given stamp, so the map isn't requiered anymore
  303.  * @param   {array} stamp an array of strings
  304.  * @param   {map}   map   a map with characters as keys and color codes as values
  305.  * @returns {array}       an array of arrays
  306.  */
  307. microm.applyMap = function( stamp, map ){
  308.     var result = [];
  309.    
  310.     for( var i=0; i<stamp.length; ++i ){
  311.         result[i] = [];
  312.         if( typeof map !== 'undefined' && (typeof stamp[i] === 'string' || stamp[i] instanceof String) ){
  313.             for( var j=0; j<stamp[i].length; ++j ){
  314.                 var key = stamp[i].charAt(j);
  315.                
  316.                 if( map[key] !== undefined ){
  317.                     result[i][j] = map[key];
  318.                 }else{
  319.                     result[i][j] = null;
  320.                 }
  321.             }
  322.         }
  323.     }
  324.     return result;
  325. };
  326.  
  327. /**
  328.  * Reverse the stamp on the y-axis
  329.  * @param   {array} stamp an array of arrays or an array of strings
  330.  * @returns {array}       the reversed array of arrays or array of strings
  331.  */
  332. microm.reverseStamp = function( stamp ){
  333.     var result = [];
  334.     stamp = microm.padStamp( stamp );
  335.    
  336.     for( var i=0; i<stamp.length; ++i ){
  337.        
  338.         if( typeof stamp[i] === 'string' || stamp[i] instanceof String ){
  339.             result[i] = stamp[i].split("").reverse().join("");
  340.         }else{
  341.             result[i] = stamp[i].reverse();
  342.         }
  343.     }
  344.     return result;
  345. };
  346.  
  347. /**
  348.  * Flip the stamp on the x-axis
  349.  * @param   {array} stamp an array of arrays or an array of strings
  350.  * @returns {array}       the flipped array of arrays or array of strings
  351.  */
  352. microm.flipStamp = function( stamp ){
  353.     var result = [];
  354.    
  355.     for( var i=0; i<stamp.length; ++i ){
  356.         result[ stamp.length-i-1 ] = stamp[i];
  357.     }
  358.     return result;
  359. };
  360.  
  361. /**
  362.  * Transpose the given stamp and turn it into an array of arrays
  363.  * @param   {array} stamp an array of arrays or an array of strings
  364.  * @param   {map}   map   a map with characters as keys and color codes as values
  365.  * @returns {array}       an array of arrays
  366.  */
  367. microm.transposeStamp = function( stamp, map ){
  368.     stamp = microm.padStamp( stamp );
  369.    
  370.     var w = microm.getStampWidth(stamp);
  371.     var result = new Array(w);
  372.    
  373.     for( var i=0; i<w; ++i ){
  374.        
  375.         var h = stamp.length;
  376.         result[i] = new Array(h);
  377.        
  378.         for( var j=0; j<h; ++j ){
  379.            
  380.             if( typeof map !== 'undefined' && (typeof stamp[j] === 'string' || stamp[j] instanceof String) ){
  381.                 var key = stamp[j].charAt(i);
  382.                    
  383.                 if( i < stamp[j].length ){
  384.                     if( map[key] !== undefined ){
  385.                         result[i][j] = map[key];
  386.                     }else{
  387.                         result[i][j] = null;
  388.                     }
  389.                 }
  390.             }else{
  391.                 if( stamp[j][i] != null ){
  392.                     result[i][j] = stamp[j][i];
  393.                 }else if( typeof stamp[j] === 'string' || stamp[j] instanceof String ){
  394.                     result[i] += ''+stamp[j].charAt(i);
  395.                 }
  396.             }
  397.         }
  398.     }
  399.     return result;
  400. };
  401.  
  402. /**
  403.  * Rotate the given stamp of the given number of degrees or radiants
  404.  * @param   {float} angle the angle of rotation in degrees or radiants
  405.  * @param   {array} stamp an array of arrays or an array of strings
  406.  * @param   {map}   map   a map with characters as keys and color codes as values
  407.  * @returns {array}       an array of arrays
  408.  */
  409. microm.rotateStamp = function( angle, stamp, map ){
  410.     var turn = 0;
  411.     var result = [];
  412.    
  413.     if( -90 < angle && angle < 90 ){
  414.         angle *= 2;
  415.     }else{
  416.         angle /= 90;
  417.     }
  418.     turn = Math.round(angle) % 4;
  419.     if( turn < 0 ){
  420.         turn = (turn + 4) % 4;
  421.     }
  422.    
  423.     switch( turn ){
  424.         case 1:
  425.             result = microm.transposeStamp(microm.reverseStamp(stamp), map);
  426.             break;
  427.         case 2:
  428.             result = microm.applyMap(microm.reverseStamp(microm.flipStamp(stamp)), map);
  429.             break;
  430.         case 3:
  431.             result = microm.reverseStamp(microm.transposeStamp(stamp, map));
  432.             break;
  433.         default:
  434.             result = microm.applyMap(stamp, map);
  435.     }
  436.     return result;
  437. };
  438.  
  439. /**
  440.  * Repeat the given function every frame, frame rate is set in the fps variable
  441.  * @param {function} fun the function to repeat every frame
  442.  */
  443. microm.startAnimation = function( fun ){
  444.     microm.animation = window.setInterval( fun, 1000 / microm.fps );
  445. };
  446.  
  447. /**
  448.  * Stop the animation
  449.  */
  450. microm.stopAnimation = function(){
  451.     window.clearInterval( microm.animation );
  452. };
Advertisement
Add Comment
Please, Sign In to add comment