MCPcopy
hub / github.com/soulwire/fit.js / fit

Function fit

fit.js:302–373  ·  view source on GitHub ↗
( target, container, options, callback, transform )

Source from the content-addressed store, hash-verified

300 lastSavedAreaHeight;
301
302 function fit( target, container, options, callback, transform ) {
303
304 // Normalise inputs to standard rectangle definitions
305
306 var rect = toRectangle( target );
307 var area = toRectangle( container );
308
309 // Compute position offset and scale
310
311 var wa = rect.width === 0 ? lastSavedRectWidth : rect.width;
312 var ha = rect.height === 0 ? lastSavedAreaHeight : rect.height;
313
314 var wb = area.width === 0 ? lastSavedAreaWidth : area.width;
315 var hb = area.height === 0 ? lastSavedAreaHeight : area.height;
316
317 lastSavedRectWidth = wa;
318 lastSavedRectHeight = ha;
319 lastSavedAreaWidth = wb;
320 lastSavedAreaHeight = hb;
321
322 var sx = wb / wa;
323 var sy = hb / ha;
324
325 var ra = wa / ha;
326 var rb = wb / hb;
327
328 var sH = options.cover ? sy : sx;
329 var sV = options.cover ? sx : sy;
330
331 var scale = ra >= rb ? sH : sV;
332 var w = wa * scale;
333 var h = ha * scale;
334
335 var tx = options.hAlign == CENTER ? 0.5 * ( w - wb ) : options.hAlign == RIGHT ? w - wb : 0.0;
336 var ty = options.vAlign == CENTER ? 0.5 * ( h - hb ) : options.vAlign == BOTTOM ? h - hb : 0.0;
337
338 // Build / modify transform object
339
340 transform = transform || {};
341
342 transform.tx = ( area.x - tx ) - rect.x;
343 transform.ty = ( area.y - ty ) - rect.y;
344
345 transform.x = ( area.x - tx ) - rect.x * scale;
346 transform.y = ( area.y - ty ) - rect.y * scale;
347
348 transform.height = rect.height * scale;
349 transform.width = rect.width * scale;
350
351 transform.scale = scale;
352
353 // Apply default transform
354
355 if ( callback )
356
357 callback( transform, target );
358
359 else if ( options.apply ) {

Callers 1

mainFunction · 0.85

Calls 1

toRectangleFunction · 0.85

Tested by

no test coverage detected