【发布时间】:2017-05-18 13:07:41
【问题描述】:
我有一个带有 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 对不起我的英语不好,我会尽量准确。首先,这里是说不可能的链接stackoverflow.com/questions/24140805/…。其次,为什么我想知道,这就是为什么当我有一张图片时,只有我点击的最后一行可见,为什么它不像第一张图片那样渲染,没有刷新
-
您确定这是您用于生成以下图像的代码吗?这条语句
this.draw_Beam_Marker()永远不会绘制任何东西,因为参数e始终未定义。我不认为这是最初使用的代码。 -
但是,这实际上是使用和运行的代码,只有在我点击画布时才定义e。
-
我仍然认为这段代码不能说明全部情况,但如果它有效,它就有效——尽管我不同意这种心态。但是看看你对方法
draw_Beam_Marker(e)的定义,它接受一个参数e,但在这里调用时没有任何适当的参数this.draw_Beam_Marker()在draw_Beam_Marker的主体中,它清楚地指出if(e != undefined){ //Quand on clique sur l'image这应该总是错误的当这是 executethis.draw_Beam_Marker()IMO 时,除了您在此处显示的内容之外,您的代码还有其他部分使其工作。
标签: javascript reactjs canvas redux