AsciiEffect.js 6.6 KB

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