Code Monkey home page Code Monkey logo

imagejs's Introduction

ImageJS

Simple (and library agnostic) library for loading images in Javascript. Preloading images is as simple as:

ImageJS.load('http://i.imgur.com/5AEdh4K.png');

Callbacks

Several callbacks are available as the image is loading:

ImageJS.load({
    start: function(url, width, height) {},
    progress: function(url, width, height) {},
    dimensions: function(url, width, height) {},
    done: function(url, width, height) {}
});
  • start: Called when the image starts loading.
  • progress: Called repeatedly as the image loads. Percentage of progress is not available in Javascript.
  • dimensions: Called as soon as the image dimensions are available (if the image is cached, this is called immediately).
  • done: Called when the image is fully loaded.

Examples

Load an image, and display it when it finishes loading. Assumes the target element <img id="image" /> is in the HTML:

ImageJS.load({
    url: 'http://i.imgur.com/5AEdh4K.png',
    done: function(url, width, height) {
        document.getElementById('image').src = url;
    }
});

Full example with spinner placeholder.

ImageJS.load({
    url: 'http://i.imgur.com/5AEdh4K.png',
    start: function() {
        $('#image')
            .css('background-image', 'url("http://www.ajaxload.info/cache/FF/FF/FF/00/00/00/5-0.gif")')
            .css('background-position', 'center')
            .css('background-repeat', 'no-repeat');
    },
    dimensions: function(url, width, height) {
        document.getElementById('image').width = width;
        document.getElementById('image').height = height;
    },
    done: function(url) {
        $('#image')
            .css('background-image', 'none');

        document.getElementById('image').src = url;
    }
});

imagejs's People

Contributors

nathancahill avatar

Stargazers

 avatar Taylor Daughtry avatar Jon Maim avatar Eric Skogen avatar Hidayet Doğan avatar Rafael Oliveira avatar Jason Kulatunga avatar Locke Bircher avatar Varun Malhotra avatar Cyrus David avatar Jaisen Mathai avatar George Psarakis avatar Adam Winick avatar Michal Wozniak avatar Eric Haughee avatar Mark Andrews avatar James Hernandez avatar

Watchers

James Cloos avatar  avatar  avatar

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.