Reputation: 45
I'm starting with node and I've written this program without success. I intend to create an image with dimensions with a color, write text in the image and then save to a file. The program does everything except the text. What am I doing wrong?
let Jimp = require('jimp');
let image = new Jimp(300, 530, 'green', (err, image) => {
if (err) throw err;
});
let message = 'Hello!';
let x = 10;
let y = 10;
let maxWidth = 300;
Jimp.loadFont(Jimp.FONT_SANS_8_BLACK)
.then(font => {
image.print(font, x, y, message);
});
let file = 'new_name' + '.' + image.getExtension();
Upvotes: 3
Views: 17130
Reputation: 1919
@Mario, here's the code from @David-S written using await
for the async
loadFont
:
let Jimp = require('jimp')
let image = new Jimp(300, 530, 'green', (err, image) => {
if (err) throw err
})
let message = 'Hello!'
let x = 10
let y = 10
const font = await Jimp.loadFont(Jimp.FONT_SANS_64_BLACK)
image.print(font, x, y, message)
let file = `new_name.${image.getExtension()}`
image.write(file) // save
Upvotes: 0
Reputation: 21
The code to write the text must be INSIDE the callback, otherwise it may run before the image is actually created.
Upvotes: 2
Reputation: 6705
I think @barro32 may be correct. I added some code to write out the image and it seems to work:
let Jimp = require('jimp')
let image = new Jimp(300, 530, 'green', (err, image) => {
if (err) throw err
})
let message = 'Hello!'
let x = 10
let y = 10
Jimp.loadFont(Jimp.FONT_SANS_64_BLACK)
.then(font => {
image.print(font, x, y, message)
return image
}).then(image => {
let file = `new_name.${image.getExtension()}`
return image.write(file) // save
})
The result was:
Upvotes: 7