I have an image send by a camera with a websocket and I display it with a canvas, then, I draw on it. I tried to, when the user click on a different place, to delete the previous lines and draw the new ones but, I've read that was impossible to clear a precise part of a canvas, but, with surprise, it's happend to me.
So I wanted to know why it works even if I read multiple time that was impossible.
When I launch the program for the first time and there is no images yet, there is no refresh but when I display the first picture, I can click many times, only the last click will be show and I don't understand why.
Thanks for your help and here a sample of my code
updateImage(){
console.log(this.refs.myCanvas);
const ctx = this.refs.myCanvas.getContext('2d');
let src = this.state.imageSrc;
this.image.onload = () => {
console.log('done')
//ctx.clearRect(0,0,this.refs.myCanvas.width,this.refs.myCanvas.height)
ctx.drawImage(this.image, 0, 0,this.refs.myCanvas.width,this.refs.myCanvas.height); //On dessine l'image
if(this.props.beam_markX != undefined && this.props.beam_markY != undefined){
this.draw_Beam_Marker()
}
}
this.image.src = "data:image/jpg;base64,"+src;
this.componentWillUnmount() //On appel la derniere methode
}
draw_Beam_Marker(e){
const ctx = this.refs.myCanvas.getContext('2d');
ctx.strokeStyle = "#7FFF00" //chartreuse
ctx.font = '15px Arial'
ctx.fillStyle = "#7FFF00"
if(e != undefined){ //Quand on clique sur l'image
if(this.props.beam_markX != e.nativeEvent.offsetX && this.props.beam_markY != e.nativeEvent.offsetY && this.props.crosshair === 0){ //On ne dessine que si nouveaux coordonnes et que le crosshair n'est pas lock
ctx.beginPath();
ctx.moveTo(0, e.nativeEvent.offsetY);
ctx.lineTo(this.refs.myCanvas.width, e.nativeEvent.offsetY);
ctx.stroke();
ctx.moveTo(e.nativeEvent.offsetX, 0);
ctx.lineTo(e.nativeEvent.offsetX, this.refs.myCanvas.height);
ctx.stroke();
ctx.fillText("X: "+e.nativeEvent.offsetX,e.nativeEvent.offsetX+1,e.nativeEvent.offsetY+12);
ctx.fillText("Y: "+e.nativeEvent.offsetY,e.nativeEvent.offsetX+1,e.nativeEvent.offsetY+30);
ctx.fillText("W: "+this.refs.myCanvas.width,e.nativeEvent.offsetX+1,e.nativeEvent.offsetY+48);
ctx.fillText("H: "+this.refs.myCanvas.height,e.nativeEvent.offsetX+1,e.nativeEvent.offsetY+66);
ctx.closePath();
this.props.setBeamMark(e.nativeEvent.offsetX,e.nativeEvent.offsetY);
}
}
else if(this.props.beam_markX != undefined && this.props.beam_markY != undefined ){ //Quand on passe a une nouvelle image
//ctx.clearRect(0,0,this.refs.myCanvas.width,this.refs.myCanvas.height)
ctx.beginPath();
ctx.moveTo(0, this.props.beam_markY);
ctx.lineTo(this.refs.myCanvas.width, this.props.beam_markY);
ctx.stroke();
ctx.moveTo(this.props.beam_markX, 0);
ctx.lineTo(this.props.beam_markX, this.refs.myCanvas.height);
ctx.stroke();
ctx.fillText("X: "+this.props.beam_markX,this.props.beam_markX+1,this.props.beam_markY+12)
ctx.fillText("Y: "+this.props.beam_markY,this.props.beam_markX+1,this.props.beam_markY+30);
ctx.fillText("W: "+this.refs.myCanvas.width,this.props.beam_markX+1,this.props.beam_markY+48);
ctx.fillText("H: "+this.refs.myCanvas.height,this.props.beam_markX+1,this.props.beam_markY+66);
ctx.closePath();
}
}