I am working through the following code:
class Canvas extends React.Component {
componentDidMount() {
const canvas = this.refs.canvas;
const ctx = canvas.getContext("2d");
const img = this.refs.image;
img.onload = () => {
ctx.drawImage(img, 0, 0);
ctx.font = "40px Courier";
ctx.fillText(this.props.text, 210, 75); // THIS IS THE PLACE TEXT IS EMBEDDED INTO THE PICTURE
};
}
render() {
return (
<div>
<canvas ref="canvas" width={640} height={425} />
<img
ref="image"
alt="Stackoverflow56203352"
src={backImg}
className="hidden"
/>
</div>
);
}
}
I am trying to convert it to functional form and my effort has been so far stalled due to lack of replacement of getContext.
i.e my try here:
const Canvas = ({}) => {
const canvas = useRef("")
const ctx = canvas.getContext("2d")
const img = useRef("")
img.onload = () => {
ctx.drawImage(img, 0, 0);
ctx.font = "40px Courier";
ctx.fillText(this.props.text, 210, 75); // THIS IS THE PLACE TEXT IS EMBEDDED INTO THE PICTURE
};
return (
<div>
<canvas ref="canvas" width={640} height={425} />
<img
ref="image"
alt="Stackoverflow56203352"
src={backImg}
className="hidden"
/>
</div>
);
}
doesn't cut it.
The complete sandbox code is at this location