MrBlenny / react-flow-chart

🌊 A flexible, stateless, declarative flow chart library for react.
https://mrblenny.github.io/react-flow-chart/index.html
MIT License
1.46k stars 307 forks source link

Change the zoom point #150

Open georginaso opened 4 years ago

georginaso commented 4 years ago

Thank you for your great job! 👏

Actually, the zoom point is in the top-left corner:

zoom-point-center

It would be great to use the center of the screen (or allow configuration for it).

altran-fabian commented 4 years ago

or maybe it's possible having the zoom-center where the mouse arrow is? i think this is the most common behaviour for zooming, isn't it? (at least for mouse-wheel/gesture zooming)

georginaso commented 4 years ago

@altran-fabian I guess that the desired behavior is the Google maps one 🤩. Using the buttons, the zoom point is the center of the screen, and using the mouse, is the cursor.

ajuhos commented 4 years ago

Here is a basic solution, which you can use without changing the library (by the way, it can be a great start point for a PR):

componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<IChart>) {
    if(this.state.scale !== this.props.scale) {
        const div = document.getElementById('graph-wrapper') as any;
        const centerX = div.offsetWidth / 2 - this.state.offset.x;
        const centerY = div.offsetHeight / 2 - this.state.offset.y;
        const deltaX = centerX/this.props.scale - centerX/this.state.scale;
        const deltaY = centerY/this.props.scale - centerY/this.state.scale;
        this.setState(state => ({
            ...state,
            scale: this.props.scale,
            offset: {
                x: this.state.offset.x + deltaX*this.props.scale,
                y: this.state.offset.y + deltaY*this.props.scale
            }
        }))
    }
}

Requirements:

georginaso commented 4 years ago

@ajuhos thank you :) I'm gonna try!

el-j commented 4 years ago

@ajuhos thanks for this solution :) i will try to use your example as a starting point for a react-hooks implementation.