-
Notifications
You must be signed in to change notification settings - Fork 691
/
zoom.js
139 lines (136 loc) · 4.39 KB
/
zoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React from 'react';
import Show from '../components/show';
import { Zoom } from '@vx/zoom';
import { genPhyllotaxis } from '@vx/mock-data';
const bg = '#fcd200';
class ZoomDemo extends React.Component {
constructor(props) {
super(props);
}
render() {
const { width, height, margin } = this.props;
const gen = genPhyllotaxis({ radius: 10, width, height });
const phyllotaxis = [...new Array(1000)].map((d, i) => gen(i));
return (
<Zoom
width={width}
height={height}
scaleXMin={0.25}
scaleXMax={10}
scaleYMin={0.25}
scaleYMax={10}
>
{zoom => {
console.log(zoom);
return (
<div style={{ position: 'relative' }}>
<svg
width={width}
height={height}
style={{ cursor: zoom.isDragging ? 'grabbing' : 'grab' }}
>
<defs>
<linearGradient id="dot-gradient" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" stopColor="#050300" stopOpacity={1} />
<stop offset="100%" stopColor="#050300" stopOpacity={0} />
</linearGradient>
</defs>
<rect width={width} height={height} rx={14} fill={bg} />
<g transform={zoom.toString()}>
{phyllotaxis.map((d, i) => {
const { x, y } = d;
return (
<circle
key={`dot-${i}`}
cx={x}
cy={y}
r={4}
fill={i % 2 === 0 ? `url(#dot-gradient)` : '#050300'}
/>
);
})}
</g>
<rect
width={width}
height={height}
rx={14}
fill="transparent"
onWheel={zoom.handleWheel}
onMouseDown={zoom.dragStart}
onMouseMove={zoom.dragMove}
onMouseUp={zoom.dragEnd}
/>
</svg>
<div
style={{
position: 'absolute',
top: 10,
right: 15,
display: 'flex',
flexDirection: 'column',
alignItems: 'flex-end'
}}
>
<button
style={{
width: 18,
height: 18,
padding: 0,
margin: 0,
textAlign: 'center',
border: 'none',
background: 'white'
}}
onClick={() => zoom.scale({ scaleX: 1.5, scaleY: 1.5 })}
>
+
</button>
<button
style={{
width: 18,
height: 18,
padding: 0,
margin: 0,
textAlign: 'center',
border: 'none',
background: 'white',
borderTop: '1px solid #efefef'
}}
onClick={() => zoom.scale({ scaleX: 0.8, scaleY: 0.8 })}
>
-
</button>
<button onClick={zoom.reset}>reset</button>
<div
style={{
display: 'flex',
flexDirection: 'column',
background: 'white',
fontSize: 9,
fontWeight: 900,
lineHeight: 1,
padding: 2,
marginTop: 1,
alignItems: 'flex-end'
}}
>
<div>{`scaleX: ${zoom.transformMatrix.scaleX.toFixed(2)}`}</div>
<div>{`scaleY: ${zoom.transformMatrix.scaleY.toFixed(2)}`}</div>
<div>{`translateX: ${zoom.transformMatrix.translateX.toFixed(2)}`}</div>
<div>{`translateY: ${zoom.transformMatrix.translateY.toFixed(2)}`}</div>
</div>
</div>
</div>
);
}}
</Zoom>
);
}
}
export default () => {
return (
<Show component={ZoomDemo} title="Zoom">
{``}
</Show>
);
};