Search code examples
konvajsreact-konvakonvajs-reactjs

How can I get image properties like scaleX in react-kova?


Like the example shown for shape, this has x,y,rotation etc. https://konvajs.github.io/docs/select_and_transform/Transform_Events.html

Image in react-konva also extends shape.

How can I get these values for image(with transformer) in react-konva.


Solution

  • You can listen transform event and then read all properties from the image node.

      handleTransform = () => {
        const props = {
          x: this.image.x(),
          y: this.image.y(),
          rotatio: this.image.rotation(),
          width: this.image.width(),
          height: this.image.height(),
          scaleX: this.image.scaleX(),
          scaleY: this.image.scaleY()
        };
        console.log(props);
      };
      render() {
        return (
          <Stage width={window.innerWidth} height={window.innerHeight}>
            <Layer>
              <Image
                image={this.state.image}
                ref={node => {
                  this.image = node;
                }}
                draggable
                onTransform={this.handleTransform}
                onDragMove={this.handleTransform}
              />
              <Transformer
                ref={node => {
                  this.transformer = node;
                }}
              />
            </Layer>
          </Stage>
        );
      }
    

    Demo: https://codesandbox.io/s/wq184owy45