AsciiEffect.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. console.warn( "THREE.AsciiEffect: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author zz85 / https://github.com/zz85
  4. *
  5. * Ascii generation is based on http://www.nihilogic.dk/labs/jsascii/
  6. * Maybe more about this later with a blog post at http://lab4games.net/zz85/blog
  7. *
  8. * 16 April 2012 - @blurspline
  9. */
  10. THREE.AsciiEffect = function ( renderer, charSet, options ) {
  11. // its fun to create one your own!
  12. charSet = ( charSet === undefined ) ? ' .:-=+*#%@' : charSet;
  13. // ' .,:;=|iI+hHOE#`$';
  14. // darker bolder character set from https://github.com/saw/Canvas-ASCII-Art/
  15. // ' .\'`^",:;Il!i~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'.split('');
  16. if ( ! options ) options = {};
  17. // Some ASCII settings
  18. var bResolution = ! options[ 'resolution' ] ? 0.15 : options[ 'resolution' ]; // Higher for more details
  19. var iScale = ! options[ 'scale' ] ? 1 : options[ 'scale' ];
  20. var bColor = ! options[ 'color' ] ? false : options[ 'color' ]; // nice but slows down rendering!
  21. var bAlpha = ! options[ 'alpha' ] ? false : options[ 'alpha' ]; // Transparency
  22. var bBlock = ! options[ 'block' ] ? false : options[ 'block' ]; // blocked characters. like good O dos
  23. var bInvert = ! options[ 'invert' ] ? false : options[ 'invert' ]; // black is white, white is black
  24. var strResolution = 'low';
  25. var width, height;
  26. var domElement = document.createElement( 'div' );
  27. domElement.style.cursor = 'default';
  28. var oAscii = document.createElement( "table" );
  29. domElement.appendChild( oAscii );
  30. var iWidth, iHeight;
  31. var oImg;
  32. this.setSize = function ( w, h ) {
  33. width = w;
  34. height = h;
  35. renderer.setSize( w, h );
  36. initAsciiSize();
  37. };
  38. this.render = function ( scene, camera ) {
  39. renderer.render( scene, camera );
  40. asciifyImage( renderer, oAscii );
  41. };
  42. this.domElement = domElement;
  43. // Throw in ascii library from http://www.nihilogic.dk/labs/jsascii/jsascii.js
  44. /*
  45. * jsAscii 0.1
  46. * Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
  47. * MIT License [http://www.nihilogic.dk/licenses/mit-license.txt]
  48. */
  49. function initAsciiSize() {
  50. iWidth = Math.round( width * fResolution );
  51. iHeight = Math.round( height * fResolution );
  52. oCanvas.width = iWidth;
  53. oCanvas.height = iHeight;
  54. // oCanvas.style.display = "none";
  55. // oCanvas.style.width = iWidth;
  56. // oCanvas.style.height = iHeight;
  57. oImg = renderer.domElement;
  58. if ( oImg.style.backgroundColor ) {
  59. oAscii.rows[ 0 ].cells[ 0 ].style.backgroundColor = oImg.style.backgroundColor;
  60. oAscii.rows[ 0 ].cells[ 0 ].style.color = oImg.style.color;
  61. }
  62. oAscii.cellSpacing = 0;
  63. oAscii.cellPadding = 0;
  64. var oStyle = oAscii.style;
  65. oStyle.display = "inline";
  66. oStyle.width = Math.round( iWidth / fResolution * iScale ) + "px";
  67. oStyle.height = Math.round( iHeight / fResolution * iScale ) + "px";
  68. oStyle.whiteSpace = "pre";
  69. oStyle.margin = "0px";
  70. oStyle.padding = "0px";
  71. oStyle.letterSpacing = fLetterSpacing + "px";
  72. oStyle.fontFamily = strFont;
  73. oStyle.fontSize = fFontSize + "px";
  74. oStyle.lineHeight = fLineHeight + "px";
  75. oStyle.textAlign = "left";
  76. oStyle.textDecoration = "none";
  77. }
  78. var aDefaultCharList = ( " .,:;i1tfLCG08@" ).split( "" );
  79. var aDefaultColorCharList = ( " CGO08@" ).split( "" );
  80. var strFont = "courier new, monospace";
  81. var oCanvasImg = renderer.domElement;
  82. var oCanvas = document.createElement( "canvas" );
  83. if ( ! oCanvas.getContext ) {
  84. return;
  85. }
  86. var oCtx = oCanvas.getContext( "2d" );
  87. if ( ! oCtx.getImageData ) {
  88. return;
  89. }
  90. var aCharList = ( bColor ? aDefaultColorCharList : aDefaultCharList );
  91. if ( charSet ) aCharList = charSet;
  92. var fResolution = 0.5;
  93. switch ( strResolution ) {
  94. case "low" : fResolution = 0.25; break;
  95. case "medium" : fResolution = 0.5; break;
  96. case "high" : fResolution = 1; break;
  97. }
  98. if ( bResolution ) fResolution = bResolution;
  99. // Setup dom
  100. var fFontSize = ( 2 / fResolution ) * iScale;
  101. var fLineHeight = ( 2 / fResolution ) * iScale;
  102. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  103. var fLetterSpacing = 0;
  104. if ( strResolution == "low" ) {
  105. switch ( iScale ) {
  106. case 1 : fLetterSpacing = - 1; break;
  107. case 2 :
  108. case 3 : fLetterSpacing = - 2.1; break;
  109. case 4 : fLetterSpacing = - 3.1; break;
  110. case 5 : fLetterSpacing = - 4.15; break;
  111. }
  112. }
  113. if ( strResolution == "medium" ) {
  114. switch ( iScale ) {
  115. case 1 : fLetterSpacing = 0; break;
  116. case 2 : fLetterSpacing = - 1; break;
  117. case 3 : fLetterSpacing = - 1.04; break;
  118. case 4 :
  119. case 5 : fLetterSpacing = - 2.1; break;
  120. }
  121. }
  122. if ( strResolution == "high" ) {
  123. switch ( iScale ) {
  124. case 1 :
  125. case 2 : fLetterSpacing = 0; break;
  126. case 3 :
  127. case 4 :
  128. case 5 : fLetterSpacing = - 1; break;
  129. }
  130. }
  131. // can't get a span or div to flow like an img element, but a table works?
  132. // convert img element to ascii
  133. function asciifyImage( canvasRenderer, oAscii ) {
  134. oCtx.clearRect( 0, 0, iWidth, iHeight );
  135. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  136. var oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  137. // Coloring loop starts now
  138. var strChars = "";
  139. // console.time('rendering');
  140. for ( var y = 0; y < iHeight; y += 2 ) {
  141. for ( var x = 0; x < iWidth; x ++ ) {
  142. var iOffset = ( y * iWidth + x ) * 4;
  143. var iRed = oImgData[ iOffset ];
  144. var iGreen = oImgData[ iOffset + 1 ];
  145. var iBlue = oImgData[ iOffset + 2 ];
  146. var iAlpha = oImgData[ iOffset + 3 ];
  147. var iCharIdx;
  148. var fBrightness;
  149. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  150. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  151. if ( iAlpha == 0 ) {
  152. // should calculate alpha instead, but quick hack :)
  153. //fBrightness *= (iAlpha / 255);
  154. fBrightness = 1;
  155. }
  156. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  157. if ( bInvert ) {
  158. iCharIdx = aCharList.length - iCharIdx - 1;
  159. }
  160. // good for debugging
  161. //fBrightness = Math.floor(fBrightness * 10);
  162. //strThisChar = fBrightness;
  163. var strThisChar = aCharList[ iCharIdx ];
  164. if ( strThisChar === undefined || strThisChar == " " )
  165. strThisChar = "&nbsp;";
  166. if ( bColor ) {
  167. strChars += "<span style='"
  168. + "color:rgb(" + iRed + "," + iGreen + "," + iBlue + ");"
  169. + ( bBlock ? "background-color:rgb(" + iRed + "," + iGreen + "," + iBlue + ");" : "" )
  170. + ( bAlpha ? "opacity:" + ( iAlpha / 255 ) + ";" : "" )
  171. + "'>" + strThisChar + "</span>";
  172. } else {
  173. strChars += strThisChar;
  174. }
  175. }
  176. strChars += "<br/>";
  177. }
  178. oAscii.innerHTML = "<tr><td>" + strChars + "</td></tr>";
  179. // console.timeEnd('rendering');
  180. // return oAscii;
  181. }
  182. // end modified asciifyImage block
  183. };