forked from ValentinH/react-easy-crop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
327 lines (295 loc) · 9.93 KB
/
index.tsx
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import queryString from 'query-string'
import * as React from 'react'
import ReactDOM from 'react-dom'
import debounce from 'lodash/debounce'
import Cropper from '../../src/index'
import { Area, Point } from '../../src/types'
import './styles.css'
const TEST_IMAGES = {
'/images/dog.jpeg': 'Landscape',
'/images/flower.jpeg': 'Portrait',
'/images/cat.jpeg': 'Small portrait',
// Photos used in tests, used to verify values:
'/images/2000x1200.jpeg': '2000x1200',
}
const urlArgs = queryString.parse(window.location.search)
const imageSrcFromQuery =
typeof urlArgs.img === 'string' ? urlArgs.img : Object.keys(TEST_IMAGES)[0] // so we can change the image from our tests
type HashType = 'percent' | 'pixel'
type State = {
imageSrc: string
crop: Point
rotation: number
flip: { horizontal: boolean; vertical: boolean }
hashType: HashType
zoom: number
aspect: number
cropShape: 'rect' | 'round'
showGrid: boolean
zoomSpeed: number
restrictPosition: boolean
croppedArea: Area | null
croppedAreaPixels: Area | null
initialCroppedAreaPercentages: Area | undefined
initialCroppedAreaPixels: Area | undefined
}
const hashNames = ['imageSrc', 'hashType', 'x', 'y', 'width', 'height', 'rotation'] as const
const debouncedUpdateHash = debounce(
({ hashType, croppedArea, croppedAreaPixels, imageSrc, rotation }: State) => {
if (hashType === 'percent') {
if (croppedArea) {
window.location.hash = `${imageSrc},percent,${croppedArea.x},${croppedArea.y},${croppedArea.width},${croppedArea.height},${rotation}`
}
} else {
if (croppedAreaPixels) {
window.location.hash = `${imageSrc},pixel,${croppedAreaPixels.x},${croppedAreaPixels.y},${croppedAreaPixels.width},${croppedAreaPixels.height},${rotation}`
}
}
},
150
)
class App extends React.Component<{}, State> {
constructor(props: {}) {
super(props)
let rotation = 0
let initialCroppedAreaPercentages: Area | undefined = undefined
let initialCroppedAreaPixels: Area | undefined = undefined
let hashType: HashType = 'percent'
let imageSrc = imageSrcFromQuery
if (window && !urlArgs.setInitialCrop) {
const hashArray = window.location.hash.slice(1).split(',')
if (hashArray.length === hashNames.length) {
const hashInfo = {} as Record<typeof hashNames[number], string>
hashNames.forEach((key, index) => (hashInfo[key] = hashArray[index]))
const {
rotation: rotationFromHash,
hashType: hashTypeFromHash,
imageSrc: imageSrcFromHash,
...croppedArea
} = hashInfo
rotation = parseFloat(rotationFromHash)
imageSrc = imageSrcFromHash
// create a new object called parsedCroppedArea with values converted to floats
const parsedCroppedArea = {
x: parseFloat(croppedArea.x),
y: parseFloat(croppedArea.y),
width: parseFloat(croppedArea.width),
height: parseFloat(croppedArea.height),
} as Area
if (hashTypeFromHash === 'percent') {
initialCroppedAreaPercentages = parsedCroppedArea
} else {
initialCroppedAreaPixels = parsedCroppedArea
hashType = 'pixel'
}
}
}
this.state = {
imageSrc,
crop: { x: 0, y: 0 },
rotation,
flip: { horizontal: false, vertical: false },
hashType,
zoom: 1,
aspect: 4 / 3,
cropShape: 'rect',
showGrid: true,
zoomSpeed: 1,
restrictPosition: true,
croppedArea: null,
croppedAreaPixels: null,
initialCroppedAreaPercentages,
initialCroppedAreaPixels,
}
}
onCropChange = (crop: Point) => {
this.setState({ crop })
}
onCropComplete = (croppedArea: Area, croppedAreaPixels: Area) => {
console.log('onCropComplete!', croppedArea, croppedAreaPixels)
this.setState({ croppedArea, croppedAreaPixels }, this.updateHash)
}
onCropAreaChange = (croppedArea: Area, croppedAreaPixels: Area) => {
console.log('onCropAreaChange!', croppedArea, croppedAreaPixels)
this.setState({ croppedArea, croppedAreaPixels })
}
updateHash = () => {
if (urlArgs.setInitialCrop) {
return
}
debouncedUpdateHash(this.state)
}
onZoomChange = (zoom: number) => {
this.setState({ zoom })
}
onRotationChange = (rotation: number) => {
this.setState({ rotation })
}
onInteractionStart = () => {
console.log('user interaction started')
}
onInteractionEnd = () => {
console.log('user interaction ended')
}
onHashTypeChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
this.setState({ hashType: e.target.value as HashType }, this.updateHash)
}
onImageSrcChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
this.setState({
imageSrc: e.target.value,
initialCroppedAreaPercentages: undefined,
initialCroppedAreaPixels: undefined,
})
}
render() {
return (
<div className="App">
<div className="controls">
<div>
<label>
<input
type="range"
min={0}
max={360}
list="rotation-detents"
value={this.state.rotation}
onChange={({ target: { value: rotation } }) =>
this.setState({ rotation: Number(rotation) })
}
/>
{this.state.rotation}°
</label>
<datalist id="rotation-detents">
<option value="90" />
<option value="180" />
<option value="270" />
</datalist>
</div>
<div>
<label>
<input
type="checkbox"
checked={this.state.flip.horizontal}
onChange={() =>
this.setState((prev) => ({
rotation: 360 - prev.rotation,
flip: {
horizontal: !prev.flip.horizontal,
vertical: prev.flip.vertical,
},
}))
}
/>
Flip Horizontal
</label>
<label>
<input
type="checkbox"
checked={this.state.flip.vertical}
onChange={() =>
this.setState((prev) => ({
rotation: 360 - prev.rotation,
flip: {
horizontal: prev.flip.horizontal,
vertical: !prev.flip.vertical,
},
}))
}
/>
Flip Vertical
</label>
<div>
<label>
Save to hash:
<select value={this.state.hashType} onChange={this.onHashTypeChange}>
<option value="percent">Percent</option>
<option value="pixel">Pixel</option>
</select>
</label>
</div>
<div>
<label>
Picture:
<select value={this.state.imageSrc} onChange={this.onImageSrcChange}>
{Object.entries(TEST_IMAGES).map(([key, value]) => (
<option key={key} value={key}>
{value}
</option>
))}
</select>
</label>
</div>
</div>
<button
id="horizontal-center-button"
onClick={() => {
this.setState({
crop: { ...this.state.crop, x: 0 },
})
}}
>
Center Horizontally
</button>
<div>
crop: {this.state.crop.x}, {this.state.crop.y}
<br />
zoom: {this.state.zoom}
</div>
<div>
<p>Crop Area:</p>
<div>
{(['x', 'y', 'width', 'height'] as const).map((attribute) => {
if (!this.state.croppedArea) {
return null
}
return (
<div key={attribute}>
{attribute}:
<b id={`crop-area-${attribute}`}>
{Math.round(this.state.croppedArea[attribute])}
</b>
</div>
)
})}
</div>
</div>
</div>
<div className="crop-container">
<Cropper
image={this.state.imageSrc}
crop={this.state.crop}
rotation={this.state.rotation}
zoom={this.state.zoom}
aspect={this.state.aspect}
cropShape={this.state.cropShape}
showGrid={this.state.showGrid}
zoomSpeed={this.state.zoomSpeed}
restrictPosition={this.state.restrictPosition}
onCropChange={this.onCropChange}
onRotationChange={this.onRotationChange}
onCropComplete={this.onCropComplete}
onCropAreaChange={this.onCropAreaChange}
onZoomChange={this.onZoomChange}
onInteractionStart={this.onInteractionStart}
onInteractionEnd={this.onInteractionEnd}
initialCroppedAreaPixels={
Boolean(urlArgs.setInitialCrop) // used to set the initial crop in e2e test
? { width: 699, height: 524, x: 875, y: 157 }
: this.state.initialCroppedAreaPixels
}
initialCroppedAreaPercentages={this.state.initialCroppedAreaPercentages}
transform={[
`translate(${this.state.crop.x}px, ${this.state.crop.y}px)`,
`rotateZ(${this.state.rotation}deg)`,
`rotateY(${this.state.flip.horizontal ? 180 : 0}deg)`,
`rotateX(${this.state.flip.vertical ? 180 : 0}deg)`,
`scale(${this.state.zoom})`,
].join(' ')}
/>
</div>
</div>
)
}
}
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)