2
0

AsciiEffect.js 6.4 KB

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