AsciiEffect.js 6.2 KB

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