aboutsummaryrefslogtreecommitdiff
path: root/js/lib/avatar_cropper.js
blob: bfd76225be8b22d1849d3bcfd86be646c42747b1 (plain)
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
/**
 * Avatar cropping
 */

elgg.provide('elgg.avatarCropper');

/**
 * Register the avatar cropper.
 */
elgg.avatarCropper.init = function() {
	$('#user-avatar-cropper').imgAreaSelect({
		selectionOpacity: 0,
		aspectRatio: '1:1',
		onSelectEnd: elgg.avatarCropper.selectChange,
		onSelectChange: elgg.avatarCropper.preview
	});
}

/**
 * Handler for changing select area.
 */
elgg.avatarCropper.preview = function(img, selection) {
	// catch for the first click on the image
	if (selection.width == 0 || selection.height == 0) {
		return;
	}

	var origWidth = $("#user-avatar-cropper").width();
	var origHeight = $("#user-avatar-cropper").height();
	var scaleX = 100 / selection.width;
	var scaleY = 100 / selection.height;
	$('#user-avatar-preview > img').css({
		width: Math.round(scaleX * origWidth) + 'px',
		height: Math.round(scaleY * origHeight) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
	});
}

/**
 * Handler for updating the form inputs after select ends
 */
elgg.avatarCropper.selectChange = function(img, selection) {
	$('input[name=x1]').val(selection.x1);
	$('input[name=x2]').val(selection.x2);
	$('input[name=y1]').val(selection.y1);
	$('input[name=y2]').val(selection.y2);
}

elgg.register_hook_handler('init', 'system', elgg.avatarCropper.init);