反应konva放大和缩小鼠标指针位置上的鼠标滚动不正常

时间:2018-09-17 09:12:18

标签: javascript reactjs html5-canvas konvajs react-konva

我正在尝试使用react konva在一个omage上绘制矩形 我想在鼠标指针位置和鼠标滚轮事件放大/缩小图像时绘制一个矩形,但是问题是矩形是根据图像的位置绘制的,而不是舞台的,请帮助我解决这个问题

请参考我用于放大和缩小图像的代码

zoom=()=>{
    var oldScale = this.refs.layer.attrs.scaleX;
            mousePointTo = {
                x: this.props.x / oldScale - this.refs.layer.attrs.x / oldScale,
                y: this.props.y/ oldScale - this.refs.layer.attrs.y / oldScale,
            };  
           newScale = this.props.variation > 0 ? oldScale * this.props.scaleBy : oldScale / this.props.scaleBy; 
            newPos = {
                x: -(mousePointTo.x - this.props.x / newScale) * newScale,
                y: -(mousePointTo.y - this.props.y / newScale) * newScale
            }; 

            this.setState({newposx:newPos.x,newposy:newPos.y,newScale:newScale})
    }

handleClick = (e) => {    

    if (this.state.isDrawing) {
      this.setState({
        isDrawing: !this.state.isDrawing,
      });
      return;
    }

    const newShapes = this.state.shapes.slice();
    newShapes.push({
      x: e.evt.layerX, 
      y: e.evt.layerY,
    });

    this.setState({
      isDrawing: true,
      shapes: newShapes,
    });

  }

  handleMouseMove = (e) => {
    if (!this.state.isDrawingMode) {
      return;
    }

    const mouseX = e.evt.layerX;  
    const mouseY = e.evt.layerY;


    if (this.state.isDrawing) {

      const currShapeIndex = this.state.shapes.length - 1;
      const currShape = this.state.shapes[currShapeIndex];


      const newWidth = mouseX - currShape.x;
      const newHeight = mouseY - currShape.y;

      const newShapesList = this.state.shapes.slice();
      newShapesList[currShapeIndex] = {
        x: currShape.x,
        y: currShape.y,
        width: newWidth,
        height: newHeight
      }

      this.setState({
        shapes: newShapesList,
      });
    }
  }

render() {

    return (
        <Stage
          ref='stage'
          width={this.props.width}
          height={this.props.height}
          onContentClick={this.handleClick}
          onContentMouseMove={this.handleMouseMove}
          draggable="true"
        >
          <Layer ref='layer' width={this.props.width}
          height={this.props.height} x={this.state.newposx}
              y={this.state.newposy}
              scaleX={this.state.newScale}
              scaleY={this.state.newScale}>
            <Image 
              width={this.props.width} 
              height={this.props.height} 
              image={this.props.image}  
              ref={(node) => {this.imageNode = node;}}    
            />

            {this.state.shapes.map((shape) => {
              return (
                <Group>
                  <Rect
                    x={shape.x}
                    y={shape.y}
                    width={shape.width}
                    height={shape.height}
                    isDrawingMode={this.state.isDrawingMode}
                    draggable="true"
                    }}
                  />
                </Group >
              );
            })}

          </Layer>
        </Stage>
      </div>
    );
  }
}

0 个答案:

没有答案