Skip to main content

How to access the canvas context?

The canvas node and hence context can be accessed by placing a ref to the element as:

functionApp(){
const chartRef =useRef(null);

useEffect(()=>{
const chart = chartRef.current;

if(chart){
console.log('CanvasRenderingContext2D', chart.ctx);
console.log('HTMLCanvasElement', chart.canvas);
}
},[]);

return<Chartref={chartRef}type='line'data={chartData}/>;
}

See sandbox with working example.

AltStyle によって変換されたページ (->オリジナル) /