AsciiEffect.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Ascii generation is based on http://www.nihilogic.dk/labs/jsascii/
  3. * Maybe more about this later with a blog post at http://lab4games.net/zz85/blog
  4. *
  5. * 16 April 2012 - @blurspline
  6. */
  7. var AsciiEffect = function ( renderer, charSet, options ) {
  8. // its fun to create one your own!
  9. charSet = ( charSet === undefined ) ? ' .:-=+*#%@' : charSet;
  10. // ' .,:;=|iI+hHOE#`$';
  11. // darker bolder character set from https://github.com/saw/Canvas-ASCII-Art/
  12. // ' .\'`^",:;Il!i~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'.split('');
  13. if ( ! options ) options = {};
  14. // Some ASCII settings
  15. var bResolution = ! options[ 'resolution' ] ? 0.15 : options[ 'resolution' ]; // Higher for more details
  16. var iScale = ! options[ 'scale' ] ? 1 : options[ 'scale' ];
  17. var bColor = ! options[ 'color' ] ? false : options[ 'color' ]; // nice but slows down rendering!
  18. var bAlpha = ! options[ 'alpha' ] ? false : options[ 'alpha' ]; // Transparency
  19. var bBlock = ! options[ 'block' ] ? false : options[ 'block' ]; // blocked characters. like good O dos
  20. var bInvert = ! options[ 'invert' ] ? false : options[ 'invert' ]; // black is white, white is black
  21. var strResolution = 'low';
  22. var width, height;
  23. var domElement = document.createElement( 'div' );
  24. domElement.style.cursor = 'default';
  25. var oAscii = document.createElement( 'table' );
  26. domElement.appendChild( oAscii );
  27. var iWidth, iHeight;
  28. var oImg;
  29. this.setSize = function ( w, h ) {
  30. width = w;
  31. height = h;
  32. renderer.setSize( w, h );
  33. initAsciiSize();
  34. };
  35. this.render = function ( scene, camera ) {
  36. renderer.render( scene, camera );
  37. asciifyImage( renderer, oAscii );
  38. };
  39. this.domElement = domElement;
  40. // Throw in ascii library from http://www.nihilogic.dk/labs/jsascii/jsascii.js
  41. /*
  42. * jsAscii 0.1
  43. * Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
  44. * MIT License [http://www.nihilogic.dk/licenses/mit-license.txt]
  45. */
  46. function initAsciiSize() {
  47. iWidth = Math.round( width * fResolution );
  48. iHeight = Math.round( height * fResolution );
  49. oCanvas.width = iWidth;
  50. oCanvas.height = iHeight;
  51. // oCanvas.style.display = "none";
  52. // oCanvas.style.width = iWidth;
  53. // oCanvas.style.height = iHeight;
  54. oImg = renderer.domElement;
  55. if ( oImg.style.backgroundColor ) {
  56. oAscii.rows[ 0 ].cells[ 0 ].style.backgroundColor = oImg.style.backgroundColor;
  57. oAscii.rows[ 0 ].cells[ 0 ].style.color = oImg.style.color;
  58. }
  59. oAscii.cellSpacing = 0;
  60. oAscii.cellPadding = 0;
  61. var oStyle = oAscii.style;
  62. oStyle.display = 'inline';
  63. oStyle.width = Math.round( iWidth / fResolution * iScale ) + 'px';
  64. oStyle.height = Math.round( iHeight / fResolution * iScale ) + 'px';
  65. oStyle.whiteSpace = 'pre';
  66. oStyle.margin = '0px';
  67. oStyle.padding = '0px';
  68. oStyle.letterSpacing = fLetterSpacing + 'px';
  69. oStyle.fontFamily = strFont;
  70. oStyle.fontSize = fFontSize + 'px';
  71. oStyle.lineHeight = fLineHeight + 'px';
  72. oStyle.textAlign = 'left';
  73. oStyle.textDecoration = 'none';
  74. }
  75. var aDefaultCharList = ( ' .,:;i1tfLCG08@' ).split( '' );
  76. var aDefaultColorCharList = ( ' CGO08@' ).split( '' );
  77. var strFont = 'courier new, monospace';
  78. var oCanvasImg = renderer.domElement;
  79. var oCanvas = document.createElement( 'canvas' );
  80. if ( ! oCanvas.getContext ) {
  81. return;
  82. }
  83. var oCtx = oCanvas.getContext( '2d' );
  84. if ( ! oCtx.getImageData ) {
  85. return;
  86. }
  87. var aCharList = ( bColor ? aDefaultColorCharList : aDefaultCharList );
  88. if ( charSet ) aCharList = charSet;
  89. var fResolution = 0.5;
  90. switch ( strResolution ) {
  91. case 'low' : fResolution = 0.25; break;
  92. case 'medium' : fResolution = 0.5; break;
  93. case 'high' : fResolution = 1; break;
  94. }
  95. if ( bResolution ) fResolution = bResolution;
  96. // Setup dom
  97. var fFontSize = ( 2 / fResolution ) * iScale;
  98. var fLineHeight = ( 2 / fResolution ) * iScale;
  99. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  100. var fLetterSpacing = 0;
  101. if ( strResolution == 'low' ) {
  102. switch ( iScale ) {
  103. case 1 : fLetterSpacing = - 1; break;
  104. case 2 :
  105. case 3 : fLetterSpacing = - 2.1; break;
  106. case 4 : fLetterSpacing = - 3.1; break;
  107. case 5 : fLetterSpacing = - 4.15; break;
  108. }
  109. }
  110. if ( strResolution == 'medium' ) {
  111. switch ( iScale ) {
  112. case 1 : fLetterSpacing = 0; break;
  113. case 2 : fLetterSpacing = - 1; break;
  114. case 3 : fLetterSpacing = - 1.04; break;
  115. case 4 :
  116. case 5 : fLetterSpacing = - 2.1; break;
  117. }
  118. }
  119. if ( strResolution == 'high' ) {
  120. switch ( iScale ) {
  121. case 1 :
  122. case 2 : fLetterSpacing = 0; break;
  123. case 3 :
  124. case 4 :
  125. case 5 : fLetterSpacing = - 1; break;
  126. }
  127. }
  128. // can't get a span or div to flow like an img element, but a table works?
  129. // convert img element to ascii
  130. function asciifyImage( canvasRenderer, oAscii ) {
  131. oCtx.clearRect( 0, 0, iWidth, iHeight );
  132. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  133. var oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  134. // Coloring loop starts now
  135. var strChars = '';
  136. // console.time('rendering');
  137. for ( var y = 0; y < iHeight; y += 2 ) {
  138. for ( var x = 0; x < iWidth; x ++ ) {
  139. var iOffset = ( y * iWidth + x ) * 4;
  140. var iRed = oImgData[ iOffset ];
  141. var iGreen = oImgData[ iOffset + 1 ];
  142. var iBlue = oImgData[ iOffset + 2 ];
  143. var iAlpha = oImgData[ iOffset + 3 ];
  144. var iCharIdx;
  145. var fBrightness;
  146. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  147. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  148. if ( iAlpha == 0 ) {
  149. // should calculate alpha instead, but quick hack :)
  150. //fBrightness *= (iAlpha / 255);
  151. fBrightness = 1;
  152. }
  153. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  154. if ( bInvert ) {
  155. iCharIdx = aCharList.length - iCharIdx - 1;
  156. }
  157. // good for debugging
  158. //fBrightness = Math.floor(fBrightness * 10);
  159. //strThisChar = fBrightness;
  160. var strThisChar = aCharList[ iCharIdx ];
  161. if ( strThisChar === undefined || strThisChar == ' ' )
  162. strThisChar = '&nbsp;';
  163. if ( bColor ) {
  164. strChars += '<span style=\''
  165. + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');'
  166. + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' )
  167. + ( bAlpha ? 'opacity:' + ( iAlpha / 255 ) + ';' : '' )
  168. + '\'>' + strThisChar + '</span>';
  169. } else {
  170. strChars += strThisChar;
  171. }
  172. }
  173. strChars += '<br/>';
  174. }
  175. oAscii.innerHTML = '<tr><td>' + strChars + '</td></tr>';
  176. // console.timeEnd('rendering');
  177. // return oAscii;
  178. }
  179. // end modified asciifyImage block
  180. };
  181. export { AsciiEffect };