AsciiEffect.js 6.4 KB

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