Hello there! I'm trying to center printed text! Am I doing something wrong?
Text isn't centered!
var Jimp = require('jimp');
var fileName = 'test.png';
var imageCaption = '12';
var loadedImage;
Jimp.read(fileName, (err, image) => {
if (err) throw err;
Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
// load font from .fnt file
image
.print(font, 0, 0, {
text: imageCaption,
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
})
.write(fileName); // save
});
});
Screenshots
When you supply HORIZONTAL_ALIGN_CENTER and VERTICAL_ALIGN_MIDDLE you have to also supply maxHeight and maxWidth to tell the print function where to center in.
const Jimp = require('jimp');
const fileName = 'test.png';
const imageCaption = '12';
let loadedImage;
Jimp.read(fileName, (err, image) => {
if (err) {
throw err;
}
Jimp.loadFont(Jimp.FONT_SANS_16_BLACK).then(font => {
// load font from .fnt file
image
.print(
font,
0,
0,
{
text: imageCaption,
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
},
image.bitmap.width,
image.bitmap.height
)
.write(fileName); // save
});
});
Most helpful comment
When you supply HORIZONTAL_ALIGN_CENTER and VERTICAL_ALIGN_MIDDLE you have to also supply maxHeight and maxWidth to tell the print function where to center in.