Skip to content

Commit d214e64

Browse files
committed
[#164] Added 'zoomToCenter' function.
1 parent a6de905 commit d214e64

29 files changed

+1281
-1204
lines changed

src/components/canvas/handlers/WorkareaHandler.ts

Lines changed: 337 additions & 337 deletions
Large diffs are not rendered by default.

src/components/canvas/handlers/ZoomHandler.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { fabric } from 'fabric';
22

33
import Handler from './Handler';
44
import { VideoObject } from '../objects/Video';
5+
import { FabricObject } from '../utils';
56

67
class ZoomHandler {
78
handler?: Handler;
@@ -12,8 +13,9 @@ class ZoomHandler {
1213

1314
/**
1415
* Zoom to point
16+
*
1517
* @param {fabric.Point} point
16-
* @param {number} zoom
18+
* @param {number} zoom ex) 0 ~ 1. Not percentage value.
1719
*/
1820
public zoomToPoint = (point: fabric.Point, zoom: number) => {
1921
const { minZoom, maxZoom } = this.handler;
@@ -44,6 +46,7 @@ class ZoomHandler {
4446

4547
/**
4648
* Zoom one to one
49+
*
4750
*/
4851
public zoomOneToOne = () => {
4952
const center = this.handler.canvas.getCenter();
@@ -53,6 +56,7 @@ class ZoomHandler {
5356

5457
/**
5558
* Zoom to fit
59+
*
5660
*/
5761
public zoomToFit = () => {
5862
let scaleX;
@@ -77,6 +81,7 @@ class ZoomHandler {
7781

7882
/**
7983
* Zoom in
84+
*
8085
*/
8186
public zoomIn = () => {
8287
let zoomRatio = this.handler.canvas.getZoom();
@@ -87,13 +92,63 @@ class ZoomHandler {
8792

8893
/**
8994
* Zoom out
95+
*
9096
*/
9197
public zoomOut = () => {
9298
let zoomRatio = this.handler.canvas.getZoom();
9399
zoomRatio -= 0.05;
94100
const center = this.handler.canvas.getCenter();
95101
this.zoomToPoint(new fabric.Point(center.left, center.top), zoomRatio);
96102
};
103+
104+
/**
105+
* Zoom to center with object
106+
*
107+
* @param {FabricObject} target If zoomFit true, rescaled canvas zoom.
108+
*/
109+
public zoomToCenterWithObject = (target: FabricObject, zoomFit?: boolean) => {
110+
const { left: canvasLeft, top: canvasTop } = this.handler.canvas.getCenter();
111+
const { left, top, width, height } = target;
112+
const diffTop = canvasTop - (top + height / 2);
113+
const diffLeft = canvasLeft - (left + width / 2);
114+
if (zoomFit) {
115+
let scaleX;
116+
let scaleY;
117+
scaleX = this.handler.canvas.getWidth() / width;
118+
scaleY = this.handler.canvas.getHeight() / height;
119+
if (height > width) {
120+
scaleX = scaleY;
121+
if (this.handler.canvas.getWidth() < width * scaleX) {
122+
scaleX = scaleX * (this.handler.canvas.getWidth() / (width * scaleX));
123+
}
124+
} else {
125+
scaleY = scaleX;
126+
if (this.handler.canvas.getHeight() < height * scaleX) {
127+
scaleX = scaleX * (this.handler.canvas.getHeight() / (height * scaleX));
128+
}
129+
}
130+
this.handler.canvas.setViewportTransform([1, 0, 0, 1, diffLeft, diffTop]);
131+
this.zoomToPoint(new fabric.Point(canvasLeft, canvasTop), scaleX);
132+
} else {
133+
const zoom = this.handler.canvas.getZoom();
134+
this.handler.canvas.setViewportTransform([1, 0, 0, 1, diffLeft, diffTop]);
135+
this.zoomToPoint(new fabric.Point(canvasLeft, canvasTop), zoom);
136+
}
137+
};
138+
139+
/**
140+
* Zoom to center with objects
141+
*
142+
* @param {boolean} [zoomFit] If zoomFit true, rescaled canvas zoom.
143+
* @returns
144+
*/
145+
public zoomToCenter = (zoomFit?: boolean) => {
146+
const activeObject = this.handler.canvas.getActiveObject();
147+
if (!activeObject) {
148+
return;
149+
}
150+
this.zoomToCenterWithObject(activeObject, zoomFit);
151+
};
97152
}
98153

99154
export default ZoomHandler;

src/components/common/EditTable.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Button, Table, Modal, Input, Form } from 'antd';
44
import i18n from 'i18next';
55

66
import Icon from '../icon/Icon';
7-
import { FlexBox } from '../flex';
7+
import { Flex } from '../flex';
88

99
class EditTable extends Component {
1010
static propTypes = {
@@ -176,15 +176,15 @@ class EditTable extends Component {
176176
},
177177
];
178178
return (
179-
<FlexBox flexDirection="column">
180-
<FlexBox justifyContent="flex-end">
179+
<Flex flexDirection="column">
180+
<Flex justifyContent="flex-end">
181181
<Button className="rde-action-btn" shape="circle" onClick={this.handleAdd}>
182182
<Icon name="plus" />
183183
</Button>
184184
<Button className="rde-action-btn" shape="circle" onClick={this.handleClear}>
185185
<Icon name="times" />
186186
</Button>
187-
</FlexBox>
187+
</Flex>
188188
<Table
189189
size="small"
190190
pagination={{
@@ -220,7 +220,7 @@ class EditTable extends Component {
220220
/>
221221
</Form.Item>
222222
</Modal>
223-
</FlexBox>
223+
</Flex>
224224
);
225225
}
226226
}

src/components/flex/Flex.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React, { Component } from 'react';
2+
import Item from './Item';
3+
4+
export interface BoxProps extends React.HTMLAttributes<any> {
5+
display?: 'flex' | 'inline-flex';
6+
flexDirection?: 'column-reverse' | 'column' | 'row-reverse' | 'row';
7+
flexWrap?: 'nowrap' | 'wrap-reverse' | 'wrap';
8+
flexFlow?: string;
9+
justifyContent?: 'center' | 'flex-end' | 'flex-start' | 'space-around' | 'space-between' | 'space-evenly';
10+
alignItems?: 'baseline' | 'center' | 'flex-end' | 'flex-start' | 'stretch';
11+
alignContent?: 'center' | 'flex-end' | 'flex-start' | 'space-around' | 'space-between' | 'stretch';
12+
alignSelf?: 'baseline' | 'center' | 'flex-end' | 'flex-start' | 'stretch';
13+
order?: number;
14+
flexGrow?: number | string;
15+
flexShrink?: number | string;
16+
flexBasis?: number | string;
17+
flex?: number | string;
18+
}
19+
20+
class Flex extends Component<BoxProps> {
21+
static Item: typeof Item;
22+
23+
render() {
24+
const {
25+
flexDirection,
26+
flexWrap,
27+
flexFlow,
28+
justifyContent,
29+
alignItems,
30+
alignContent,
31+
alignSelf,
32+
order,
33+
flexGrow,
34+
flexShrink,
35+
flexBasis,
36+
flex,
37+
style,
38+
children,
39+
...other
40+
} = this.props;
41+
const newStyle = Object.assign(
42+
{},
43+
{
44+
display: 'flex',
45+
flexDirection,
46+
flexWrap,
47+
flexFlow,
48+
justifyContent,
49+
alignItems,
50+
alignContent,
51+
alignSelf,
52+
order,
53+
flexGrow,
54+
flexShrink,
55+
flexBasis,
56+
flex,
57+
},
58+
style,
59+
) as any;
60+
return (
61+
<div
62+
style={Object.keys(newStyle).reduce((prev, key) => {
63+
if (newStyle[key]) {
64+
return Object.assign(prev, { [key]: newStyle[key] });
65+
}
66+
return prev;
67+
}, {})}
68+
{...other}
69+
>
70+
{children}
71+
</div>
72+
);
73+
}
74+
}
75+
76+
Flex.Item = Item;
77+
78+
export default Flex;

src/components/flex/FlexBox.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/components/flex/FlexItem.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/components/flex/Item.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import React from 'react';
2+
3+
export interface ItemProps extends React.HTMLAttributes<any> {
4+
alignSelf?: 'baseline' | 'center' | 'flex-end' | 'flex-start' | 'stretch';
5+
order?: number;
6+
flexGrow?: number | string;
7+
flexShrink?: number | string;
8+
flexBasis?: number | string;
9+
flex?: number | string;
10+
}
11+
12+
const Item: React.SFC<ItemProps> = props => {
13+
const { alignSelf, order, flexGrow, flexShrink, flexBasis, flex, style, children, ...other } = props;
14+
const newStyle = Object.assign(
15+
{},
16+
{
17+
alignSelf,
18+
order,
19+
flexGrow,
20+
flexShrink,
21+
flexBasis,
22+
flex,
23+
},
24+
style,
25+
) as any;
26+
return (
27+
<div
28+
style={Object.keys(newStyle).reduce((prev, key) => {
29+
if (newStyle[key]) {
30+
return Object.assign(prev, { [key]: newStyle[key] });
31+
}
32+
return prev;
33+
}, {})}
34+
{...other}
35+
>
36+
{children}
37+
</div>
38+
);
39+
};
40+
41+
export default Item;

src/components/flex/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/components/flex/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as Flex } from './Flex';

0 commit comments

Comments
 (0)