• Stars
    star
    1,888
  • Rank 24,567 (Top 0.5 %)
  • Language
    JavaScript
  • License
    Do What The F*ck ...
  • Created almost 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fit things into other things

Fit.js

fit( theThing, /* into */ theOtherThing );

Fit.js has a very simple purpose and that is to help you fit things into other things. The concept it probably best illustrated by the demo page.

tl;dr Here's a JS Fiddle example.


Here are a few simple examples, that should be quite self explanatory.

Given some markup:

    <div id="foo">
        <div id="bar"></div>
    </div>

You might want to fit bar into foo whilst maintaining its original aspect ratio.

    let foo = document.getElementById('foo')
    let bar = document.getElementById('bar');
    fit( bar, foo );

You can control how it fits, like this:

    fit( bar, foo, { cover: true } );

Or this:

    fit( bar, foo, { hAlign: fit.RIGHT } );

Or any combination of the options below.

    fit( bar, foo, {
        
        // Alignment
        hAlign: fit.CENTER, // or fit.LEFT, fit.RIGHT
        vAlign: fit.CENTER, // or fit.TOP, fit.BOTTOM
        
        // Fit within the area or fill it all (true)
        cover: false,

        // Fit again automatically on window resize
        watch: false,
        
        // Apply computed transformations (true) or just
        // return the transformation definition (false)
        apply: true
    });

You can also pass a callback and use it to transform bar however you wish. The callback is passed a transform object that contains all the information you should need.

Here's an example of fitting text by setting the font size of bar such that it fills foo

    fit( bar, foo, function( transform ) {
        var style = window.getComputedStyle( bar );
        var size = parseFloat( style.fontSize );
        bar.style.fontSize = size * transform.scale + 'px';
    });

You can also simply use one of the built in methods of transforming DOM elements

    // Translates and scales the object (default)
    fit( bar, foo, fit.cssTransform );
    
    // uses left, top, width and height
    fit( bar, foo, fit.cssPosition );
    
    // uses margin-left, margin-top, width and height
    fit( bar, foo, fit.cssMargin );

But fit.js was designed to be used with any kind of rectangular object, not just DOM elements.

    var area = { x: 20, y: 20, width: 400, height: 300 };
    var rect = { x: 0, y: 0, width: 100, height: 120 };
    fit( rect, area );

If you are using the DOM, you can tell fit to run again whenever the window resizes. To do this, simply set the watch option to true.

    // This will trigger a fit each time the window resizes
    var watching = fit( bar, foo, { watch: true } );
    
    // You can stop watching at any time
    watching.off();
    
    // And start watching again
    watching.on();
    
    // And trigger a fit manually too
    watching.trigger();

For some visual examples, check out the demo page.

Why?

Sometimes, the standard CSS properties just can't manipulate elements to fit in the way you want them to, or your fit methods need to be more dynamic, or you're not using the DOM at all and need to perform these computations more abstractly.

Either way, if you've ever needed to work out how an object of any size should fit into another of any size, or find yourself writing the same code to do this over and over again, you'll understand how fit.js might be useful.

Appendix

If you contribute to the script (thanks!) then generate a minified version like so (assuming you have installed uglifyjs):

    uglifyjs fit.js --comments /copy/i -cmo fit.min.js

More Repositories

1

sketch.js

Cross-Platform JavaScript Creative Coding Framework
JavaScript
4,086
star
2

Makisu

CSS 3D Dropdown Concept
CSS
1,532
star
3

Coffee-Physics

A simple, lightweight physics engine written in CoffeeScript
CoffeeScript
1,059
star
4

WTFEngine

An HTML / CSS / JavaScript template for creating WhatTheFuckIsMyMashup.com style web sites
JavaScript
686
star
5

Muscular-Hydrostats

Tentacle simulation using inverse kinematics
JavaScript
491
star
6

FoldScroll

Experimental CSS 3D scroll behavior
JavaScript
419
star
7

WebGL-GPU-Particles

1 million+ particles being moved around on the GPU via WebGL
JavaScript
380
star
8

Recursion-Toy

A tool for exploring and visualising recursion / branching systems, written in JavaScript
JavaScript
207
star
9

Springboard

Springboard helps you setup web projects in seconds and easily manage concatenation, minification and compilation of scripts and styles. The idea behind Springboard is to provide a universal build system for HTML/CSS/JS projects that works with your own project template library (whichever boilerplates you may use) and helps you get going with projects of any size quickly and easily.
JavaScript
195
star
10

FontMetrics

A lightweight JavaScript library for computing accurate font metrics such as x-height, cap height, ascent, descent and tittle for any loaded web font.
JavaScript
175
star
11

Plasmatic-Isosurface

A 2 dimensional plasma simulation running on the GPU, written in GLSL and CoffeeScript and rendered with WebGL.
CoffeeScript
138
star
12

SoulwireAS3Framework

Soulwire AS3 Framework
ActionScript
87
star
13

Worms

Experimenting with Inverse kinematics, Catmull-Rom Splines & Wander Behaviors
JavaScript
43
star
14

Unwrapageddon

The gift that keeps on giving...
JavaScript
32
star
15

organis.ms

An exploration of simulated evolution, movement and behavior
JavaScript
30
star
16

Crystallisation

A JavaScript / CoffeeScript & 2D Canvas experiment, exploring the recursive subdivision of polygons to form crystallisation patterns.
JavaScript
20
star
17

ParticleNodeSequencer

An experimental particle based audio sequencer, created in Flash using Tonfall; the new open source AS3 audio engine produced by Andre Michelle
ActionScript
16
star
18

Coffee-Percolator

Use import directives in CoffeeScript to manage dependancies with this tasty CakeFile
CoffeeScript
15
star
19

alfred-hash-workflow

Generate hashes for strings via Alfred using various algorithms including MD5, SHA1, SHA224, SHA256, SHA384 and SHA512
7
star
20

NeuroSynth

Synthesiser and sequencer based somewhat on the concept of neural networks
JavaScript
7
star
21

TensorField

A small interactive experiment with gravitational forces
JavaScript
6
star
22

KineticCanvas

Kinetic Canvas
JavaScript
3
star
23

superlativ.es

Emancipate your verbal lexicon from monotonous and hackneyed superlatives with this preeminent compendium of atypical utterances.
3
star