【问题标题】:Auto delete/refresh elements in canvas自动删除/刷新画布中的元素
【发布时间】: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


【解决方案1】:

仅显示最后一次单击,因为您正在绘制图像并且将覆盖整个画布,然后您正在绘制线条this.props.beam_markXthis.props.beam_markY,这只会绘制一条线。您可以这样做的一种方法是每次单击将 x 和 y 值存储在数组中时:清除画布,绘制图像,然后重新绘制线条!这是一个例子:

var canvas = document.querySelectorAll('canvas')[0]
var ctx = canvas.getContext('2d')
var clicks = []
canvas.addEventListener('click', function(event){
    var x = event.clientX
    var y = event.clientY

    clicks.push({
      x:event.clientX,
      y:event.clientY
    })
    
    drawLines()
})

function drawLines(){
	//Make the canvas blank
  ctx.clearRect(0, 0, canvas.width, canvas.height)
  
  //Draw your image here
  //ctx.drawImage
  
  //Draw the lings
  for(var click of clicks){
  		//Y Axis Line
  		drawLine(click.x, 0, click.x, canvas.height)
      //X Axis Line
      drawLine(0, click.y, canvas.width, click.y)
      //Text
      ctx.fillText(click.x + ', ' + click.y,click.x,click.y)
      
  }
}

function drawLine(x1, y1, x2, y2){
			ctx.beginPath();
      ctx.moveTo(x1,y1);
      ctx.lineTo(x2,y2);
      ctx.stroke();
}
html, body{
  margin:0px;
  padding:0px;
}
canvas{
  background:#cdcdcd;
}
<canvas width="200" height="200"></canvas>
<p>Click in the canvas!</p>

希望对你有帮助!! :]

【讨论】:

  • 感谢您花时间提供帮助,但我只有一个问题,“仅显示最后一次点击,因为您正在绘制图像并且将覆盖整个画布”。好的,我明白了,但是当画布与图像重叠时,为什么会出现令人耳目一新的风格?我的意思是,每次我点击某个地方时,图像是否再次绘制,所以覆盖了她之前的“实例”?
  • 是的,画布与普通 html 元素的工作方式不同,从技术上讲,您甚至不需要在这里清除画布,因为图像将占据全部宽度和高度并覆盖所有内容! :] 所以是的,每次绘制图像时它都会覆盖最后一张图像
  • 是的,我理解那部分,但如果我不改变图像,我每次都保持不变,然后我点击了某个地方,旧标记消失了,新标记出现了,我不知道为什么因为我不调用方法来绘制新图像......对不起,我可能是智障......
  • 好的我明白了,我试图操纵我的绘图图像的宽度和高度,你写的是,每次我点击我的图像,即使它是相同的重新绘制,谢谢寻求帮助:)
【解决方案2】:

您的全部问题在于这段代码:

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() 
  }
}

简单地说,这个声明ctx.drawImage(this.image, 0, 0, this.refs.myCanvas.width, this.refs.myCanvas.height); 将覆盖您之前的图纸。唯一保留的标记是加载图像后绘制的标记。

【讨论】:

  • 我试图删除这部分:if(this.props.beam_markX != undefined && this.props.beam_markY != undefined){ this.draw_Beam_Marker() } 结果是,如果我尝试点击某处,标记出现并立即消失,就像画布总是在刷新
  • 好的我明白了,我试图操纵我的绘图图像的宽度和高度,你写的是,每次我点击我的图像,即使它是重新绘制的一样,谢谢寻求帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 2021-12-25
  • 2017-06-07
  • 2017-04-26
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
相关资源
最近更新 更多