-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Background Image - Cannot use loadimage and resize the image #103
Comments
niveKKumar
changed the title
Cannot use loadimage and resize the image
Background Image - Cannot use loadimage and resize the image
May 14, 2022
With svg it works 👀 |
Hi there! This works fine for me: const { ChartJSNodeCanvas } = require('./index');
const { loadImage } = require('canvas');
const fs = require('fs/promises');
async function main() {
const width = 400;
const height = 400;
const configuration = {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
}
};
const buf = await fs.readFile('./banana.jpg');
const img = await loadImage(buf);
const chartJSNodeCanvas = new ChartJSNodeCanvas({
width, height,
chartCallback(c) {
c.register({
id: "logo",
beforeDraw(chart) {
const canvas = chart.canvas;
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalAlpha = 0.5;
const scale = Math.max(canvas.width / img.width, canvas.height / img.height);
// get the top left position of the image
const x = (canvas.width / 2) - (img.width / 2) * scale;
const y = (canvas.height / 2) - (img.height / 2) * scale;
ctx.drawImage(img, x, y, img.width * scale, img.height * scale);
ctx.restore();
},
});
}
});
const buffer = await chartJSNodeCanvas.renderToBuffer(configuration);
await fs.writeFile('./test.png', buffer, 'base64');
}
main(); Looks like you are using canvas.drawImage incorrectly. Also, note the use of |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am trying to load a local image and draw it to my background of my chart. Problem is, it doesnt resize and keeps the original width. Any idea why is that ?
With faktor 1 it should be as big as my canvas. My file has the size 500x500 which it keeps
In addition it seems that drawImage only allows HTMLImageElement, which as far I am aware of you, you cant create with a pure node project
The text was updated successfully, but these errors were encountered: