I am using Node.js 14.18.1 and need to create an image, jpeg, with my initials using TypeScript, without the use of any 3rd party packages, like canvas.
I have performed this task using canvas, but it doesn't deploy well across multiple Operating Systems in my environment.
Any snippets would be appreciated.
For reference, here is the function I created using the canvas package from the npm registry:
const generateAvatar = ({ text, foregroundColor = 'white', backgroundColor = '#041c2c', height = 42, width = 42 }: {
text: string,
foregroundColor?: string,
backgroundColor?: string,
height?: number,
width?: number
}): Buffer => {
const canvas = createCanvas(width, height);
const context = canvas.getContext('2d');
// Draw background
context.fillStyle = backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
// Draw text
context.font = "1em Arial";
context.fillStyle = foregroundColor;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2);
const buffer = canvas.toBuffer("image/jpeg");
return buffer;
}