Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <pre></pre>
              
            
!

CSS

              
                body{
  font-size:5px;
  background-color: #000;
  color:white;
}
pre{
  transform:scale(0.15);
  transform-origin:0% 0%;
}
              
            
!

JS

              
                // 取得圖像載入promise
function loadImage(src){
    let img = new Image();
    // 把resolve暴露給外部變數
    let resolve;
    let loadPromise = new Promise((res)=>{
         resolve = res;
    })
    // 這一步cross-origin是因為我們的圖片是外部來源
    // 如果沒有把外部來源設置為"Anonymous",drawImage方法會排除掉非本地來源的圖片資訊, 導致無法進行下一步繪圖
    img.crossOrigin = "Anonymous";
    img.onload = ()=>{
       resolve(img);
    }
    img.src = src;
  
    return loadPromise;
}

async function getImageDataFromImage(src,ratio = 0.5){
  // 這邊有一個ratio參數是因為我讀取的圖片稍微有點大張
  // 所以我補一個參數讓我可以自己決定要把圖片縮小多少倍率
  const img = await loadImage(src);
  const width = img.width * ratio;
  const height = img.height * ratio;
  // 把外部圖源繪製到架空的canvas上面然後取得imageData
  const cvs = document.createElement('canvas');
  const ctx = cvs.getContext('2d');
      cvs.width = width;
      cvs.height = height;
      ctx.drawImage(img,0,0,width,height);
  const imageData = ctx.getImageData(0,0,width,height);
  
  return imageData;
}

async function turnImageDataIntoGrayscale(src){
   const imageData = await getImageDataFromImage(src);
   const data = imageData.data;
  // 這邊這個loop的用意就在於把channel值依像素順序來執行程序
   for(let i=0;i<data.length;i = i+4){
     const r = data[i];
     const g = data[i+1];
     const b = data[i+2];
     //取得rgb值得平均, 如此一來因為rgb都變成同一個數值
     // 圖像就會變成灰階圖
     const average = (r+g+b)/3
     data[i] = data[i+1] = data[i+2] = average;
   }
  return imageData;
}

async function redrawAsASCII(src){
  const grayscaleImageData = await turnImageDataIntoGrayscale(src);
  // 取用的樣版字元
  const glyphSource = "$@#*。";
  let stringOutput = '';
  for(let i = 0;i<grayscaleImageData.data.length;i = i+4){
     
     const pixelIndex = Math.ceil(i/4);
     // 從像素的次序來判斷該像素是否為右邊緣像素
     const pixelIsRightRimPixel = (pixelIndex + 1) % grayscaleImageData.width === 0;
     // 根據像素的灰階值, 用內插的方式來決定要使用哪一個樣版字元來代表該像素
     const glyphIndex = 
       Math.floor(grayscaleImageData.data[i] / 255 * (glyphSource.length-1));
       
       stringOutput+=glyphSource[glyphIndex];
    
     if(pixelIsRightRimPixel){
       // 如果是最右邊緣像素, 則另外補一個換行符號
        stringOutput+='\n';
     }
  }
  // 把字串填入pre tag
  const text = document.querySelector('pre');
  text.innerHTML = stringOutput;
}




(()=>{
  redrawAsASCII('https://i.imgur.com/52TLlOk.png');
})()
              
            
!
999px

Console