AsciiEffect.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * A class that creates an ASCII effect.
  3. *
  4. * The ASCII generation is based on [jsascii]{@link https://github.com/hassadee/jsascii/blob/master/jsascii.js}.
  5. *
  6. * @three_import import { AsciiEffect } from 'three/addons/effects/AsciiEffect.js';
  7. */
  8. class AsciiEffect {
  9. /**
  10. * Constructs a new ASCII effect.
  11. *
  12. * @param {WebGLRenderer} renderer - The renderer.
  13. * @param {string} [charSet=' .:-=+*#%@'] - The char set.
  14. * @param {AsciiEffect~Options} [options] - The configuration parameter.
  15. */
  16. constructor( renderer, charSet = ' .:-=+*#%@', options = {} ) {
  17. // ' .,:;=|iI+hHOE#`$';
  18. // darker bolder character set from https://github.com/saw/Canvas-ASCII-Art/
  19. // ' .\'`^",:;Il!i~+_-?][}{1)(|/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'.split('');
  20. // Some ASCII settings
  21. const fResolution = options[ 'resolution' ] || 0.15;
  22. const iScale = options[ 'scale' ] || 1;
  23. const bColor = options[ 'color' ] || false;
  24. const bAlpha = options[ 'alpha' ] || false;
  25. const bBlock = options[ 'block' ] || false;
  26. const bInvert = options[ 'invert' ] || false;
  27. const strResolution = options[ 'strResolution' ] || 'low';
  28. let width, height;
  29. const domElement = document.createElement( 'div' );
  30. domElement.style.cursor = 'default';
  31. const oAscii = document.createElement( 'table' );
  32. domElement.appendChild( oAscii );
  33. let iWidth, iHeight;
  34. let oImg;
  35. /**
  36. * Resizes the effect.
  37. *
  38. * @param {number} w - The width of the effect in logical pixels.
  39. * @param {number} h - The height of the effect in logical pixels.
  40. */
  41. this.setSize = function ( w, h ) {
  42. width = w;
  43. height = h;
  44. renderer.setSize( w, h );
  45. initAsciiSize();
  46. };
  47. /**
  48. * When using this effect, this method should be called instead of the
  49. * default {@link WebGLRenderer#render}.
  50. *
  51. * @param {Object3D} scene - The scene to render.
  52. * @param {Camera} camera - The camera.
  53. */
  54. this.render = function ( scene, camera ) {
  55. renderer.render( scene, camera );
  56. asciifyImage( oAscii );
  57. };
  58. /**
  59. * The DOM element of the effect. This element must be used instead of the
  60. * default {@link WebGLRenderer#domElement}.
  61. *
  62. * @type {HTMLDivElement}
  63. */
  64. this.domElement = domElement;
  65. // Throw in ascii library from https://github.com/hassadee/jsascii/blob/master/jsascii.js (MIT License)
  66. function initAsciiSize() {
  67. iWidth = Math.floor( width * fResolution );
  68. iHeight = Math.floor( height * fResolution );
  69. oCanvas.width = iWidth;
  70. oCanvas.height = iHeight;
  71. // oCanvas.style.display = "none";
  72. // oCanvas.style.width = iWidth;
  73. // oCanvas.style.height = iHeight;
  74. oImg = renderer.domElement;
  75. if ( oImg.style.backgroundColor ) {
  76. oAscii.rows[ 0 ].cells[ 0 ].style.backgroundColor = oImg.style.backgroundColor;
  77. oAscii.rows[ 0 ].cells[ 0 ].style.color = oImg.style.color;
  78. }
  79. oAscii.cellSpacing = '0';
  80. oAscii.cellPadding = '0';
  81. const oStyle = oAscii.style;
  82. oStyle.whiteSpace = 'pre';
  83. oStyle.margin = '0px';
  84. oStyle.padding = '0px';
  85. oStyle.letterSpacing = fLetterSpacing + 'px';
  86. oStyle.fontFamily = strFont;
  87. oStyle.fontSize = fFontSize + 'px';
  88. oStyle.lineHeight = fLineHeight + 'px';
  89. oStyle.textAlign = 'left';
  90. oStyle.textDecoration = 'none';
  91. }
  92. const strFont = 'courier new, monospace';
  93. const oCanvasImg = renderer.domElement;
  94. const oCanvas = document.createElement( 'canvas' );
  95. if ( ! oCanvas.getContext ) {
  96. return;
  97. }
  98. const oCtx = oCanvas.getContext( '2d' );
  99. if ( ! oCtx.getImageData ) {
  100. return;
  101. }
  102. let aCharList;
  103. if ( charSet ) {
  104. aCharList = ( charSet ).split( '' );
  105. } else {
  106. const aDefaultCharList = ( ' .,:;i1tfLCG08@' ).split( '' );
  107. const aDefaultColorCharList = ( ' CGO08@' ).split( '' );
  108. aCharList = ( bColor ? aDefaultColorCharList : aDefaultCharList );
  109. }
  110. // Setup dom
  111. const fFontSize = ( 2 / fResolution ) * iScale;
  112. const fLineHeight = ( 2 / fResolution ) * iScale;
  113. // adjust letter-spacing for all combinations of scale and resolution to get it to fit the image width.
  114. let fLetterSpacing = 0;
  115. if ( strResolution == 'low' ) {
  116. switch ( iScale ) {
  117. case 1 : fLetterSpacing = - 1; break;
  118. case 2 :
  119. case 3 : fLetterSpacing = - 2.1; break;
  120. case 4 : fLetterSpacing = - 3.1; break;
  121. case 5 : fLetterSpacing = - 4.15; break;
  122. }
  123. }
  124. if ( strResolution == 'medium' ) {
  125. switch ( iScale ) {
  126. case 1 : fLetterSpacing = 0; break;
  127. case 2 : fLetterSpacing = - 1; break;
  128. case 3 : fLetterSpacing = - 1.04; break;
  129. case 4 :
  130. case 5 : fLetterSpacing = - 2.1; break;
  131. }
  132. }
  133. if ( strResolution == 'high' ) {
  134. switch ( iScale ) {
  135. case 1 :
  136. case 2 : fLetterSpacing = 0; break;
  137. case 3 :
  138. case 4 :
  139. case 5 : fLetterSpacing = - 1; break;
  140. }
  141. }
  142. // can't get a span or div to flow like an img element, but a table works?
  143. // convert img element to ascii
  144. function asciifyImage( oAscii ) {
  145. oCtx.clearRect( 0, 0, iWidth, iHeight );
  146. oCtx.drawImage( oCanvasImg, 0, 0, iWidth, iHeight );
  147. const oImgData = oCtx.getImageData( 0, 0, iWidth, iHeight ).data;
  148. // Coloring loop starts now
  149. let strChars = '';
  150. // console.time('rendering');
  151. for ( let y = 0; y < iHeight; y += 2 ) {
  152. for ( let x = 0; x < iWidth; x ++ ) {
  153. const iOffset = ( y * iWidth + x ) * 4;
  154. const iRed = oImgData[ iOffset ];
  155. const iGreen = oImgData[ iOffset + 1 ];
  156. const iBlue = oImgData[ iOffset + 2 ];
  157. const iAlpha = oImgData[ iOffset + 3 ];
  158. let iCharIdx;
  159. let fBrightness;
  160. fBrightness = ( 0.3 * iRed + 0.59 * iGreen + 0.11 * iBlue ) / 255;
  161. // fBrightness = (0.3*iRed + 0.5*iGreen + 0.3*iBlue) / 255;
  162. if ( iAlpha == 0 ) {
  163. // should calculate alpha instead, but quick hack :)
  164. //fBrightness *= (iAlpha / 255);
  165. fBrightness = 1;
  166. }
  167. iCharIdx = Math.floor( ( 1 - fBrightness ) * ( aCharList.length - 1 ) );
  168. if ( bInvert ) {
  169. iCharIdx = aCharList.length - iCharIdx - 1;
  170. }
  171. // good for debugging
  172. //fBrightness = Math.floor(fBrightness * 10);
  173. //strThisChar = fBrightness;
  174. let strThisChar = aCharList[ iCharIdx ];
  175. if ( strThisChar === undefined || strThisChar == ' ' )
  176. strThisChar = '&nbsp;';
  177. if ( bColor ) {
  178. strChars += '<span style=\''
  179. + 'color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');'
  180. + ( bBlock ? 'background-color:rgb(' + iRed + ',' + iGreen + ',' + iBlue + ');' : '' )
  181. + ( bAlpha ? 'opacity:' + ( iAlpha / 255 ) + ';' : '' )
  182. + '\'>' + strThisChar + '</span>';
  183. } else {
  184. strChars += strThisChar;
  185. }
  186. }
  187. strChars += '<br/>';
  188. }
  189. oAscii.innerHTML = `<tr><td style="display:block;width:${width}px;height:${height}px;overflow:hidden">${strChars}</td></tr>`;
  190. // console.timeEnd('rendering');
  191. // return oAscii;
  192. }
  193. }
  194. }
  195. /**
  196. * This type represents configuration settings of `AsciiEffect`.
  197. *
  198. * @typedef {Object} AsciiEffect~Options
  199. * @property {number} [resolution=0.15] - A higher value leads to more details.
  200. * @property {number} [scale=1] - The scale of the effect.
  201. * @property {boolean} [color=false] - Whether colors should be enabled or not. Better quality but slows down rendering.
  202. * @property {boolean} [alpha=false] - Whether transparency should be enabled or not.
  203. * @property {boolean} [block=false] - Whether blocked characters should be enabled or not.
  204. * @property {boolean} [invert=false] - Whether colors should be inverted or not.
  205. * @property {('low'|'medium'|'high')} [strResolution='low'] - The string resolution.
  206. **/
  207. export { AsciiEffect };