#
Canvas Text Drawing
This script utilizes the ApexPainter
class from the apexify.js
library to draw customizable text on a canvas background.
#
Usage
1.Import the ApexPainter
class from the apexify.js
library:
const { ApexPainter } = require('apexify.js');
import { ApexPainter } from 'apexify.js';
2.Create an instance of the ApexPainter
class:
const painter = new ApexPainter();
3.Define an array of text objects with their respective options:
- You can use your custom image by converting it into buffer and use it in createImage.
const text = [
{
text: 'Hello World',
x: 90,
y: 20,
fontPath: './fonts/customFont.ttf',
fontName: 'fontName',
fontSize: 30,
isBold: true,
color: '#00ff00',
maxWidth: 30,
lineHeight: 10,
textAlign: 'center',
textBaseline: 'alphabetic',
shadow: {
color: 'white',
offsetX: 10,
offsetY: -10,
blur: 10,
opacity: 5,
},
stroke: {
color: 'black',
width: 1,
}
},
// Add other texts and their options here.
];
Note
The option fontPath
should point to the file containing your custom font.
4.Generate the final canvas image with text:
const textBuffer = await painter.createText(text, bufferImage);
console.log(textBuffer); // Returns a buffer image.