@@ -6,8 +6,7 @@ MEME.MemeCanvasView = Backbone.View.extend({
6
6
7
7
initialize : function ( ) {
8
8
var canvas = document . createElement ( 'canvas' ) ;
9
- var $container = $ ( '#meme-canvas' ) ;
10
- var _this = this ;
9
+ var $container = MEME . $ ( '#meme-canvas' ) ;
11
10
12
11
// Display canvas, if enabled:
13
12
if ( canvas && canvas . getContext ) {
@@ -21,17 +20,6 @@ MEME.MemeCanvasView = Backbone.View.extend({
21
20
22
21
// Listen to model for changes, and re-render in response:
23
22
this . listenTo ( this . model , 'change' , this . render ) ;
24
- // Allow for moving the background image within the canvas
25
- $ ( this . canvas ) . on ( 'mousedown' , function ( e ) {
26
- _this . startDrag ( e ) ;
27
- } ) ;
28
- $ ( document ) . on ( 'mouseup' , function ( e ) {
29
- _this . stopDrag ( e ) ;
30
- } ) ;
31
- $ ( document ) . on ( 'mousemove' , function ( e ) {
32
- _this . moveDrag ( e ) ;
33
- } ) ;
34
-
35
23
} ,
36
24
37
25
setDownload : function ( ) {
@@ -62,14 +50,12 @@ MEME.MemeCanvasView = Backbone.View.extend({
62
50
var bw = m . background . width ;
63
51
64
52
if ( bh && bw ) {
65
- var bp = m . get ( 'backgroundPosition' ) ;
66
-
67
53
// Transformed height and width:
68
54
// Set the base position if null
69
55
var th = bh * d . imageScale ;
70
56
var tw = bw * d . imageScale ;
71
- var cx = bp . x || d . width / 2 ;
72
- var cy = bp . y || d . height / 2 ;
57
+ var cx = d . backgroundPosition . x || d . width / 2 ;
58
+ var cy = d . backgroundPosition . y || d . height / 2 ;
73
59
74
60
ctx . drawImage ( m . background , 0 , 0 , bw , bh , cx - ( tw / 2 ) , cy - ( th / 2 ) , tw , th ) ;
75
61
}
@@ -180,40 +166,47 @@ MEME.MemeCanvasView = Backbone.View.extend({
180
166
'href' : data ,
181
167
'download' : ( d . downloadName || 'share' ) + '.png'
182
168
} ) ;
183
- } ,
184
-
185
- startDrag : function ( e ) {
186
169
187
- if ( e . button != null && e . button != 0 ) {
188
- this . _canMove = false ;
189
- return true ;
190
- }
191
-
192
- this . _canMove = true ;
193
- $ ( 'body' ) . addClass ( 'noselect' ) ;
194
-
195
- this . _startBG = this . model . get ( 'backgroundPosition' ) ;
196
- this . _startPos = { x : e . clientX , y : e . clientY } ;
197
- this . canvas . style . cursor = 'move' ;
170
+ // Enable drag cursor while canvas has artwork:
171
+ this . canvas . style . cursor = this . model . background . width ? 'move' : 'default' ;
198
172
} ,
199
173
200
- stopDrag : function ( e ) {
201
- this . _canMove = false ;
202
- $ ( 'body' ) . removeClass ( 'noselect' ) ;
203
- this . canvas . style . cursor = 'default' ;
174
+ events : {
175
+ 'mousedown canvas' : 'onDrag'
204
176
} ,
205
177
206
- moveDrag : function ( e ) {
207
- if ( typeof ( this . _canMove ) !== "undefined" && this . _canMove ) {
208
- var origPos = this . _startBG ;
209
-
210
- var position = {
211
- x : origPos . x - ( this . _startPos . x - e . clientX ) ,
212
- y : origPos . y - ( this . _startPos . y - e . clientY )
213
- } ;
214
-
215
- this . model . set ( 'backgroundPosition' , position ) ;
216
-
178
+ // Performs drag-and-drop on the background image placement:
179
+ onDrag : function ( evt ) {
180
+ evt . preventDefault ( ) ;
181
+
182
+ // Return early if there is no background image:
183
+ if ( ! this . model . hasBackground ( ) ) return ;
184
+
185
+ // Configure drag settings:
186
+ var model = this . model ;
187
+ var d = model . toJSON ( ) ;
188
+ var iw = model . background . width * d . imageScale / 2 ;
189
+ var ih = model . background . height * d . imageScale / 2 ;
190
+ var origin = { x : evt . clientX , y : evt . clientY } ;
191
+ var start = d . backgroundPosition ;
192
+ start . x = start . x || d . width / 2 ;
193
+ start . y = start . y || d . height / 2 ;
194
+
195
+ // Create update function with draggable constraints:
196
+ function update ( evt ) {
197
+ evt . preventDefault ( ) ;
198
+ model . set ( 'backgroundPosition' , {
199
+ x : Math . max ( d . width - iw , Math . min ( start . x - ( origin . x - evt . clientX ) , iw ) ) ,
200
+ y : Math . max ( d . height - ih , Math . min ( start . y - ( origin . y - evt . clientY ) , ih ) )
201
+ } ) ;
217
202
}
203
+
204
+ // Perform drag sequence:
205
+ var $doc = MEME . $ ( document )
206
+ . on ( 'mousemove.drag' , update )
207
+ . on ( 'mouseup.drag' , function ( evt ) {
208
+ $doc . off ( 'mouseup.drag mousemove.drag' ) ;
209
+ update ( evt ) ;
210
+ } ) ;
218
211
}
219
212
} ) ;
0 commit comments