forked from mosch/react-avatar-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.es5.js
377 lines (311 loc) · 12 KB
/
index.es5.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'use strict';
var React = require('react');
var isTouchDevice = 'ontouchstart' in window || navigator.msMaxTouchPoints > 0;
var draggableEvents = {
mobile: {
react: {
down: 'onTouchStart',
drag: 'onTouchMove',
drop: 'onTouchEnd',
move: 'onTouchMove',
up: 'onTouchUp'
},
native: {
down: 'touchstart',
drag: 'touchmove',
drop: 'touchend',
move: 'touchmove',
up: 'touchup'
}
},
desktop: {
react: {
down: 'onMouseDown',
drag: 'onDragOver',
drop: 'onDrop',
move: 'onMouseMove',
up: 'onMouseUp'
},
native: {
down: 'mousedown',
drag: 'dragStart',
drop: 'drop',
move: 'mousemove',
up: 'mouseup'
}
}
};
var deviceEvents = isTouchDevice ? draggableEvents.mobile : draggableEvents.desktop;
var ReactAvatarEditor = React.createClass({
displayName: 'ReactAvatarEditor',
propTypes: {
scale: React.PropTypes.number,
image: React.PropTypes.string,
border: React.PropTypes.number,
width: React.PropTypes.number,
height: React.PropTypes.number,
color: React.PropTypes.arrayOf(React.PropTypes.number),
onImageReady: React.PropTypes.func,
style: React.PropTypes.object,
highQuality: React.PropTypes.bool
},
getDefaultProps: function getDefaultProps() {
return {
scale: 1,
border: 25,
width: 200,
height: 200,
color: [0, 0, 0, 0.5],
onImageReady: function onImageReady() {},
style: {},
highQuality: false
};
},
getInitialState: function getInitialState() {
return {
drag: false,
my: null,
mx: null,
image: {
x: 0,
y: 0
}
};
},
getDimensions: function getDimensions() {
return {
width: this.props.width,
height: this.props.height,
border: this.props.border,
canvas: {
width: this.props.width + this.props.border * 2,
height: this.props.height + this.props.border * 2
}
};
},
getImage: function getImage(type, quality) {
var dom = document.createElement('canvas');
var context = dom.getContext('2d');
var dimensions = this.getDimensions();
dom.width = dimensions.width;
dom.height = dimensions.height;
context.globalCompositeOperation = 'destination-over';
var imageState = this.state.image;
this.paintImage(context, {
resource: imageState.resource,
x: imageState.x - dimensions.border,
y: imageState.y - dimensions.border,
width: imageState.width,
height: imageState.height
}, true);
return dom.toDataURL(type, quality);
},
isDataURL: function isDataURL(str) {
var regex = /^\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i;
return !!str.match(regex);
},
loadImage: function loadImage(imageURL) {
var imageObj = new Image();
imageObj.onload = this.handleImageReady.bind(this, imageObj);
if (!this.isDataURL(imageURL)) imageObj.crossOrigin = 'anonymous';
imageObj.src = imageURL;
},
componentDidMount: function componentDidMount() {
var context = this.getDOMNode().getContext('2d');
if (this.props.image) {
this.loadImage(this.props.image);
}
this.paint(context);
document && document.addEventListener(deviceEvents.native.move, this.handleMouseMove, false);
document && document.addEventListener(deviceEvents.native.up, this.handleMouseUp, false);
if (isTouchDevice) React.initializeTouchEvents(true);
},
componentWillUnmount: function componentWillUnmount() {
document && document.removeEventListener(deviceEvents.native.move, this.handleMouseMove, false);
document && document.removeEventListener(deviceEvents.native.up, this.handleMouseUp, false);
},
componentDidUpdate: function componentDidUpdate() {
var context = this.getDOMNode().getContext('2d');
context.clearRect(0, 0, this.getDimensions().canvas.width, this.getDimensions().canvas.height);
this.paint(context);
this.paintImage(context, this.state.image);
},
handleImageReady: function handleImageReady(image) {
var imageState = this.getInitialSize(image.width, image.height);
imageState.resource = image;
imageState.x = this.props.border;
imageState.y = this.props.border;
this.setState({ drag: false, image: imageState }, this.props.onImageReady);
},
getInitialSize: function getInitialSize(width, height) {
var newHeight, newWidth, dimensions, canvasRatio, imageRatio;
dimensions = this.getDimensions();
canvasRatio = dimensions.height / dimensions.width;
imageRatio = height / width;
if (canvasRatio > imageRatio) {
newHeight = this.getDimensions().height;
newWidth = width * (newHeight / height);
} else {
newWidth = this.getDimensions().width;
newHeight = height * (newWidth / width);
}
return {
height: newHeight,
width: newWidth
};
},
componentWillReceiveProps: function componentWillReceiveProps(newProps) {
if (this.props.image != newProps.image) {
this.loadImage(newProps.image);
}
if (this.props.scale != newProps.scale) {
this.squeeze(newProps);
}
},
paintImage: function paintImage(context, image, saving) {
if (image.resource) {
var position = this.calculatePosition(image);
context.save();
context.globalCompositeOperation = 'destination-over';
if (this.props.highQuality && saving) {
var scale = image.resource.width / position.width;
var dimensions = this.getDimensions();
context.canvas.width = dimensions.width * scale;
context.canvas.height = dimensions.height * scale;
var x = image.x * scale;
var y = image.y * scale;
context.drawImage(image.resource, x, y, image.resource.width, image.resource.height);
} else {
context.drawImage(image.resource, image.x, image.y, position.width, position.height);
}
context.restore();
}
},
calculatePosition: function calculatePosition(image) {
image = image || this.state.image;
var x,
y,
width,
height,
dimensions = this.getDimensions();
width = image.width * this.props.scale;
height = image.height * this.props.scale;
var widthDiff = (width - image.width) / 2;
var heightDiff = (height - image.height) / 2;
x = image.x - widthDiff;
y = image.y - heightDiff;
// top and left border bounding
x = Math.min(x, dimensions.border);
y = Math.min(y, dimensions.border);
// right and bottom
var fromBottom = height + (y - dimensions.border);
y = fromBottom > dimensions.height ? y : y + (dimensions.height - fromBottom);
var fromRight = width + (x - dimensions.border);
x = fromRight > dimensions.width ? x : x + (dimensions.width - fromRight);
return {
x: x,
y: y,
height: height,
width: width
};
},
paint: function paint(context) {
context.save();
context.translate(0, 0);
context.fillStyle = 'rgba(' + this.props.color.slice(0, 4).join(',') + ')';
var dimensions = this.getDimensions();
var borderSize = dimensions.border;
var height = dimensions.canvas.height;
var width = dimensions.canvas.width;
context.fillRect(0, 0, width, borderSize); // top
context.fillRect(0, height - borderSize, width, borderSize); // bottom
context.fillRect(0, borderSize, borderSize, height - borderSize * 2); // left
context.fillRect(width - borderSize, borderSize, borderSize, height - borderSize * 2); // right
context.restore();
},
handleMouseDown: function handleMouseDown() {
this.setState({
drag: true,
mx: null,
my: null
});
},
handleMouseUp: function handleMouseUp() {
if (this.state.drag) {
this.setState({ drag: false });
}
},
handleMouseMove: function handleMouseMove(e) {
if (false == this.state.drag) {
return;
}
var imageState = this.state.image;
var lastX = imageState.x;
var lastY = imageState.y;
var mousePositionX = isTouchDevice ? event.targetTouches[0].pageX : e.clientX;
var mousePositionY = isTouchDevice ? event.targetTouches[0].pageY : e.clientY;
var newState = { mx: mousePositionX, my: mousePositionY, image: imageState };
if (this.state.mx && this.state.my) {
var xDiff = this.state.mx - mousePositionX;
var yDiff = this.state.my - mousePositionY;
imageState.y = this.getBoundedY(lastY - yDiff, this.props.scale);
imageState.x = this.getBoundedX(lastX - xDiff, this.props.scale);
}
this.setState(newState);
},
squeeze: function squeeze(props) {
var imageState = this.state.image;
imageState.y = this.getBoundedY(imageState.y, props.scale);
imageState.x = this.getBoundedX(imageState.x, props.scale);
this.setState({ image: imageState });
},
getBoundedX: function getBoundedX(x, scale) {
var image = this.state.image;
var dimensions = this.getDimensions();
var widthDiff = Math.ceil((image.width * scale - image.width) / 2);
var rightPoint = Math.ceil(-image.width * scale + dimensions.width + dimensions.border);
var leftPoint = dimensions.border;
var result;
if (x - widthDiff >= dimensions.border) result = dimensions.border + widthDiff;
if (x < rightPoint) result = rightPoint;
if (x > leftPoint) result = leftPoint;
return result || x;
},
getBoundedY: function getBoundedY(y, scale) {
var image = this.state.image;
var dimensions = this.getDimensions();
var heightDiff = Math.ceil((image.height * scale - image.height) / 2);
var bottomPoint = Math.ceil(-image.height * scale + dimensions.height + dimensions.border);
var topPoint = dimensions.border;
var result;
if (y - heightDiff >= dimensions.border) result = dimensions.border + heightDiff;
if (y < bottomPoint) result = bottomPoint;
if (y > topPoint) result = topPoint;
return result || y;
},
handleDragOver: function handleDragOver(e) {
e.preventDefault();
},
handleDrop: function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
var reader = new FileReader();
reader.onload = this.handleUploadReady;
reader.readAsDataURL(e.dataTransfer.files[0]);
},
handleUploadReady: function handleUploadReady(e) {
this.loadImage(e.target.result);
},
render: function render() {
var attributes = {
width: this.getDimensions().canvas.width,
height: this.getDimensions().canvas.height,
style: this.props.style
};
attributes[deviceEvents.react.down] = this.handleMouseDown;
attributes[deviceEvents.react.drag] = this.handleDragOver;
attributes[deviceEvents.react.drop] = this.handleDrop;
return React.createElement('canvas', attributes);
}
});
module.exports = ReactAvatarEditor;