Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="ascii" style="
    display: grid;
    grid-template-columns: repeat(var(--width), 1fr);
    font-family: 'Courier New', Courier, monospace;
    font-size: 9px;
    width: calc(var(--width) * 5.41px);
    height: calc(var(--height) * 10px);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  ">
</div>
<div id="debug-image"></div>
<div id="debug-chars"></div>
<a id="export" href="#" download="ascii-art.html"></a>

<!-- dribbble - twitter -->
<a class="dribbble" href="https://dribbble.com/TaminoMartinius" target="_blank">
  <img src="https://cdn.dribbble.com/assets/dribbble-ball-mark-2bd45f09c2fb58dbbfb44766d5d1d07c5a12972d602ef8b32204d28fa3dda554.svg" alt=""/>
</a>
<a class="twitter" target="_top" href="https://twitter.com/TaminoMartinius">
  <svg xmlns="http://www.w3.org/2000/svg" width="72" height="72" viewBox="0 0 72 72">
    <path d="M67.812 16.141a26.246 26.246 0 0 1-7.519 2.06 13.134 13.134 0 0 0 5.756-7.244 26.127 26.127 0 0 1-8.313 3.176A13.075 13.075 0 0 0 48.182 10c-7.229 0-13.092 5.861-13.092 13.093 0 1.026.118 2.021.338 2.981-10.885-.548-20.528-5.757-26.987-13.679a13.048 13.048 0 0 0-1.771 6.581c0 4.542 2.312 8.551 5.824 10.898a13.048 13.048 0 0 1-5.93-1.638c-.002.055-.002.11-.002.162 0 6.345 4.513 11.638 10.504 12.84a13.177 13.177 0 0 1-3.449.457c-.846 0-1.667-.078-2.465-.231 1.667 5.2 6.499 8.986 12.23 9.09a26.276 26.276 0 0 1-16.26 5.606A26.21 26.21 0 0 1 4 55.976a37.036 37.036 0 0 0 20.067 5.882c24.083 0 37.251-19.949 37.251-37.249 0-.566-.014-1.134-.039-1.694a26.597 26.597 0 0 0 6.533-6.774z"/>
  </svg>    
</a>
              
            
!

CSS

              
                html,
body {
  padding: 0;
  margin: 0;
  background-color: #ECEFFC;
}

#debug-image canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: calc(var(--width) * 5.41px);
  height: calc(var(--height) * 10px);
  z-index: -1;
}

#debug-chars {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-row-gap: 1px;
  width: 63px;
}

@media screen and (max-width: 800px) {
  .dg {
    display: none;
  }
}

// dribbble - twitter
.dribbble {
  position: fixed;
  display: block;
  right: 20px;
  bottom: 20px;
  img {
    display: block;
    height: 28px;
  }
}
.twitter {
  position: fixed;
  display: block;
  right: 64px;
  bottom: 14px;
  svg {
    width: 32px;
    height: 32px;
    fill: #1da1f2;
  }
}

              
            
!

JS

              
                declare var dat: any;
interface Dict<T> {
  [key: string]: T;
}

enum ColorPalette {
  Monochrome = 'none',
  Grey2Bit = 'grey2bit',
  Grey4Bit = 'grey4bit',
  Grey8Bit = 'grey8bit',
  Color3Bit = 'color3bit',
  Color4Bit = 'color4bit',
  ColorFull = 'color',
}

class AsciiArtGenerator {
  settings = {
    charSet: ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
    url: 'data:image/jpeg;base64,/9j/4AAQSkZJR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fEquQ62a6JCxixCnzePYnwJfMqOPjfRQLqi42cuBjvkXBrXXXbZWHkRj4DWy4/gyxp+f/EACARAAMAAgIDAQEBAAAAAAAAAAABAgMREBIgITAEExT/2gAIAQMBAT8A8tGvt2N/Rs2Rjo/ixwP18mY42QpQ2jRkn5Ix+iDY6HOyhJebMOLY8WjCOBz7Mcpmf8ujq150fipOSlU0N6MWQaF6Mltobe/JDRgt4mf6ZaKsx0TmO40upbSfwS9lT6GhHYx5Cq2i18Jfsb9FrneiMmyn4b52OiLKXcqevCXk0OtDyHY1xjyaGu5c6Jfk78JfDMeTR6syR1ZL8eprmeUiMvUq+/E18NCofDFzPP8A/9k=',
    charSamples: 1,
    size: 50,
    contrast: 0,
    brightness: 0,
    alpha: 0,
    ColorPalette: ColorPalette.Grey2Bit,
    debug: false,
    isDemoRunning: true,
    saveAsHtml: () => {
      this.asciiElement.style.setProperty('--width', this.width.toString());
      this.asciiElement.style.setProperty('--height', this.height.toString());
      const blob = new Blob([this.asciiElement.outerHTML]);
      this.exportElement.href = URL.createObjectURL(blob);
      this.exportElement.click();
    },
  };
  demoDirection = -1;
  demoSettings: { url: string, size: number, charSamples: number }[] = [
    {
      url: 'https://static.taminomartinius.de/avatar.png',
      size: 50,
      charSamples: 1,
    },
    {
      url: 'https://static.taminomartinius.de/slogan.png',
      size: 60,
      charSamples: 1,
    },
    {
      url: 'https://static.taminomartinius.de/codepen.png',
      size: 120,
      charSamples: 2,
    },
  ];
  demoIndex = 0;
  isImageLoaded = false;
  charRegions: Dict<number[]> = {};
  colorMap: number[][] = [];
  valueMap: number[][] = [];
  normalizedMap: number[][] = [];
  width: number = 0;
  height: number = 0;
  cachedUrls: Dict<HTMLImageElement> = {};
  asciiElement: HTMLElement;
  exportElement: HTMLAnchorElement;
  debugImageElement: HTMLElement;
  debugCharsElement: HTMLElement;
  colorPalettes: Dict<number[][]> = {};

  constructor() {
    const elements = this.elements;
    this.asciiElement = elements.asciiElement;
    this.exportElement = elements.exportElement;
    this.debugImageElement = elements.debugImageElement;
    this.debugCharsElement = elements.debugCharsElement;

    this.initGui();
    this.generatePalettes();
    this.analyzeCharRegions();
    this.loadFromUrl();
    this.initDemo();
  }

  initDemo() {
    this.demo();
    const stopDemo = () => {
      this.settings.isDemoRunning = false;
      window.removeEventListener('mousedown', stopDemo);
    }
    window.addEventListener('mousedown', stopDemo)
  }

  initGui() {
    const gui: GUI = new dat.GUI();
    gui.add(this.settings, 'charSet').onChange(() => {
      this.analyzeCharRegions();
      this.generate();
    });
    gui.add(this.settings, 'url').listen().onChange(() => this.loadFromUrl());
    gui.add(this.settings, 'charSamples', 1, 3, 1).listen().onChange(() => {
      this.analyzeCharRegions();
      this.loadFromUrl();
    });
    gui.add(this.settings, 'size', 10, 150, 1).listen().onChange(() => this.loadFromUrl());
    gui.add(this.settings, 'contrast', -1, 1, 0.01).onChange(() => {
      this.normalizeValueMap();
      this.generate();
    });
    gui.add(this.settings, 'brightness', -1, 1, 0.01).listen().onChange(() => {
      this.normalizeValueMap();
      this.generate();
    });
    gui.add(this.settings, 'alpha', -1, 1, 0.01).onChange(() => this.generate());
    gui.add(this.settings, 'ColorPalette', ColorPalette).onChange(() => {
      this.generate();
    });
    gui.add(this.settings, 'debug').onChange(() => {
      this.analyzeCharRegions();
      this.loadFromUrl();
    });
    gui.add(this.settings, 'isDemoRunning').listen().onChange(() => {
      if (this.settings.isDemoRunning) {
        this.demo();
      }
    });
    gui.add(this.settings, 'saveAsHtml')
  }

  get elements() {
    const asciiElement = document.getElementById('ascii');
    if (!asciiElement) throw '#ascii Element is missing';
    const exportElement = document.getElementById('export') as HTMLAnchorElement;
    if (!exportElement) throw '#export Element is missing';
    const debugImageElement = document.getElementById('debug-image');
    if (!debugImageElement) throw '#debug-image Element is missing';
    const debugCharsElement = document.getElementById('debug-chars');
    if (!debugCharsElement) throw '#debug-chars Element is missing';
    return { asciiElement, exportElement, debugImageElement, debugCharsElement };
  }

  generatePalettes() {
    this.colorPalettes[ColorPalette.Grey2Bit] = [
      [0, 0, 0],
      [104, 104, 104],
      [184, 184, 184],
      [255, 255, 255],
    ];

    this.colorPalettes[ColorPalette.Grey4Bit] = [];
    for (let i = 0; i < 16; i += 1) {
      this.colorPalettes[ColorPalette.Grey4Bit].push([i * 17, i * 17, i * 17]);
    }

    this.colorPalettes[ColorPalette.Grey8Bit] = [];
    for (let i = 0; i < 256; i += 1) {
      this.colorPalettes[ColorPalette.Grey8Bit].push([i, i, i]);
    }
    this.colorPalettes[ColorPalette.Color3Bit] = [
      [0, 0, 0],
      [0, 249, 45],
      [0, 252, 254],
      [255, 48, 21],
      [255, 62, 253],
      [254, 253, 52],
      [16, 37, 251],
      [255, 255, 255],
    ];
    this.colorPalettes[ColorPalette.Color4Bit] = [...this.colorPalettes[ColorPalette.Color3Bit]];
    for (let i = 1; i < 8; i += 1) {
      this.colorPalettes[ColorPalette.Color4Bit].push([i * 32, i * 32, i * 32]);
    }
  }

  analyzeChar(char: string) {
    const canvas = document.createElement('canvas');
    canvas.width = 12;
    canvas.height = 12;
    const ctx = canvas.getContext('2d');
    if (!ctx) throw 'context creation failed';
    ctx.font = '12px monospace';
    ctx.fillText(char, 2, 10);
    const data = ctx.getImageData(0, 0, 12, 12).data;
    const values: number[] = [];
    const size = 12 / this.settings.charSamples;
    for (let cellY = 0; cellY < this.settings.charSamples; cellY += 1) {
      for (let cellX = 0; cellX < this.settings.charSamples; cellX += 1) {
        let value = 0;
        for (let posY = 0; posY < size; posY += 1) {
          for (let posX = 0; posX < size; posX += 1) {
            value += data[(cellX * size + posX + (cellY * size + posY) * 12) * 4 + 3];
          }
        }
        values.push(value / (size * size) / 255);
      }
    }
    if (this.settings.debug) {
      this.debugCharsElement.appendChild(canvas);
      for (let cellX = 0; cellX < this.settings.charSamples; cellX += 1) {
        for (let cellY = 0; cellY < this.settings.charSamples; cellY += 1) {
          ctx.fillStyle = `rgba(255, 0, 255, ${values[cellX + cellY * this.settings.charSamples]})`;
          ctx.fillRect(cellX * size, cellY * size, size, size);
        }
      }
      console.log({ char, values });
    }
    this.charRegions[char] = values;
  }

  normalizeCharRegions() {
    let min = 1;
    let max = 0;
    for (const char in this.charRegions) {
      // let value = 0;
      for (const region of this.charRegions[char]) {
        if (min > region) min = region;
        if (max < region) max = region;
        // value += region;
      }
      // value /= this.settings.charSamples * this.settings.charSamples;
      // if (min > value) min = value;
      // if (max < value) max = value;
    }
    if (max > 0 && min != max) {
      const diff = max - min;
      for (const char in this.charRegions) {
        const regions = this.charRegions[char];
        for (let index = 0; index < regions.length; index += 1) {
          regions[index] = (regions[index] - min) * (1 / diff);
        }
      }
    }
    if (this.settings.debug) {
      console.log({ min, max, charRegions: this.charRegions });
    }
  }

  analyzeCharRegions() {
    this.clearElement(this.debugCharsElement);
    this.charRegions = {};
    for (const char of this.settings.charSet) {
      this.analyzeChar(char);
    }
    this.normalizeCharRegions();
  }

  loadFromUrl() {
    this.isImageLoaded = false;
    if (this.cachedUrls[this.settings.url]) {
      this.onImageLoaded(this.cachedUrls[this.settings.url]);
    } else {
      const img = document.createElement('img');
      img.crossOrigin = 'Anonymous';
      img.src = this.settings.url;
      img.addEventListener('load', () => {
        this.cachedUrls[this.settings.url] = img;
        this.onImageLoaded(img);
      });
      img.addEventListener('error', () => {
        const urls = Object.keys(this.cachedUrls);
        if (urls.length > 0) {
          this.onImageLoaded(this.cachedUrls[urls[urls.length - 1]]);
        }
      });
    }
  }

  onImageLoaded(img: HTMLImageElement) {
    this.width = this.settings.size;
    this.height = ~~((img.height / img.width) * this.width / 1.9);
    const canvas = document.createElement('canvas');
    canvas.width = this.width * this.settings.charSamples;
    canvas.height = this.height * this.settings.charSamples;
    const ctx = canvas.getContext('2d');
    if (!ctx) throw 'context creation failed';
    ctx.drawImage(img, 0, 0, this.width * this.settings.charSamples, this.height * this.settings.charSamples);
    this.clearElement(this.debugImageElement);
    if (this.settings.debug) {
      this.debugImageElement.appendChild(canvas);
      console.log({ img, width: this.width, height: this.height });
    }
    document.body.style.setProperty('--width', this.width.toString());
    document.body.style.setProperty('--height', this.height.toString());
    this.generateValueMap(ctx);
    this.isImageLoaded = true;
  }

  generateValueMap(ctx: CanvasRenderingContext2D) {
    this.valueMap = [];
    this.colorMap = [];
    const data = Array.from(ctx.getImageData(0, 0, this.width * this.settings.charSamples, this.height * this.settings.charSamples).data);
    const rowLength = this.width * this.settings.charSamples * 4;
    for (let cellY = 0; cellY < this.height; cellY += 1) {
      for (let cellX = 0; cellX < this.width; cellX += 1) {
        const cell = [];
        const pos = (cellX * this.settings.charSamples) * 4 + (cellY * this.settings.charSamples) * rowLength;
        this.colorMap.push(data.slice(pos, pos + 4));
        for (let posY = 0; posY < this.settings.charSamples; posY += 1) {
          for (let posX = 0; posX < this.settings.charSamples; posX += 1) {
            const pos = (cellX * this.settings.charSamples + posX) * 4 + (cellY * this.settings.charSamples + posY) * rowLength;
            const alpha = data[pos + 3] / 255;
            const values = data.slice(pos, pos + 3);
            const value = 1 - ((values[0] + values[1] + values[2]) / 765 * (alpha) + 1 - alpha);
            if (this.settings.debug) {
              ctx.fillStyle = `rgba(255, 0, 255, ${value})`;
              ctx.fillRect(cellX * this.settings.charSamples + posX, cellY * this.settings.charSamples + posY, 1, 1);
            }
            cell.push(value);
          }
        }
        this.valueMap.push(cell);
      }
    }
    if (this.settings.debug) {
      console.log({ valueMap: this.valueMap, colorMap: this.colorMap });
    }
    this.normalizeValueMap();
    this.generate();
  }

  normalizeValueMap() {
    let min = 1;
    let max = 0;
    for (const regions of this.valueMap) {
      // const value = 0;
      for (const region of regions) {
        if (min > region) min = region;
        if (max < region) max = region;
        // value += region;
      }
      // value /= this.settings.charSamples * this.settings.charSamples;
      // if (min > value) min = value;
      // if (max < value) max = value;
    }
    if (max > 0 && min != max) {
      const diff = max - min;
      this.normalizedMap = [];
      for (const regions of this.valueMap) {
        const normals = Array.from(regions);
        for (let index = 0; index < normals.length; index += 1) {
          normals[index] = (normals[index] - min) * (1 / diff);
          normals[index] = (this.settings.contrast + 1) * (normals[index] - 0.5) + 0.5 + this.settings.brightness;
        }
        this.normalizedMap.push(normals);
      }
    } else {
      this.normalizedMap = this.valueMap;
    }
    if (this.settings.debug) {
      console.log({ min, max, valueMap: this.valueMap });
    }
  }

  getClosestChar(values: number[]) {
    let minDiff = Number.MAX_VALUE;
    let minChar = '';
    for (const char in this.charRegions) {
      const regions = this.charRegions[char];
      let diff = 0;
      for (let index = 0; index < regions.length; index++) {
        diff += Math.abs(regions[index] - values[index]);
      }
      if (diff < minDiff) {
        minDiff = diff;
        minChar = char;
      }
    }
    return minChar;
  }

  clearElement(element: HTMLElement) {
    while (element.firstChild) {
      element.removeChild(element.firstChild);
    }
  }

  arrayToRgba(color: number[]) {
    const r = color[3] > 0 ? ~~color[0] : 255;
    const g = color[3] > 0 ? ~~color[1] : 255;
    const b = color[3] > 0 ? ~~color[2] : 255;
    const a = Math.max(0, Math.min(1, color[3] / 255 + this.settings.alpha));
    return `rgba(${r},${g},${b},${a})`;
  }

  getCharColor(color: number[]) {
    if (this.settings.ColorPalette === ColorPalette.ColorFull) {
      return this.arrayToRgba(color);
    } else {
      let closestColor = [0, 0, 0];
      let minDiff = Number.MAX_VALUE;
      for (const paletteColor of this.colorPalettes[this.settings.ColorPalette]) {
        const diff = Math.abs(color[0] - paletteColor[0]) + Math.abs(color[1] - paletteColor[1]) + Math.abs(color[2] - paletteColor[2]);
        if (diff < minDiff) {
          minDiff = diff;
          closestColor = paletteColor;
        }
      }
      return this.arrayToRgba([...closestColor, color[3]]);
    }
  }

  generate() {
    this.clearElement(this.asciiElement);
    for (let cellY = 0; cellY < this.height; cellY += 1) {
      for (let cellX = 0; cellX < this.width; cellX += 1) {
        const cell = document.createElement('div');
        if (this.settings.ColorPalette !== ColorPalette.Monochrome) {
          cell.style.color = this.getCharColor(this.colorMap[cellX + cellY * this.width]);
        }
        cell.innerHTML = this.getClosestChar(this.normalizedMap[cellX + cellY * this.width]).replace(' ', '&nbsp;');
        this.asciiElement.appendChild(cell);
      }
    }
  }

  demo() {    
    if (this.settings.isDemoRunning) {
      if (this.isImageLoaded) {
        this.settings.brightness += 0.05 * this.demoDirection;
        this.normalizeValueMap();
        this.generate();
        if (this.settings.brightness >= 0.5 || this.settings.brightness <= -1) {
          this.demoDirection *= -1;
        }
        if (this.settings.brightness <= -1) {
          this.demoIndex = (this.demoIndex + 1) % this.demoSettings.length;
          this.settings.url = this.demoSettings[this.demoIndex].url;
          this.settings.size = this.demoSettings[this.demoIndex].size;
          this.settings.charSamples = this.demoSettings[this.demoIndex].charSamples;
          this.analyzeCharRegions();
          this.loadFromUrl();
        }
      }
      requestAnimationFrame(() => this.demo());
    }
  }
}

const generator = new AsciiArtGenerator();
console.log(generator);

              
            
!
999px

Console