get-ascii-image

Frontend only package, which converts image source into ASCII art

Usage no npm install needed!

<script type="module">
  import getAsciiImage from 'https://cdn.skypack.dev/get-ascii-image';
</script>

README

Cover Image

get-ascii-image

A zero dependency image to ascii convertor for Web๐ŸŒ



Note : This is a frontend only package, As this package uses Canvas API of the DOM.


๐Ÿงฐ Installation

Using npm or yarn ๐Ÿ“ฆ

# Using npm
npm i get-ascii-image

# Using yarn
yarn add get-ascii-image

Using CDN โšก

Include the js script in your head tag as follows:

<script src="https://unpkg.com/get-ascii-image/index.js" />

Or you can download the package from here, extract it and include the path to the index.js in your head tag

<script src="path/to/get-ascii-image/index.js" />

๐Ÿฐ Example

const getAsciiImage = require('get-ascii-image');

const imageURL = 'https://example.com/image.jpg';

const config = {
    maxWidth:100,
    maxHeight:300
};

getAsciiImage(imageURL,config)
    .then(ascii=>{
        document.getElementById('image-container').innerText = ascii;
    })
    .catch(err=>{
        console.log(err);
    }
});

๐Ÿงต Syntax

Using async-await โœ‹๐Ÿป

const getAsciiImage = require("get-ascii-image");

let asciiImage;

//inside an async function
try {
  //Returns Ascii Image as a string
  asciiImage = await getAsciiImage(imageSource, config);
} catch (err) {
  console.log(err);
}

Using Promise ๐Ÿค๐Ÿป

const getAsciiImage = require("get-ascii-image");

let asciiImage;

getAsciiImage(imageSource, config)
  .then((ascii) => {
    asciiImage = ascii;
  })
  .catch((err) => {
    console.log(err);
  });

๐Ÿฌ Parameters

  • imageSource (String) : The absolute path to the image source.

  • config (Function) : Optional. An object with 3 values namely maxWidth and maxHeight and avoidedCharacters.

๐Ÿ”ง Configuration

Pass the configurations to the function using the config parameter. The config is an object which can contain the following values.

  • maxWidth (number) : Maximum ascii characters in one row of generated Ascii image. Default value is 300
  • maxHeight (number) : Maximum ascii characters in one column of generated Ascii image . Default value is 500
  • avoidedCharacters (String Array) : Add all characters you want to avoid in the output, to this array.

Here is an example of a config object :

const config = {
  maxWidth: 400,
  maxHeight: 700,
  avoidedCharacters: ["#", "a"],
};

๐Ÿ”— Miscellaneous

  • In case you are taking the image from an external link, make sure the response to the link request contains the header Access-Control-Allow-Origin: * in it.

๐Ÿ“œ License

MIT License

Copyright (c) 2020 Aromal Anil

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

โœ๐Ÿป Author

Aromal Anil