I have implemented the worldcloud of this form: [https://www.npmjs.com/package/chartjs-chart-wordcloud] in a tsx project. The cloud is implemented and mostly doing fine. I have useEffect on the chart, but it's only suuposed to run one (maybe twice in debug mode, and that's why I have an if statement). But yes I don't understand why it moves, the original implementation shows no intention of doing this. I am using tsx, and tailwind for CSS.
I see that height is being changed in the console when I am running the code in inspect. I don't know why this is happening. (I have changed the code a bit, by some suggestions from the comments, so updated the code but the same problem)
Here is my code that I have implemented.
import React, { useEffect, useRef } from 'react';
import { WordCloudChart } from 'chartjs-chart-wordcloud';
import { Chart, registerables } from 'chart.js';
import { Chart as ChartJS } from 'chart.js/auto'
Chart.register(...registerables);
export const WordCloud: React.FunctionComponent = () => {
const canvasRef = useRef<HTMLCanvasElement|null>(null);
let canvas : any = canvasRef?.current;
let chart:any;
useEffect(() => {
if (chart) {
chart.destroy();
}
//let canvas : any = document.getElementById("mycanvas");
let canvas : any = canvasRef?.current;
let ctx = canvas.getContext("2d", { willReadFrequently: true });
chart = new WordCloudChart(ctx, {
data: {
// text
labels: ['Hello', 'world', 'normally', 'you', 'want', 'more', 'words', 'than', 'this'],
datasets: [
{
label: 'DS',
// size in pixel
data: [90, 80, 70, 60, 50, 40, 30, 20, 10],
},
],
},
options: {},
});
}, []);
return(
<div className='max-h-10'>
<canvas className='max-h-10 max-w-10' ref={canvasRef}></canvas>
<canvas className='max-h-10 max-w-10' id="mycanvas"></canvas>
<p>Ja du</p>
</div>
);
};