Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using library functions in react #58

Closed
prentesvarten opened this issue Jan 11, 2017 · 3 comments
Closed

Example using library functions in react #58

prentesvarten opened this issue Jan 11, 2017 · 3 comments

Comments

@prentesvarten
Copy link

prentesvarten commented Jan 11, 2017

would it be possible to add an example where you illustrate how to use for instance .setOffset on text in a react context?

`
var simpleText = new Konva.Text({
x: stage.getWidth() / 2,
y: 15,
text: 'Simple Text',
fontSize: 30,
fontFamily: 'Calibri',
fill: 'green'
});

// to align text in the middle of the screen, we can set the
// shape offset to the center of the text shape after instantiating it
simpleText.setOffset({
x: simpleText.getWidth() / 2
});
`

@lavrton
Copy link
Member

lavrton commented Jan 11, 2017

You can get reference of your simpleText node in componentDidMount and set offset here.

componentDidMount() {
   this.text.offsetX(text.width() / 2);
}
render() {
  return <Text {...yourAttrs} ref={(node) => { this.text = node; }/>;
}

https://github.com/lavrton/react-konva#getting-reference-to-konva-objects

@lavrton lavrton closed this as completed Jan 25, 2017
@bay007
Copy link

bay007 commented May 22, 2017

Hello; I want to perform move a Rect element on press key in keyboard, I have

componentDidMount() {
   window.addEventListener('keydown', e => {
     //console.log(e);

     this.text.setX(this.text.getX() + 10);
     this.text.setY(this.text.getY() + 10);

     this.text.x += 10;
     this.text.y += 10;
     console.log('fired');
   });
 }

 render = () => (
   <Stage
     width={this.props.width}
     height={this.props.height}
     onContentMousedown={this.onMouseDown}
     onContentMousemove={this.onMouseMove}
     onContentMouseup={this.onMouseUp}
   >
     {this.props.children}
     <Layer>
       <Rect
         ref={node => {
           this.text = node;
         }}
         name="selection"
         x={0}
         y={0}
         width={50}
         height={50}
         fill="rgba(0,0,255,.5)"
       />
     </Layer>
   </Stage>
 );

But my Rect element is not moving on keyDown :( , it still on x=0, y=0.

@lavrton
Copy link
Member

lavrton commented May 24, 2017

@bay007 changing attributes of a node directly is not recommended in react-konva. Store position of a text in the state of your component (or in special state-container like redux). In event listener just update your state and re-render component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants