AsciiEffect.js 6.6 KB

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