image-to-ascii-art
Version:
convert image to ascii art in browser
1 lines • 3.82 kB
JavaScript
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";function n(t){return null!=t}function i(t,e,r){return Math.round(.299*t+.587*e+.114*r)}r.r(e);class a{constructor(t){this.drawWidth=a.dealDrawParam(t.drawWidth,'"drawWith" config is invalid',1),this.drawHeight=a.dealDrawParam(t.drawHeight,'"drawHeight" config is invalid',1),this.pickDensityHorizontal=a.dealPickDensity(t.pickDensityHorizontal,'"pickDensityHorizontal" config is invalid',1),this.pickDensityVertical=a.dealPickDensity(t.pickDensityVertical,'"pickDensityVertical" config is invalid',1),this.greyRangeChar=a.dealGreyRangeChar(t.greyRangeChar,'"greyRangeChar" config is invalid',[{from:0,to:30,char:"#"},{from:31,to:60,char:"&"},{from:61,to:120,char:"$"},{from:121,to:150,char:"*"},{from:151,to:180,char:"o"},{from:181,to:210,char:"!"},{from:211,to:240,char:";"}]),this.defaultGreyChar=" "}static dealDrawParam(t,e,r){if(!n(t))return r;if(isNaN(t)||t<=0)throw new Error(e);return t>1&&(t=Math.floor(t)),t}static dealPickDensity(t,e,r){if(!n(t))return r;if(isNaN(t)||t<=1)throw new Error(e);return Math.floor(t)}static dealGreyRangeChar(t,e,r){if(!n(t))return r;const i=[];for(let r=0;r<t.length;r++){if(t[r].from>t[r].to)throw new Error(e);i.push({from:Math.floor(t[r].from),to:Math.floor(t[r].to),char:t[r].char})}return i}}r.d(e,"ImageToAsciiArt",function(){return o});class o{constructor({canvas:t,config:e={}}={}){t instanceof HTMLCanvasElement?(this.canvas=t,this.canvasIsStable=!0):(this.canvas=document.createElement("canvas"),this.canvas.style.display="none",document.body.appendChild(this.canvas)),this.canvasCtx=this.canvas.getContext("2d"),this.setConfig(e)}setConfig(t){this.config=new a(t)}convert(t){let e;return t instanceof HTMLImageElement?e=t:(e=new Image).src=t,new Promise(t=>{let r=()=>{e.removeEventListener("load",r);let n=this.config.drawWidth<=1?this.config.drawWidth*e.naturalWidth:this.config.drawWidth,a=this.config.drawHeight<=1?this.config.drawHeight*e.naturalHeight:this.config.drawHeight;this.canvasIsStable||(this.canvas.width=n,this.canvas.height=a),this.canvasCtx.drawImage(e,0,0,n,a);const o=this.canvasCtx.getImageData(0,0,n,a),s=o.data,c=o.height,f=o.width;let h=[];for(let t=0;t<c;t+=this.config.pickDensityHorizontal){for(let e=0;e<f;e+=this.config.pickDensityVertical){let r=4*(e+f*t),n=s[r],a=s[r+1],o=s[r+2];h.push(i(n,a,o))}h.push(-1)}t(function(t,e,r=" "){const n={};for(let t=0;t<e.length;t++){const r=e[t];for(let t=r.from;t<=r.to;t++)n[t]=r.char}return t.map(t=>n[t]||r).join("")}(h,[...this.config.greyRangeChar,{from:-1,to:-1,char:"\r\n"}]))};e.complete?r():e.addEventListener("load",r)})}destroy(){this.canvasIsStable||document.body.removeChild(this.canvas)}}}])});