【发布时间】:2016-10-06 22:48:31
【问题描述】:
我正在学习反应,我正在尝试从反应中渲染画布并在每次状态发生变化时更新它。
//set initial state
constructor(props) {
super(props);
this.state = {
imgFile: this.props.imgFile,
show: false
};
}
//The render canvas
render() {
return (
<div className="col-sm-4">
<canvas ref={(c) => { this.myCanvas = c; }} />
</div>
);
}
我的问题是:有没有办法通过道具更新画布?到目前为止,我得到了这个:
componentDidMount() {
const origin = this.state.imgFile;
// canvas area/img calculations
this.funcForDrawCanvas(origin.path, origin.presition, origin.height, origin.width);
}
但是在安装组件之后不再更新它有什么想法吗?或者有没有办法删除canvas标签并在状态改变时重新放置?
【问题讨论】:
-
确保阅读每个组件规范的作用。有很多方法可以做到这一点,但如何在
componentDidMount上初始化画布,使用shouldComponentUpdate检查道具更改并在componentDidUpdate上进行画布渲染。 -
@Dom 你能给我一个例子,说明如何用 img 重绘 ca canvas 吗?
-
这是您要寻找的答案。 stackoverflow.com/questions/49786505/…
标签: javascript html reactjs canvas