Search code examples
reactjskonvajs

getPointerPosition() of stage in React-Konva


I would like to get cursor position on Stage in React-Konva by getPointerPosition() `handleStageOnMouseMove(e){ console.log(e.getPointerPosition()); }

render(){ return( {this.handleStageOnMouseOver(e)} } > Hello World ); } ` But I found some trouble that is when I move cursor over the program will return 's event instead of 's event.

** I have some method that is use 'ref' attribute to references but I don't know how to get cursor position.


Solution

  • handleMouseMove = (e) => {
      // there are several ways to get stage reference
    
      // first is
      var stage = e.currentTarget;
    
      // or this:
      stage = this.stageRef.getStage();
    
      // or even this:
      stage = e.target.getStage();
    
      this.setState({
        cursor: stage.getPointerPosition()
      });
    }
    

    https://codesandbox.io/s/xp9o58nl2z