@@ -33,8 +33,17 @@ var fit = (function() {
33
33
var win = window || self ;
34
34
var doc = document ;
35
35
var getStyle = win . getComputedStyle ;
36
+ var watching = [ ] ;
36
37
var vendor ;
37
38
39
+ var defaults = {
40
+ hAlign : CENTER ,
41
+ vAlign : CENTER ,
42
+ watch : false ,
43
+ cover : false ,
44
+ apply : true
45
+ } ;
46
+
38
47
/*
39
48
————————————————————————————————————————————————————————————————————————————————
40
49
@@ -208,33 +217,27 @@ var fit = (function() {
208
217
/*
209
218
————————————————————————————————————————————————————————————————————————————————
210
219
211
- Fit
220
+ Event Handlers
212
221
213
222
————————————————————————————————————————————————————————————————————————————————
214
223
*/
215
224
216
- function fit ( target , container , options , callback ) {
225
+ function onWindowResize ( ) {
217
226
218
- // Parse arguments
219
-
220
- if ( ! target || ! container )
221
-
222
- throw 'You must supply a target and a container' ;
223
-
224
- if ( typeof options === 'function' ) {
227
+ for ( var i = 0 , n = watching . length ; i < n ; i ++ )
225
228
226
- callback = options ;
227
- options = { } ;
228
- }
229
+ watching [ i ] ( ) ;
230
+ }
229
231
230
- // Default options
232
+ /*
233
+ ————————————————————————————————————————————————————————————————————————————————
234
+
235
+ Fit
236
+
237
+ ————————————————————————————————————————————————————————————————————————————————
238
+ */
231
239
232
- options = extend ( options || { } , {
233
- hAlign : CENTER ,
234
- vAlign : CENTER ,
235
- cover : false ,
236
- apply : true
237
- } ) ;
240
+ function fit ( target , container , options , callback , transform ) {
238
241
239
242
// Normalise inputs to standard rectangle definitions
240
243
@@ -265,21 +268,20 @@ var fit = (function() {
265
268
var tx = options . hAlign == CENTER ? 0.5 * ( w - wb ) : options . hAlign == RIGHT ? w - wb : 0.0 ;
266
269
var ty = options . vAlign == CENTER ? 0.5 * ( h - hb ) : options . vAlign == BOTTOM ? h - hb : 0.0 ;
267
270
268
- // Build transform object
271
+ // Build / modify transform object
269
272
270
- var transform = {
273
+ transform = transform || { } ;
271
274
272
- tx : ( area . x - tx ) - rect . x ,
273
- ty : ( area . y - ty ) - rect . y ,
275
+ transform . tx = ( area . x - tx ) - rect . x ;
276
+ transform . ty = ( area . y - ty ) - rect . y ;
274
277
275
- x : ( area . x - tx ) - rect . x * scale ,
276
- y : ( area . y - ty ) - rect . y * scale ,
278
+ transform . x = ( area . x - tx ) - rect . x * scale ;
279
+ transform . y = ( area . y - ty ) - rect . y * scale ;
277
280
278
- height : rect . height * scale ,
279
- width : rect . width * scale ,
281
+ transform . height = rect . height * scale ;
282
+ transform . width = rect . width * scale ;
280
283
281
- scale : scale
282
- } ;
284
+ transform . scale = scale ;
283
285
284
286
// Apply default transform
285
287
@@ -303,6 +305,69 @@ var fit = (function() {
303
305
return transform ;
304
306
}
305
307
308
+ function main ( target , container , options , callback ) {
309
+
310
+ // Parse arguments
311
+
312
+ if ( ! target || ! container )
313
+
314
+ throw 'You must supply a target and a container' ;
315
+
316
+ if ( typeof options === 'function' ) {
317
+
318
+ callback = options ;
319
+ options = { } ;
320
+ }
321
+
322
+ // Default options
323
+
324
+ options = extend ( options || { } , defaults ) ;
325
+
326
+ // Do it
327
+
328
+ var transform = fit ( target , container , options , callback ) ;
329
+
330
+ // Optionally handle window resizes automatically
331
+
332
+ if ( options . watch ) {
333
+
334
+ if ( ! watching . length )
335
+
336
+ win . addEventListener ( 'resize' , onWindowResize ) ;
337
+
338
+ transform . trigger = function ( ) {
339
+
340
+ fit ( target , container , options , callback , transform ) ;
341
+ }
342
+
343
+ transform . on = function ( suppress ) {
344
+
345
+ var index = watching . indexOf ( transform . trigger ) ;
346
+
347
+ if ( ! ~ index )
348
+
349
+ watching . push ( transform . trigger ) ;
350
+
351
+ if ( ! suppress )
352
+
353
+ transform . trigger ( ) ;
354
+ }
355
+
356
+ transform . off = function ( ) {
357
+
358
+ var index = watching . indexOf ( transform . trigger ) ;
359
+
360
+ if ( ! ! ~ index )
361
+
362
+ watching . splice ( index , 1 ) ;
363
+ } ;
364
+
365
+ transform . on ( true ) ;
366
+ }
367
+
368
+ return transform ;
369
+ }
370
+
306
371
/*
307
372
————————————————————————————————————————————————————————————————————————————————
308
373
@@ -311,7 +376,12 @@ var fit = (function() {
311
376
————————————————————————————————————————————————————————————————————————————————
312
377
*/
313
378
314
- return extend ( fit , {
379
+ return extend ( main , {
380
+
381
+ // Properties
382
+
383
+ watching : watching ,
384
+ defaults : defaults ,
315
385
316
386
// Methods
317
387
0 commit comments