AsciiEffect.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 bResolution = ! options[ 'resolution' ] ? 0.15 : options[ 'resolution' ]; // Higher for more details
  13. const iScale = ! options[ 'scale' ] ? 1 : options[ 'scale' ];
  14. const bColor = ! options[ 'color' ] ? false : options[ 'color' ]; // nice but slows down rendering!
  15. const bAlpha = ! options[ 'alpha' ] ? false : options[ 'alpha' ]; // Transparency
  16. const bBlock = ! options[ 'block' ] ? false : options[ 'block' ]; // blocked characters. like good O dos
  17. const bInvert = ! options[ 'invert' ] ? false : options[ 'invert' ]; // black is white, white is black
  18. const 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( renderer, oAscii );
  35. };
  36. this.domElement = domElement;
  37. // Throw in ascii library from https://github.com/hassadee/jsascii/blob/master/jsascii.js
  38. /*
  39. * jsAscii 0.1
  40. * Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
  41. * MIT License [http://www.nihilogic.dk/licenses/mit-license.txt]
  42. */
  43. function initAsciiSize() {
  44. iWidth = Math.round( width * fResolution );
  45. iHeight = Math.round( height * fResolution );
  46. oCanvas.width = iWidth;
  47. oCanvas.height = iHeight;
  48. // oCanvas.style.display = "none";
  49. // oCanvas.style.width = iWidth;
  50. // oCanvas.style.height = iHeight;
  51. oImg = renderer.domElement;
  52. if ( oImg.style.backgroundColor ) {
  53. oAscii.rows[ 0 ].cells[ 0 ].style.backgroundColor = oImg.style.backgroundColor;
  54. oAscii.rows[ 0 ].cells[ 0 ].style.color = oImg.style.color;
  55. }
  56. oAscii.cellSpacing = 0;
  57. oAscii.cellPadding = 0;
  58. const oStyle = oAscii.style;
  59. oStyle.display = 'inline';
  60. oStyle.width = Math.round( iWidth / fResolution * iScale ) + 'px';
  61. oStyle.height = Math.round( iHeight / fResolution * iScale ) + 'px';
  62. oStyle.whiteSpace = 'pre';
  63. oStyle.margin = '0px';
  64. oStyle.padding = '0px';
  65. oStyle.letterSpacing = fLetterSpacing + 'px';
  66. oStyle.fontFamily = strFont;
  67. oStyle.fontSize = fFontSize + 'px';
  68. oStyle.lineHeight = fLineHeight + 'px';
  69. oStyle.textAlign = 'left';
  70. oStyle.textDecoration = 'none';
  71. }
  72. const aDefaultCharList = ( ' .,:;i1tfLCG08@' ).split( '' );
  73. const aDefaultColorCharList = ( ' CGO08@' ).split( '' );
  74. const strFont = 'courier new, monospace';
  75. const oCanvasImg = renderer.domElement;
  76. const oCanvas = document.createElement( 'canvas' );
  77. if ( ! oCanvas.getContext ) {
  78. return;
  79. }
  80. const oCtx = oCanvas.getContext( '2d' );
  81. if ( ! oCtx.getImageData ) {
  82. return;
  83. }
  84. let aCharList = ( bColor ? aDefaultColorCharList : aDefaultCharList );
  85. if ( charSet ) aCharList = charSet;
  86. let fResolution = 0.5;
  87. switch ( strResolution ) {
  88. case 'low' : fResolution = 0.25; break;
  89. case 'medium' : fResolution = 0.5; break;
  90. case 'high' : fResolution = 1; break;
  91. }
  92. if ( bResolution ) fResolution = bResolution;
  93. // Setup dom
  94. const fFontSize = ( 2 / fResolution ) * iScale;
  95. const fLineHeight = ( 2 / fResolution ) * iScale;
  96. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  97. let fLetterSpacing = 0;
  98. if ( strResolution == 'low' ) {
  99. switch ( iScale ) {
  100. case 1 : fLetterSpacing = - 1; break;
  101. case 2 :
  102. case 3 : fLetterSpacing = - 2.1; break;
  103. case 4 : fLetterSpacing = - 3.1; break;
  104. case 5 : fLetterSpacing = - 4.15; break;
  105. }
  106. }
  107. if ( strResolution == 'medium' ) {
  108. switch ( iScale ) {
  109. case 1 : fLetterSpacing = 0; break;
  110. case 2 : fLetterSpacing = - 1; break;
  111. case 3 : fLetterSpacing = - 1.04; break;
  112. case 4 :
  113. case 5 : fLetterSpacing = - 2.1; break;
  114. }
  115. }
  116. if ( strResolution == 'high' ) {
  117. switch ( iScale ) {
  118. case 1 :
  119. case 2 : fLetterSpacing = 0; break;
  120. case 3 :
  121. case 4 :
  122. case 5 : fLetterSpacing = - 1; break;
  123. }
  124. }
  125. // can't get a span or div to flow like an img element, but a table works?
  126. // convert img element to ascii
  127. function asciifyImage( canvasRenderer, oAscii ) {
  128. oCtx.clearRect( 0, 0, iWidth, iHeight );
  129. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  130. const oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  131. // Coloring loop starts now
  132. let strChars = '';
  133. // console.time('rendering');
  134. for ( let y = 0; y < iHeight; y += 2 ) {
  135. for ( let x = 0; x < iWidth; x ++ ) {
  136. const iOffset = ( y * iWidth + x ) * 4;
  137. const iRed = oImgData[ iOffset ];
  138. const iGreen = oImgData[ iOffset + 1 ];
  139. const iBlue = oImgData[ iOffset + 2 ];
  140. const iAlpha = oImgData[ iOffset + 3 ];
  141. let iCharIdx;
  142. let fBrightness;
  143. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  144. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  145. if ( iAlpha == 0 ) {
  146. // should calculate alpha instead, but quick hack :)
  147. //fBrightness *= (iAlpha / 255);
  148. fBrightness = 1;
  149. }
  150. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  151. if ( bInvert ) {
  152. iCharIdx = aCharList.length - iCharIdx - 1;
  153. }
  154. // good for debugging
  155. //fBrightness = Math.floor(fBrightness * 10);
  156. //strThisChar = fBrightness;
  157. let strThisChar = aCharList[ iCharIdx ];
  158. if ( strThisChar === undefined || strThisChar == ' ' )
  159. strThisChar = '&nbsp;';
  160. if ( bColor ) {
  161. strChars += '<span style=\''
  162. + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');'
  163. + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' )
  164. + ( bAlpha ? 'opacity:' + ( iAlpha / 255 ) + ';' : '' )
  165. + '\'>' + strThisChar + '</span>';
  166. } else {
  167. strChars += strThisChar;
  168. }
  169. }
  170. strChars += '<br/>';
  171. }
  172. oAscii.innerHTML = '<tr><td>' + strChars + '</td></tr>';
  173. // console.timeEnd('rendering');
  174. // return oAscii;
  175. }
  176. }
  177. }
  178. export { AsciiEffect };