AsciiEffect.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 fResolution = options[ 'resolution' ] || 0.15; // Higher for more details
  13. const iScale = options[ 'scale' ] || 1;
  14. const bColor = options[ 'color' ] || false; // nice but slows down rendering!
  15. const bAlpha = options[ 'alpha' ] || false; // Transparency
  16. const bBlock = options[ 'block' ] || false; // blocked characters. like good O dos
  17. const bInvert = options[ 'invert' ] || false; // black is white, white is black
  18. const strResolution = options[ '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( 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. // Setup dom
  82. const fFontSize = ( 2 / fResolution ) * iScale;
  83. const fLineHeight = ( 2 / fResolution ) * iScale;
  84. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  85. let fLetterSpacing = 0;
  86. if ( strResolution == 'low' ) {
  87. switch ( iScale ) {
  88. case 1 : fLetterSpacing = - 1; break;
  89. case 2 :
  90. case 3 : fLetterSpacing = - 2.1; break;
  91. case 4 : fLetterSpacing = - 3.1; break;
  92. case 5 : fLetterSpacing = - 4.15; break;
  93. }
  94. }
  95. if ( strResolution == 'medium' ) {
  96. switch ( iScale ) {
  97. case 1 : fLetterSpacing = 0; break;
  98. case 2 : fLetterSpacing = - 1; break;
  99. case 3 : fLetterSpacing = - 1.04; break;
  100. case 4 :
  101. case 5 : fLetterSpacing = - 2.1; break;
  102. }
  103. }
  104. if ( strResolution == 'high' ) {
  105. switch ( iScale ) {
  106. case 1 :
  107. case 2 : fLetterSpacing = 0; break;
  108. case 3 :
  109. case 4 :
  110. case 5 : fLetterSpacing = - 1; break;
  111. }
  112. }
  113. // can't get a span or div to flow like an img element, but a table works?
  114. // convert img element to ascii
  115. function asciifyImage( oAscii ) {
  116. oCtx.clearRect( 0, 0, iWidth, iHeight );
  117. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  118. const oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  119. // Coloring loop starts now
  120. let strChars = '';
  121. // console.time('rendering');
  122. for ( let y = 0; y < iHeight; y += 2 ) {
  123. for ( let x = 0; x < iWidth; x ++ ) {
  124. const iOffset = ( y * iWidth + x ) * 4;
  125. const iRed = oImgData[ iOffset ];
  126. const iGreen = oImgData[ iOffset + 1 ];
  127. const iBlue = oImgData[ iOffset + 2 ];
  128. const iAlpha = oImgData[ iOffset + 3 ];
  129. let iCharIdx;
  130. let fBrightness;
  131. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  132. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  133. if ( iAlpha == 0 ) {
  134. // should calculate alpha instead, but quick hack :)
  135. //fBrightness *= (iAlpha / 255);
  136. fBrightness = 1;
  137. }
  138. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  139. if ( bInvert ) {
  140. iCharIdx = aCharList.length - iCharIdx - 1;
  141. }
  142. // good for debugging
  143. //fBrightness = Math.floor(fBrightness * 10);
  144. //strThisChar = fBrightness;
  145. let strThisChar = aCharList[ iCharIdx ];
  146. if ( strThisChar === undefined || strThisChar == ' ' )
  147. strThisChar = '&nbsp;';
  148. if ( bColor ) {
  149. strChars += '<span style=\''
  150. + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');'
  151. + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' )
  152. + ( bAlpha ? 'opacity:' + ( iAlpha / 255 ) + ';' : '' )
  153. + '\'>' + strThisChar + '</span>';
  154. } else {
  155. strChars += strThisChar;
  156. }
  157. }
  158. strChars += '<br/>';
  159. }
  160. oAscii.innerHTML = '<tr><td>' + strChars + '</td></tr>';
  161. // console.timeEnd('rendering');
  162. // return oAscii;
  163. }
  164. }
  165. }
  166. export { AsciiEffect };