diff --git a/src/ViewerCanvas.tsx b/src/ViewerCanvas.tsx index 19d3c81..6b46017 100644 --- a/src/ViewerCanvas.tsx +++ b/src/ViewerCanvas.tsx @@ -14,8 +14,8 @@ export interface ViewerCanvasProps { onResize: () => void; onZoom: (targetX: number, targetY: number, direct: number, scale: number) => void; zIndex: number; - scaleX: 1 | -1; - scaleY: 1 | -1; + scaleX: number; + scaleY: number; loading: boolean; drag: boolean; onCanvasMouseDown: (e: React.MouseEvent) => void; @@ -146,9 +146,8 @@ export default class ViewerCanvas extends React.Component; } if (this.props.loading) { - imgNode = ; + imgNode = ( +
+ +
+ ); } return ( diff --git a/src/ViewerCore.tsx b/src/ViewerCore.tsx index 5888f77..1ec1e3d 100644 --- a/src/ViewerCore.tsx +++ b/src/ViewerCore.tsx @@ -22,8 +22,8 @@ export interface ViewerCoreState { rotate?: number; imageWidth?: number; imageHeight?: number; - scaleX?: 1 | -1; - scaleY?: 1 | -1; + scaleX?: number; + scaleY?: number; loading?: boolean; } @@ -144,8 +144,8 @@ export default class ViewerCore extends React.Component 0 ? 1 : -1; + let directY = this.state.scaleY > 0 ? 1 : -1; + scaleX = this.state.scaleX + scale * direct * directX; + scaleY = this.state.scaleY + scale * direct * directY; + if (Math.abs(scaleX) < 0.1 || Math.abs(scaleY) < 0.1) { + return; + } + top = this.state.top + -direct * diffY / this.state.scaleX * scale * directX; + left = this.state.left + -direct * diffX / this.state.scaleY * scale * directY; + width = this.state.width; + height = this.state.height; } this.setState({ - width: this.state.width + diffWidth, - height: this.state.height + diffHeight, - top: this.state.top + -diffHeight / 2 + -direct * diffY * scale, - left: this.state.left + -diffWidth / 2 + -direct * diffX * scale, + width: width, + scaleX: scaleX, + scaleY: scaleY, + height: height, + top: top, + left: left, loading: false, }); } getImageCenterXY() { return { - x: this.state.left + this.state.width / 2, - y: this.state.top + this.state.height / 2, + x: (this.state.left + this.state.width / 2), + y: (this.state.top + this.state.height / 2), }; } @@ -402,12 +425,18 @@ export default class ViewerCore extends React.Component 0 ? 1 : -1) * this.state.scaleX - 0.11, + ); setTimeout(() => { this.setState({ visible: false, transitionEnd: false, + width: 0, + height: 0, }); }, transitionDuration); return; diff --git a/src/style/index.less b/src/style/index.less index 6f2cfcf..1aebd4f 100644 --- a/src/style/index.less +++ b/src/style/index.less @@ -86,7 +86,6 @@ z-index: @zIndex + 5; > img { display: block; - margin: 15px auto; width: auto; height: auto; user-select: none;