create a 100px 100px black image in javascript

You can create a black 100x100 image using canvas element in JavaScript:

index.tsx
const canvas = document.createElement('canvas');
canvas.width = 100;
canvas.height = 100;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 100, 100);
const image = canvas.toDataURL(); // The image data in base64 encoded string format
266 chars
8 lines

We create a canvas element with width and height of 100px, get its context and fill it with black color. Finally, we can get the image data in base64 encoded string format using toDataURL() method of canvas.

related categories

gistlibby LogSnag