AsciiEffect.js 6.8 KB

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