[英]the whole text gets cleared when trying to clear the selected word or character
I am using fabric js with reactjs for adding the text to canvas. 我正在将fabric js与reactjs一起用于将文本添加到画布。 The text is added but when i try to clear the character or selected words, the whole text gets cleared.
添加了文本,但是当我尝试清除字符或所选单词时,整个文本将被清除。 I saw some of the resources regarding this but i am using reactjs so it was hard to resolve this issue for me.
我看到了一些与此相关的资源,但是我正在使用reactjs,因此很难为我解决此问题。 Here is how i have done
这是我做的
class ImageBoard extends React.Component {
constructor() {
super();
this.state = {
selected: undefined
};
}
handleDeleteKey = event => {
if (event.keyCode === 46 || event.keyCode === 8) {
event.preventDefault();
if (this.state.selected !== undefined) {
this.canvas.remove(this.state.selected);
this.setState({ selected: undefined });
}
}
};
componentDidMount() {
this.canvas = new fabric.Canvas("canvas");
document.addEventListener("keydown", this.handleDeleteKey, false);
this.canvas.on("object:selected", e =>
this.setState({ selected: e.target })
);
this.canvas.on("selection:cleared", e =>
this.setState({ selected: undefined })
);
this.canvas.setBackgroundColor(
"rgba(255, 73, 64, 0.6)",
this.canvas.renderAll.bind(this.canvas)
);
}
componentDidUpdate(prevProps) {
if (prevProps.images !== this.props.images) {
this.setCanvasBackground(this.props.images, this.canvas);
}
}
addText = () => {
let text = new fabric.IText("Double click me to change the text!", {
fontSize: 28,
fontWeight: 600,
left: 100,
top: 100,
fill: "black",
width: 20,
height: 20
});
this.canvas.add(text);
};
render() {
return (
<Wrapper>
<Column width={80}>
<canvas
id="canvas"
ref={el => (this.canvasEl = el)}
width={580}
height={400}
className="z-depth-1"
/>
</Column>
<Filter
addText={this.addText}
/>
</Wrapper>
);
}
}
export default connect(mapStateToProps)(ImageBoard);
Since you're removing a selected object in the delete
/ backspace
key handler, it's not unexpected that when you're trying to clear text characters with delete
/ backspace
, the whole text gets removed. 由于要在
delete
/ backspace
键处理程序中delete
选定的对象,因此当您尝试使用delete
/ backspace
清除文本字符时,整个文本都会被删除,这并不意外。 The condition check if (this.state.selected !== undefined)
is not enough because when you enter text editing mode, the text object is still selected. 条件检查
if (this.state.selected !== undefined)
是否不够,因为当您进入文本编辑模式时,仍选择文本对象。
However, you can additionally check IText's isEditing
property to see if the selected object is also in the editing mode: 但是,您还可以检查IText的
isEditing
属性,以查看所选对象是否也处于编辑模式:
handleDeleteKey = event => {
if (event.keyCode === 46 || event.keyCode === 8) {
if (this.state.selected !== undefined && !this.state.selected.isEditing) {
event.preventDefault();
this.canvas.remove(this.state.selected);
this.setState({ selected: undefined });
}
}
};
Here's a working snippet to demonstrate: 这是一个工作片段来演示:
class App extends React.Component { constructor(props) { super(props); this.state = { selected: undefined }; } handleDeleteKey = event => { if (event.keyCode === 46 || event.keyCode === 8) { if (this.state.selected !== undefined && !this.state.selected.isEditing) { event.preventDefault(); this.canvas.remove(this.state.selected); this.setState({ selected: undefined }); } } }; componentDidMount() { this.canvas = new fabric.Canvas("canvas"); document.addEventListener("keydown", this.handleDeleteKey, false); this.canvas.on("object:selected", e => this.setState({ selected: e.target }) ); this.canvas.on("selection:cleared", e => this.setState({ selected: undefined }) ); this.canvas.setBackgroundColor( "rgba(255, 73, 64, 0.6)", this.canvas.renderAll.bind(this.canvas) ); } addText = () => { let text = new fabric.IText("Double click me to change the text!", { fontSize: 28, fontWeight: 600, left: 20, top: 20, fill: "black", width: 20, height: 20 }); this.canvas.add(text); }; render() { return ( <div> <canvas id="canvas" ref={el => (this.canvasEl = el)} width={400} height={170} /> <button onClick={this.addText}> add </button> </div> ); } } ReactDOM.render(<App />, document.getElementById("app"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.6.0/fabric.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id='app'></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.