File: src/js/readImage.ts

Recommend this page to a friend!
  Classes of Dom Hastings   JS Image to ANSI   src/js/readImage.ts   Download  
File: src/js/readImage.ts
Role: Example script
Content type: text/plain
Description: Example script
Class: JS Image to ANSI
Convert an image pixels to ANSI colour codes
Author: By
Last change:
Date: 2 years ago
Size: 1,061 bytes
 

Contents

Class file image Download
export const readImage = (file: File): Promise<string> => new Promise((resolve, reject) => { const reader = new FileReader(); reader.addEventListener('load', (event) => { const result = event?.target?.result as string; if (!result) { reject(new TypeError('readImage: No file.')); return; } resolve(result); }); reader.readAsDataURL(file); }); export const loadImage = (url: string): Promise<CanvasImageSource> => new Promise((resolve, reject) => { const image = new Image(); image.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKAQMAAAC3/F3+AAAAA1BMVEUAAACnej3aAAAAAXRSTlPHReaPdQAAAApJREFUCNdjwAsAAB4AAdpxxYoAAAAASUVORK5CYII='; image.addEventListener('load', () => resolve(image)); image.addEventListener('error', () => reject(new Error(`Error loading image from URL: '${url}'.`)) ); try { image.setAttribute('crossOrigin', 'Anonymous'); image.src = url; } catch (e) { reject(e); } }); export default readImage;