AsciiEffect.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ( function () {
  2. /**
  3. * Ascii generation is based on http://www.nihilogic.dk/labs/jsascii/
  4. * Maybe more about this later with a blog post at http://lab4games.net/zz85/blog
  5. *
  6. * 16 April 2012 - @blurspline
  7. */
  8. class AsciiEffect {
  9. constructor( renderer, charSet = ' .:-=+*#%@', options = {} ) {
  10. // ' .,:;=|iI+hHOE#`$';
  11. // darker bolder character set from https://github.com/saw/Canvas-ASCII-Art/
  12. // ' .\'`^",:;Il!i~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'.split('');
  13. // Some ASCII settings
  14. const bResolution = ! options[ 'resolution' ] ? 0.15 : options[ 'resolution' ]; // Higher for more details
  15. const iScale = ! options[ 'scale' ] ? 1 : options[ 'scale' ];
  16. const bColor = ! options[ 'color' ] ? false : options[ 'color' ]; // nice but slows down rendering!
  17. const bAlpha = ! options[ 'alpha' ] ? false : options[ 'alpha' ]; // Transparency
  18. const bBlock = ! options[ 'block' ] ? false : options[ 'block' ]; // blocked characters. like good O dos
  19. const bInvert = ! options[ 'invert' ] ? false : options[ 'invert' ]; // black is white, white is black
  20. const strResolution = 'low';
  21. let width, height;
  22. const domElement = document.createElement( 'div' );
  23. domElement.style.cursor = 'default';
  24. const oAscii = document.createElement( 'table' );
  25. domElement.appendChild( oAscii );
  26. let iWidth, iHeight;
  27. let oImg;
  28. this.setSize = function ( w, h ) {
  29. width = w;
  30. height = h;
  31. renderer.setSize( w, h );
  32. initAsciiSize();
  33. };
  34. this.render = function ( scene, camera ) {
  35. renderer.render( scene, camera );
  36. asciifyImage( renderer, oAscii );
  37. };
  38. this.domElement = domElement; // 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; // 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':
  89. fResolution = 0.25;
  90. break;
  91. case 'medium':
  92. fResolution = 0.5;
  93. break;
  94. case 'high':
  95. fResolution = 1;
  96. break;
  97. }
  98. if ( bResolution ) fResolution = bResolution; // Setup dom
  99. const fFontSize = 2 / fResolution * iScale;
  100. const fLineHeight = 2 / fResolution * iScale; // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  101. let fLetterSpacing = 0;
  102. if ( strResolution == 'low' ) {
  103. switch ( iScale ) {
  104. case 1:
  105. fLetterSpacing = - 1;
  106. break;
  107. case 2:
  108. case 3:
  109. fLetterSpacing = - 2.1;
  110. break;
  111. case 4:
  112. fLetterSpacing = - 3.1;
  113. break;
  114. case 5:
  115. fLetterSpacing = - 4.15;
  116. break;
  117. }
  118. }
  119. if ( strResolution == 'medium' ) {
  120. switch ( iScale ) {
  121. case 1:
  122. fLetterSpacing = 0;
  123. break;
  124. case 2:
  125. fLetterSpacing = - 1;
  126. break;
  127. case 3:
  128. fLetterSpacing = - 1.04;
  129. break;
  130. case 4:
  131. case 5:
  132. fLetterSpacing = - 2.1;
  133. break;
  134. }
  135. }
  136. if ( strResolution == 'high' ) {
  137. switch ( iScale ) {
  138. case 1:
  139. case 2:
  140. fLetterSpacing = 0;
  141. break;
  142. case 3:
  143. case 4:
  144. case 5:
  145. fLetterSpacing = - 1;
  146. break;
  147. }
  148. } // can't get a span or div to flow like an img element, but a table works?
  149. // convert img element to ascii
  150. function asciifyImage( canvasRenderer, oAscii ) {
  151. oCtx.clearRect( 0, 0, iWidth, iHeight );
  152. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  153. const oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data; // Coloring loop starts now
  154. let strChars = ''; // console.time('rendering');
  155. for ( let y = 0; y < iHeight; y += 2 ) {
  156. for ( let x = 0; x < iWidth; x ++ ) {
  157. const iOffset = ( y * iWidth + x ) * 4;
  158. const iRed = oImgData[ iOffset ];
  159. const iGreen = oImgData[ iOffset + 1 ];
  160. const iBlue = oImgData[ iOffset + 2 ];
  161. const iAlpha = oImgData[ iOffset + 3 ];
  162. let iCharIdx;
  163. let fBrightness;
  164. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255; // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  165. if ( iAlpha == 0 ) {
  166. // should calculate alpha instead, but quick hack :)
  167. //fBrightness *= (iAlpha / 255);
  168. fBrightness = 1;
  169. }
  170. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  171. if ( bInvert ) {
  172. iCharIdx = aCharList.length - iCharIdx - 1;
  173. } // good for debugging
  174. //fBrightness = Math.floor(fBrightness * 10);
  175. //strThisChar = fBrightness;
  176. let strThisChar = aCharList[ iCharIdx ];
  177. if ( strThisChar === undefined || strThisChar == ' ' ) strThisChar = '&nbsp;';
  178. if ( bColor ) {
  179. strChars += '<span style=\'' + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' ) + ( bAlpha ? 'opacity:' + iAlpha / 255 + ';' : '' ) + '\'>' + strThisChar + '</span>';
  180. } else {
  181. strChars += strThisChar;
  182. }
  183. }
  184. strChars += '<br/>';
  185. }
  186. oAscii.innerHTML = '<tr><td>' + strChars + '</td></tr>'; // console.timeEnd('rendering');
  187. // return oAscii;
  188. }
  189. }
  190. }
  191. THREE.AsciiEffect = AsciiEffect;
  192. } )();