Giter Club home page Giter Club logo

fitie's Introduction

NPM Version Build Status

fitie is an object-fit polyfill for Internet Explorer browsers.

The object-fit property defines how an element responds to the height and width of its content box. It’s intended for images, videos and other embeddable media formats.

To start using object-fit in Internet Explorer 8 - 11, add an object-fit property to your CSS file.

img.cover, video.cover {
	object-fit: cover;
}

Then, somewhere in the document, include a link to fitie.

<script src="fitie.js"></script>

That’s it. Enjoy!

fitie's People

Contributors

gradosevic avatar jnz31 avatar jonathantneal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fitie's Issues

New polyfill

So we have a first polyfill in this repo, and a different approach at:
http://codepen.io/jonneal/pen/EKPONK
which I expanded upon at:
http://codepen.io/fvsch/pen/MyyBXW

So whether we make a fitie 2.0 or a new repo with a different name, it would be good to have a broad idea of what we want to do.

Name

"fitie" might be problematic with Edge and older WebKits needing some polyfill too.
If we don't keep this name, I guess we need a new repo.

Feature set

Rationale for some of the changes I did in my codepen:

  • Add (limited) object-position support: because it's nice. :) But it could be left to users, with instructions to set both object-position and background-position.
  • Move background-size and background-position out of the JS and to the CSS: I think it's nice to have the JS only add the background-image, and let users control it in CSS. Likewise, it's probably better to avoid resetting background-color.
  • Expose a global function: allows users to reapply the polyfill to new content (has to be done manually); there's some code to make sure we don't work on the same image twice.
  • Add limited responsive image support: using currentSrc then removing srcset.

Do we want to keep all of that? Only specific bits?

Export module

Export a module so that we can import it in ES6 Syntax.

E.g import fitie from 'fitie'

Fitie isn't responsive (unless you implement something like this)

I found out that fitie wasn't responsive, and since I needed it to be I implemented a first draft of it. Not optimized, consider it a work in progress and an idea on how to implement a more solid solution.

var fitie = function (node) {
    // restrict to valid object-fit value
    var objectFit = node.currentStyle['object-fit'];

    if (!objectFit || !/^(contain|cover|fill)$/.test(objectFit)) return;

    var setCSS = node.runtimeStyle;
    var getCSS = node.currentStyle;
    node.defaultStyle = node.defaultStyle || [];

    if (node.defaultStyle === []) {
        for (var key in getCSS) {
            node.defaultStyle[key] = getCSS[key];
        }
    }

    if (node.parentNode.nodeName.toLowerCase() === 'object-fit') {
        node.parentNode.parentNode.replaceChild(node, node.parentNode);
        for (var key in node.defaultStyle) {
            setCSS[key] = node.defaultStyle[key];
        }
    }

    // prepare container styles
    var outerWidth = node.clientWidth;
    var outerHeight = node.clientHeight;
    var outerRatio = outerWidth / outerHeight;

    var name = node.nodeName.toLowerCase();

    var addEventListener = node.addEventListener || node.attachEvent;
    var removeEventListener = node.removeEventListener || node.detachEvent;
    var on = node.addEventListener ? '' : 'on';
    var img = name === 'img';
    var type = img ? 'load' : 'loadedmetadata';

    addEventListener.call(node, on + type, onload);

    if (node.complete) onload();

    function onload() {
        removeEventListener.call(node, on + type, onload);

        // prepare container styles
        var imgCSS = {
            boxSizing: 'content-box',
            display: 'inline-block',
            overflow: 'hidden'
        };

        'backgroundColor backgroundImage borderColor borderStyle borderWidth bottom fontSize lineHeight height left opacity margin position right top visibility width'.replace(/\w+/g, function (key) {
            imgCSS[key] = getCSS[key];
        });

        // prepare image styles
        setCSS.border = setCSS.margin = setCSS.padding = 0;
        setCSS.display = 'block';
        setCSS.height = setCSS.width = 'auto';
        setCSS.opacity = 1;

        var innerWidth = node.videoWidth || node.width;
        var innerHeight = node.videoHeight || node.height;
        var innerRatio = innerWidth / innerHeight;

        // style container
        var imgx = document.createElement('object-fit');

        if (node.parentNode.nodeName.toLowerCase() !== 'object-fit') {
            imgx.appendChild(node.parentNode.replaceChild(imgx, node));

            for (var key in imgCSS) {
                node.defaultStyle[key] = imgCSS[key];
            }
        } else {
            var oldImgx = node.parentNode;
            var parent = oldImgx.parentNode;

            imgx = document.createElement('object-fit');
            imgx.appendChild(node);
            parent.replaceChild(imgx, oldImgx);
        }

        for (var key in node.defaultStyle) imgx.runtimeStyle[key] = node.defaultStyle[key];

        // style image
        var newSize;

        if (objectFit === 'fill') {
            if (img) {
                setCSS.width = outerWidth;
                setCSS.height = outerHeight;
            } else {
                setCSS['-ms-transform-origin'] = '0% 0%';
                setCSS['-ms-transform'] = 'scale(' + outerWidth / innerWidth + ',' + outerHeight / innerHeight + ')';
            }
        } else if (innerRatio < outerRatio ? objectFit === 'contain' : objectFit === 'cover') {
            newSize = outerHeight * innerRatio;

            setCSS.width = Math.round(newSize) + 'px';
            setCSS.height = outerHeight + 'px';
            setCSS.marginLeft = Math.round((outerWidth - newSize) / 2) + 'px';
        } else {
            newSize = outerWidth / innerRatio;

            setCSS.width = outerWidth + 'px';
            setCSS.height = Math.round(newSize) + 'px';
            setCSS.marginTop = Math.round((outerHeight - newSize) / 2) + 'px';
        }
    }
};
fitie.init = function () {
    if (document.body) {
        var all = document.querySelectorAll('img,video');
        var index = -1;

        while (all[++index]) fitie(all[index]);

        window.addEventListener("resize", fitie.init); //TODO: optimize to run time consuming code only if width is actually changed...
    } else {
        setTimeout(fitie.init);
    }
};

if (/MSIE|Trident/.test(navigator.userAgent)) fitie.init();

NPM package isn't updated to latest code

We use npm and webpack and wants to use fitie that way.
However the fitie.init-null-pointer-issue with the wrong load order prevents us to use it.

In your latest code you concatenated the two files to one, in correct order, so that would work nice.
But if you would update the npm-package with the latest code, we could start using it again.

With the addition that we need the issue #10 to be implemented and provided along with current code in the npm-package.

dist/fitie.js throws an error

Hi Jonathan,

Nice work. Thanks for sharing. But I'm seeing:

Uncaught TypeError: Cannot set property 'init' of undefined

when I include the file from dist.

In the bundled demos you're including init() after the function. In the gulpfile you're concatenating init first, then the function. Kind of a small point but I'm sure you want this to work out of the box. Why not just one file, wrapped in an IIFE?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.