forked from akre54/cropsy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcropsy.js
More file actions
132 lines (113 loc) · 3.9 KB
/
cropsy.js
File metadata and controls
132 lines (113 loc) · 3.9 KB
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
/* cropsy.js by Adam Krebs
*
* Available under terms of the MIT license (see LICENSE in project root)
*/
;(function($) {
$.fn.cropsy = function(options) {
var settings = {
max_scale: 2,
mask_padding: 40,
}
if ( typeof options == 'object' ) {
$.extend( settings, options );
}
var $viewport = $('
<div class="cropsy-container">
<div class="cropsy-viewport">
<div class="loading-indicator"></div>
<div class="cropsy-overlay"></div>
</div>
</div>
<div class="cropsy-zoom-slider">
<div class="ui-slider-handle"></div>
</div>
<a href="javascript:void 0;" id="done">Done</a>
');
var $image = $(this);
var $viewport = $('<div />').attr('class', 'cropsy-viewport');
var $container = $('<div />').attr('class', 'cropsy-container');
$container.append($viewport);
$image.wrap($viewport);
$loading = $viewport.prepend('<div class="cropsy-loading-indicator" />'),
$container = $viewport.parent()
$overlay = $('.cropsy-overlay');
$viewport.css({
height: "+=" + settings.mask_padding * 2,
width: "+=" + settings.mask_padding * 2
});
$image.hide();
// wait for image load to attach draggable
$image.load(function() {
centerImage();
// store the original image width and height onto the image's cached
// jQuery object for easy retrieval
$.extend($image, {
originalWidth: $image.width(),
originalHeight: $image.height()
});
var onStartDragPosition;
$overlay.draggable({
start: function(event, ui) {
onStartDragPosition = $image.position();
},
drag: function(event, ui) {
$image.offset({
'top': onStartDragPosition.top + ui.offset.top,
'left': onStartDragPosition.left + ui.offset.left
});
},
stop: function() {
$overlay.css({'left': 0, 'top': 0});
}
});
var $zoom_widget = $viewport.find('.cropsy-zoom-slider')
.width($viewport.width())
.slider({
value: 0,
min: 0,
max: 100,
slide: function(event, ui) {
var imgOffset = $image.offset(),
centerX = Math.round(imgOffset.left + $image.width() / 2),
centerY = Math.round(imgOffset.top + $image.height() / 2),
newHeight = Math.round($image.originalHeight * (1 + ui.value / 100)),
newWidth = Math.round($image.originalWidth * (1 + ui.value / 100));
newHeight = (newHeight % 2) ? newHeight += 1 : newHeight;
newWidth = (newWidth % 2) ? newWidth += 1 : newWidth;
$image.height(newHeight);
$image.width(newWidth);
$image.offset({
top: Math.round(centerY - newHeight / 2),
left: Math.round(centerX - newWidth / 2)
});
}
});
// remove loader and show image
$loading.hide();
$loading.remove();
$image.fadeIn();
});
var centerImage = function() {
var image_width = $image.width(),
image_height = $image.height(),
actual_ratio = image_width / image_height,
mask_width = $viewport.width(),
mask_height = $viewport.height(),
crop_width = $viewport.width() - 2 * settings.mask_padding,
crop_height = $viewport.height() - 2 * settings.mask_padding;
if (image_width > image_height) {
image_height = crop_height;
$image.height(image_height);
image_width = $image.width();
} else {
image_width = crop_width;
$image.width(image_width);
image_height = $image.height();
}
$image.offset({
top: mask_height / 2 - image_height / 2,
left: mask_width / 2 - image_width / 2
});
}
}
})(jQuery)