AsciiEffect.js 6.6 KB

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