/sentiment/canvas/js/app/GTP.js

https://github.com/byteface/GTP · JavaScript · 147 lines · 74 code · 48 blank · 25 comment · 5 complexity · dc74b330ff55248b1228aecfec22e536 MD5 · raw file

  1. /*
  2. here for ref unadultered...
  3. Loader is now the moving progress as were tailoring it for sentiment.
  4. I'd probably see me rolling that one back to this one somehow taking best of both once i see how usage transpires
  5. this serves as gun test runner though for data so leaving as is
  6. */
  7. var Gtp = Gtp || {};
  8. Gtp.prototype = {
  9. load : function( file )
  10. {
  11. var im = new Image();
  12. var selfRef = this;
  13. im.onload = function( e ){ selfRef.imageLoaded( e ) };
  14. im.src = file;//"ImageGenTest.png";
  15. },
  16. imageLoaded : function ( ev ) {
  17. element = document.getElementById("can");
  18. c = element.getContext("2d");
  19. im = ev.target; // the image, assumed to be 783x783 current negative map
  20. // read the width and height of the canvas
  21. width = parseInt(element.getAttribute("width"));
  22. height = parseInt(element.getAttribute("height"));
  23. // stamp the image on the left of the canvas:
  24. c.drawImage(im, 0, 0);
  25. // get all canvas pixel data
  26. imageData = c.getImageData(0, 0, width, height);
  27. var pix = imageData.data;
  28. var str = "";
  29. var i=0;
  30. var len=pix.length;
  31. var arr=[];
  32. for( i; i<len; i+=4 ) {
  33. arr.push( this.map[pix[i]] );
  34. arr.push( this.map[pix[i+1]] );
  35. arr.push( this.map[pix[i+2]] );
  36. // arr.push( 0xfff );
  37. // str += map[pix[i+3]]+"";
  38. }
  39. arr.join("");
  40. //document.write( arr.join("") );
  41. // window.console.log(str);
  42. },
  43. createDataImage : function ( data )
  44. {
  45. var contents = data;
  46. var contentsLength = contents.length;
  47. var squared = Math.ceil( Math.sqrt( contentsLength/3 ) ); // round up the square root and divide by 3 as were using 3 channels
  48. // window.console.log( squared );
  49. // TODO - need to get lines too so can add extra pixels as line seperators... implement fget - note same for this now in js version
  50. // - TODO - dynamic width / height based on square size needed
  51. // jQuery('<canvas id="new" width="400", height="400"></canvas>').html("").appendTo('#myDiv');
  52. document.write('<canvas id="new" width="' + squared + '", height="' + squared + '"></canvas>' );
  53. element = document.getElementById("new");
  54. c = element.getContext("2d");
  55. // read the width and height of the canvas
  56. width = parseInt(element.getAttribute("width"));
  57. height = parseInt(element.getAttribute("height"));
  58. imageData = c.createImageData(width, height);
  59. var xPos = 0;
  60. var yPos = 0;
  61. for( var charCount = 0; charCount < contentsLength; charCount+=4 )
  62. {
  63. var char1 = contents.substr( charCount, 1);
  64. var char2 = contents.substr( charCount+1, 1);
  65. var char3 = contents.substr( charCount+2, 1);
  66. // var selfRef = this;
  67. var col = this.setPixel( imageData, xPos, yPos, this.colorise( char1 ), this.colorise( char2 ), this.colorise( char3 ), 0xff );
  68. xPos++;
  69. if(xPos>squared){
  70. xPos=0;
  71. yPos+=1;
  72. }
  73. }
  74. c.putImageData( imageData, 0, 0 );
  75. },
  76. colorise: function ( character )
  77. {
  78. var col = 244; // if a character is unrecognised it will become space for now
  79. if( this.encodeMap[character] ){
  80. col = this.encodeMap[character];
  81. }else{
  82. // TODO - some of these aren't showing... question mark seems to throw although i have it
  83. // error_log( $character );
  84. }
  85. return col;
  86. },
  87. setPixel : function ( imageData, x, y, r, g, b, a ) // TODO - add and use the alpha channel
  88. {
  89. var index = (x + y * imageData.width) * 4;
  90. imageData.data[index+0] = r;
  91. imageData.data[index+1] = g;
  92. imageData.data[index+2] = b;
  93. imageData.data[index+3] = a;
  94. }
  95. }