AsciiEffect.js 6.2 KB

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