websocketを使用してカメラで画像を送信し、それをキャンバスで表示してから描画します。ユーザーが別の場所をクリックして前の行を削除して新しい行を描画しようとしましたが、キャンバスの正確な部分をクリアすることは不可能でしたが、驚いたことにそれは私には起こります。キャンバス内の要素を自動的に削除/リフレッシュする
私は不可能な複数の時間を読んでもなぜそれが機能するのか知りたかったのです。
初めてプログラムを起動しても画像が表示されない場合は、更新はありませんが、最初の画像を表示すると何度もクリックすることができ、最後のクリックのみが表示されます理由を理解する。
あなたの助けのおかげで、ここに私のコードのサンプル
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();
}
}
私はあなたの質問を理解していません。もっと簡潔に書いてみてください; tldr。第二に、あなたが読んだ記事をリンクして、キャンバスの正確な部分をクリアすることが不可能であったので、元の意味を理解できるようにしてください。 – EyuelDK
@EyuelDK私の悪い英語で申し訳ありません、私はより正確にしようとします。まず、ここではリンクが可能ではないと言われています。http://stackoverflow.com/questions/24140805/how-to-clear-specific-line-in-canvas-html5第二に、なぜ私が知りたかったのか、それはなぜ私が画像を持っているときに、私が目に見えるようにクリックした最後の行だけで、なぜ最初の画像と同じレンダリングではなく、リフレッシュなしであるのですか? –
これはコードですあなたは下の画像を制作していましたか?この文 'this.draw_Beam_Marker()'は、パラメータeが常に定義されていないので何も描画しません。私はこれが最初に使われたコードだとは思わない。 – EyuelDK