AsciiEffect.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.floor( width * fResolution );
  40. iHeight = Math.floor( 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.whiteSpace = 'pre';
  55. oStyle.margin = '0px';
  56. oStyle.padding = '0px';
  57. oStyle.letterSpacing = fLetterSpacing + 'px';
  58. oStyle.fontFamily = strFont;
  59. oStyle.fontSize = fFontSize + 'px';
  60. oStyle.lineHeight = fLineHeight + 'px';
  61. oStyle.textAlign = 'left';
  62. oStyle.textDecoration = 'none';
  63. }
  64. const aDefaultCharList = ( ' .,:;i1tfLCG08@' ).split( '' );
  65. const aDefaultColorCharList = ( ' CGO08@' ).split( '' );
  66. const strFont = 'courier new, monospace';
  67. const oCanvasImg = renderer.domElement;
  68. const oCanvas = document.createElement( 'canvas' );
  69. if ( ! oCanvas.getContext ) {
  70. return;
  71. }
  72. const oCtx = oCanvas.getContext( '2d' );
  73. if ( ! oCtx.getImageData ) {
  74. return;
  75. }
  76. let aCharList = ( bColor ? aDefaultColorCharList : aDefaultCharList );
  77. if ( charSet ) aCharList = charSet;
  78. // Setup dom
  79. const fFontSize = ( 2 / fResolution ) * iScale;
  80. const fLineHeight = ( 2 / fResolution ) * iScale;
  81. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  82. let fLetterSpacing = 0;
  83. if ( strResolution == 'low' ) {
  84. switch ( iScale ) {
  85. case 1 : fLetterSpacing = - 1; break;
  86. case 2 :
  87. case 3 : fLetterSpacing = - 2.1; break;
  88. case 4 : fLetterSpacing = - 3.1; break;
  89. case 5 : fLetterSpacing = - 4.15; break;
  90. }
  91. }
  92. if ( strResolution == 'medium' ) {
  93. switch ( iScale ) {
  94. case 1 : fLetterSpacing = 0; break;
  95. case 2 : fLetterSpacing = - 1; break;
  96. case 3 : fLetterSpacing = - 1.04; break;
  97. case 4 :
  98. case 5 : fLetterSpacing = - 2.1; break;
  99. }
  100. }
  101. if ( strResolution == 'high' ) {
  102. switch ( iScale ) {
  103. case 1 :
  104. case 2 : fLetterSpacing = 0; break;
  105. case 3 :
  106. case 4 :
  107. case 5 : fLetterSpacing = - 1; break;
  108. }
  109. }
  110. // can't get a span or div to flow like an img element, but a table works?
  111. // convert img element to ascii
  112. function asciifyImage( oAscii ) {
  113. oCtx.clearRect( 0, 0, iWidth, iHeight );
  114. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  115. const oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  116. // Coloring loop starts now
  117. let strChars = '';
  118. // console.time('rendering');
  119. for ( let y = 0; y < iHeight; y += 2 ) {
  120. for ( let x = 0; x < iWidth; x ++ ) {
  121. const iOffset = ( y * iWidth + x ) * 4;
  122. const iRed = oImgData[ iOffset ];
  123. const iGreen = oImgData[ iOffset + 1 ];
  124. const iBlue = oImgData[ iOffset + 2 ];
  125. const iAlpha = oImgData[ iOffset + 3 ];
  126. let iCharIdx;
  127. let fBrightness;
  128. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  129. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  130. if ( iAlpha == 0 ) {
  131. // should calculate alpha instead, but quick hack :)
  132. //fBrightness *= (iAlpha / 255);
  133. fBrightness = 1;
  134. }
  135. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  136. if ( bInvert ) {
  137. iCharIdx = aCharList.length - iCharIdx - 1;
  138. }
  139. // good for debugging
  140. //fBrightness = Math.floor(fBrightness * 10);
  141. //strThisChar = fBrightness;
  142. let strThisChar = aCharList[ iCharIdx ];
  143. if ( strThisChar === undefined || strThisChar == ' ' )
  144. strThisChar = '&nbsp;';
  145. if ( bColor ) {
  146. strChars += '<span style=\''
  147. + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');'
  148. + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' )
  149. + ( bAlpha ? 'opacity:' + ( iAlpha / 255 ) + ';' : '' )
  150. + '\'>' + strThisChar + '</span>';
  151. } else {
  152. strChars += strThisChar;
  153. }
  154. }
  155. strChars += '<br/>';
  156. }
  157. oAscii.innerHTML = `<tr><td style="display:block;width:${width}px;height:${height}px;overflow:hidden">${strChars}</td></tr>`;
  158. // console.timeEnd('rendering');
  159. // return oAscii;
  160. }
  161. }
  162. }
  163. export { AsciiEffect };