forked from tybro0103/jWindowCrop
-
Notifications
You must be signed in to change notification settings - Fork 13
/
jquery.crop.js
152 lines (150 loc) · 5.64 KB
/
jquery.crop.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
/* jQuery-crop v1.0.2, based on jWindowCrop v1.0.0
* Copyright (c) 2012 Tyler Brown
* Modified by Adrien Gibrat
* Licensed under the MIT license.
*/
/* jshint laxcomma: true */
/* globals jQuery */
( function ( $, undefined ) {
var namespace = 'crop' // IE sucks
, Plugin = function ( image, options ) {
var self = this
, img = new Image()
, node = function ( tag, classname ) {
return $( '<' + tag + '/>', { 'class' : namespace + classname.charAt( 0 ).toUpperCase() + classname.slice( 1 ) } );
}
;
this.$image = $( image ).wrap( node( 'div', 'frame' ) ); // wrap image in frame;
this.options = $.extend( {}, this.options, {
width : this.$image.attr( 'width' )
, height : this.$image.attr( 'height' )
, style : this.$image.css( [ 'display', 'left', 'top', 'width', 'height' ] )
}, options );
this.$frame = this.$image.parent();
this.$image
.hide() // hide image until loaded
.addClass( namespace + 'Image' )
.data( namespace, this )
.on( 'mousedown.' + namespace, function ( event ) {
event.preventDefault(); //some browsers do image dragging themselves
$( document )
.on( 'mousemove.' + namespace, {
mouse : {
x : event.pageX
, y : event.pageY
}
, image : {
x : parseInt( self.$image.css( 'left' ), 10 )
, y : parseInt( self.$image.css( 'top' ), 10 )
}
}, $.proxy( self.drag, self ) )
.on( 'mouseup.' + namespace, function () {
$( document ).off( '.' + namespace );
} )
;
} )
.on( 'load.' + namespace, function () {
if ( img.naturalWidth === undefined ) {
img.src = self.$image.attr( 'src' ); // should not need to wait image load event as src is loaded
self.$image.prop( 'naturalWidth', img.width );
self.$image.prop( 'naturalHeight', img.height );
}
var width = self.$image.prop( 'naturalWidth' )
, height = self.$image.prop( 'naturalHeight' );
self.minPercent = Math.max( width ? self.options.width / width : 1, height ? self.options.height / height : 1 );
self.focal = { x : Math.round( width / 2 ), y : Math.round( height / 2 ) };
self.zoom( self.minPercent );
self.$image.fadeIn( 'fast' ); //display image now that it is loaded
} )
.trigger( 'load.' + namespace ) // init even if image is already loaded
;
this.$frame
.css( { width : this.options.width, height : this.options.height } )
.append( node( 'span', 'loading' ).append( this.options.loading ) )
.hover( function () { self.$frame.toggleClass( 'hover' ); } )
;
if ( this.options.controls !== false ) {
this.$frame
.append( node( 'div', 'controls' )
.append( node( 'span', 'text' ).append( this.options.controls ) )
.append( node( 'a', 'zoomIn' ).on( 'click.' + namespace, $.proxy( this.zoomIn, this ) ) )
.append( node( 'a', 'zoomOut' ).on( 'click.' + namespace, $.proxy( this.zoomOut, this ) ) )
)
;
}
}
;
$[ namespace ] = $.extend( Plugin
, {
prototype : {
percent : null
, options : {
width : 320
, height : 180
, zoom : 10
, stretch : 1
, loading : 'Loading...'
, controls : 'Click to drag'
}
, zoom : function ( percent ) {
this.percent = Math.max( this.minPercent, Math.min( this.options.stretch, percent ) );
this.$image.width( Math.ceil( this.$image.prop( 'naturalWidth' ) * this.percent ) );
this.$image.height( Math.ceil( this.$image.prop( 'naturalHeight' ) * this.percent ) );
this.$image.css( {
left : Plugin.fill( - Math.round( this.focal.x * this.percent - this.options.width / 2 ), this.$image.width(), this.options.width )
, top : Plugin.fill( - Math.round( this.focal.y * this.percent - this.options.height / 2 ), this.$image.height(), this.options.height )
} );
this.update();
}
, drag : function ( event ) {
this.$image.css( {
left : Plugin.fill( event.data.image.x + event.pageX - event.data.mouse.x, this.$image.width(), this.options.width )
, top : Plugin.fill( event.data.image.y + event.pageY - event.data.mouse.y, this.$image.height(), this.options.height )
} );
this.update();
}
, zoomIn : function () {
return !! this.zoom( this.percent + 1 / ( this.options.zoom - 1 || 1 ) );
}
, zoomOut : function () {
return !! this.zoom( this.percent - 1 / ( this.options.zoom - 1 || 1 ) );
}
, update : function () {
this.focal = {
x : Math.round( ( this.options.width / 2 - parseInt( this.$image.css( 'left' ), 10 ) ) / this.percent )
, y : Math.round( ( this.options.height / 2 - parseInt( this.$image.css( 'top' ), 10 ) ) / this.percent )
};
this.$image.trigger( $.Event( 'crop', {
cropX : - Math.floor( parseInt( this.$image.css( 'left' ), 10 ) / this.percent )
, cropY : - Math.floor( parseInt( this.$image.css( 'top' ), 10 ) / this.percent )
, cropW : Math.round( this.options.width / this.percent )
, cropH : Math.round( this.options.height / this.percent )
, stretch : this.percent > 1
} ) );
}
, destroy : function () {
this.$image
.removeClass( namespace + 'Image' )
.css( this.options.style )
.off( '.' + namespace )
.removeData( namespace )
.siblings()
.unwrap()
.remove()
;
}
}
// ensure that no gaps are between target's edges and container's edges
, fill : function ( value, target, container ) {
if ( value + target < container ) {
value = container - target;
}
return value > 0 ? 0 : value;
}
} );
$.fn[ namespace ] = function ( options ) {
return this.each( function () {
new Plugin( this, options );
} );
};
} )( jQuery );