Skip to content

Commit 3a30628

Browse files
committed
Added watch feature
1 parent b7fca14 commit 3a30628

File tree

2 files changed

+101
-31
lines changed

2 files changed

+101
-31
lines changed

fit.js

Lines changed: 100 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ var fit = (function() {
3333
var win = window || self;
3434
var doc = document;
3535
var getStyle = win.getComputedStyle;
36+
var watching = [];
3637
var vendor;
3738

39+
var defaults = {
40+
hAlign: CENTER,
41+
vAlign: CENTER,
42+
watch: false,
43+
cover: false,
44+
apply: true
45+
};
46+
3847
/*
3948
————————————————————————————————————————————————————————————————————————————————
4049
@@ -208,33 +217,27 @@ var fit = (function() {
208217
/*
209218
————————————————————————————————————————————————————————————————————————————————
210219
211-
Fit
220+
Event Handlers
212221
213222
————————————————————————————————————————————————————————————————————————————————
214223
*/
215224

216-
function fit( target, container, options, callback ) {
225+
function onWindowResize() {
217226

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++ )
225228

226-
callback = options;
227-
options = {};
228-
}
229+
watching[ i ]();
230+
}
229231

230-
// Default options
232+
/*
233+
————————————————————————————————————————————————————————————————————————————————
234+
235+
Fit
236+
237+
————————————————————————————————————————————————————————————————————————————————
238+
*/
231239

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 ) {
238241

239242
// Normalise inputs to standard rectangle definitions
240243

@@ -265,21 +268,20 @@ var fit = (function() {
265268
var tx = options.hAlign == CENTER ? 0.5 * ( w - wb ) : options.hAlign == RIGHT ? w - wb : 0.0;
266269
var ty = options.vAlign == CENTER ? 0.5 * ( h - hb ) : options.vAlign == BOTTOM ? h - hb : 0.0;
267270

268-
// Build transform object
271+
// Build / modify transform object
269272

270-
var transform = {
273+
transform = transform || {};
271274

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;
274277

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;
277280

278-
height: rect.height * scale,
279-
width: rect.width * scale,
281+
transform.height = rect.height * scale;
282+
transform.width = rect.width * scale;
280283

281-
scale: scale
282-
};
284+
transform.scale = scale;
283285

284286
// Apply default transform
285287

@@ -303,6 +305,69 @@ var fit = (function() {
303305
return transform;
304306
}
305307

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+
306371
/*
307372
————————————————————————————————————————————————————————————————————————————————
308373
@@ -311,7 +376,12 @@ var fit = (function() {
311376
————————————————————————————————————————————————————————————————————————————————
312377
*/
313378

314-
return extend( fit, {
379+
return extend( main, {
380+
381+
// Properties
382+
383+
watching: watching,
384+
defaults: defaults,
315385

316386
// Methods
317387

fit.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)