【发布时间】:2019-05-14 12:40:00
【问题描述】:
我正在做一个用户上传结构图(工程图)的项目。当我用户双击画布上的预期位置时,语音到文本引擎会打开并收听用户 cmets,然后它会绘制一个具有不同颜色的小圆圈并在该位置填充文本(计数)。我在反应状态下保存 cmets、计数、弧坐标和其他东西,并在带有编辑和删除按钮的组件中显示列表。当用户按下删除按钮时。评论和其他属性被从状态中删除。
我想从画布上移除绘制的弧线。我该怎么做?
我已经尝试过 clearRect。但在这种情况下它不起作用。
请告诉我。
componentDidMount() {
const img = this.refs.image;
const canvas = this.refs.canvas;
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
img.onload = () => {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
ctx.font = "40px Courier";
ctx.fillText('Drawing 1', 200, 100);
}
}
drawCircle(x, y, getcolor) {
const canvas = this.refs.canvas;
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(x, y, 8, 0, Math.PI * 2, false);
ctx.strokeStyle = "black";
ctx.stroke();
ctx.fillStyle = getcolor;
ctx.fill();
ctx.closePath();
// Text
ctx.beginPath();
ctx.font = "20px Arial"
ctx.fillStyle = "#00538a";
ctx.textAlign = "center";
ctx.fillText(this.state.count, x , y - 20);
ctx.fill();
ctx.closePath();
}
remove(id) {
this.setState({
comments: this.state.comments.filter(c => c.id !== id)
});
const canvas = this.refs.canvas;
const ctx = canvas.getContext('2d');
const arc = this.state.comments.filter(c => c.id === id);
let x = arc[0].coordinates.x;
let y = arc[0].coordinates.y
console.log("TCL: Drawing -> remove -> arc", arc[0].coordinates);
ctx.beginPath();
ctx.arc(x, y, 8, 0, Math.PI * 2, false);
ctx.clip();
ctx.clearRect(x-8, y-8, 16,16);
}
谢谢 见面
【问题讨论】:
-
为什么
clearRect在这种情况下不起作用?你能举一个有效的例子吗? -
没有看到你的代码,事情有点难,但理论上每次你在画布上添加/删除这样的弧线时,你都需要重新绘制整个画布。因此,如果您想删除某些内容,请清除画布,再次绘制图表并最终绘制剩余的弧线。
-
我已经添加了一些代码。
-
图表不是画布的一部分?
-
图像在画布中。我也添加了 componentDidMount 代码
标签: javascript reactjs html html5-canvas