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

Proposal: Add setScale method to API #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Available methods are listed below:
|---|---|---|
|zoomIn|`(zoomSpeed?: number)`|Zoom in from the center of the `PanZoom` container|
|zoomOut|`(zoomSpeed?: number)`|Zoom out from the center of the `PanZoom` container|
|setScale|`(scale?: number)`|Explicitly set the zoom amount via the scale.|
|autoCenter|`(zoom: number, animate?: boolean = true)`|Center and resize the view to fit the `PanZoom` container|
|reset| |Reset the view to it's original state (will not auto center if `autoCenter` is enabled)|
|moveByRatio|`(x: number, y: number, moveSpeedRatio?: number)`|Move the view along `x` or/and `y` axis|
Expand Down
4 changes: 4 additions & 0 deletions src/PanZoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,10 @@ class PanZoom extends React.Component<Props, State> {
this.centeredZoom(1, zoomSpeed)
}

setScale = (scale?: number = 1) => {
this.setState({scale})
}

reset = () => {
this.setState({ x: 0, y: 0, scale: 1, angle: 0 })
}
Expand Down
37 changes: 37 additions & 0 deletions stories/ControllerUI/ScaleControllerUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react'
import Button from './Button'

const ScaleControllerUI = ({ onSetScale }) => {
return (
<div
style={{
border: '2px solid rgba(0,0,0,0.2)',
borderRadius: 4,
overflow: 'hidden',
backgroundColor: 'white',
}}
>
<Button
onClick={() => onSetScale(1)}
style={{
borderBottom: '1px solid #ccc',
}}
>
1
</Button>
<Button
onClick={() => onSetScale(0.75)}
style={{
borderBottom: '1px solid #ccc',
}}
>
.75
</Button>
<Button onClick={() => onSetScale(0.5)}>
.5
</Button>
</div>
)
}

export default ScaleControllerUI
10 changes: 10 additions & 0 deletions stories/PanZoom.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useRef } from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs, boolean, number } from '@storybook/addon-knobs';
import ZoomControllerUI from './ControllerUI/ZoomControllerUI'
import ScaleControllerUI from './ControllerUI/ScaleControllerUI'
import PadControllerUI from './ControllerUI/PadControllerUI'
import ResetControllerUI from './ControllerUI/ResetControllerUI'
import RotationControllerUI from './ControllerUI/RotationControllerUI'
Expand Down Expand Up @@ -68,6 +69,10 @@ const PanZoomControlUI = (props) => {
panZoom.current && panZoom.current.zoomOut(zoomOutSpeed)
}

function onSetScale(scale) {
panZoom.current && panZoom.current.setScale(scale)
}

function moveByRatio(x, y) {
panZoom.current && panZoom.current.moveByRatio(x, y)
}
Expand Down Expand Up @@ -107,6 +112,11 @@ const PanZoomControlUI = (props) => {
onZoomOut={onZoomOut}
/>
</div>
<div style={{ position: 'absolute', left: 50, top: 8, zIndex: 1 }}>
<ScaleControllerUI
onSetScale={onSetScale}
/>
</div>

<div style={{ position: 'absolute', right: 8, top: 8, zIndex: 1 }}>
<RotationControllerUI
Expand Down