Giter Site home page Giter Site logo

image-progress's Introduction

image-progress

A wrapper for loading image via XHR and dispatching progress events.

How to use

1. Install the plugin.

With npm:

npm i image-progress --save

With Component(1):

component install ayamflow/image-progress

2. Use

var Progress = require('image-progress');

var img = new Progress('test-img.png');

img.on('error', function(event) {
    console.log('there has been an error', event);
});

img.on('progress', function(event) {
    console.log('The image is ' + event.progress * 100 + '% loaded !', event.loaded, event.total, event.progress);
});

img.on('complete', function(event) {
    console.log('The image is loaded.');
});

img.on('start', function(event) {
    console.log('The image with URL ' + event.url + ' has started loading');
});

img.load();

By default, the event.progress only has 2 decimals. You can set the number of decimals by passing the leading property as an instanciation option.

Methods

  • new Progress(url, params)

url: the URL for the image you want to load.

params: the params hash is used if you need to store & retrieve any property on the start & complete events. You can also pass different options there (see the options section below).

  • load()

Starts the loading. It will fire a start event.

  • destroy()

Removes all internal & external listeners, and clears the XHR object.

By default, this method is called after the complete and/or error events are triggered. you can disable this behavior by passing the autoclear: false as an instanciation option.

Instanciation options

  • onStart, onError, onProgress, onComplete (default: null)

Callbacks to be called when the appropriate events are fired.

  • autoload (default: false)

Wether the loading should start automatically on instanciation. If you set it to true, be sure to also pass onProgress/onComplete callbacks as well or you won't be able to listen for completion.

  • leading (default: 2)

The number of decimals in the event.progress property.

  • autoclear (default: true)

Set wether the destroy method should be automatically called after a complete or error event.

  • jsonp (default: false)

Uses jsonp in order to bypass CORS restrictions

Events

  • start: fired when the loading starts. The event contains a reference to the options hash, as well as the url.
  • progress: fired each time the XHR request updates. The event has 3 properties: loaded, total and progress.
  • complete': fired when the loading is complete. The event contains a reference to the options hash, as well as the url.
  • error: fired when a network-related error is raised.

Properties

  • total: the total bytes to load

  • loaded: the loaded bytes loaded

  • progress: the loading progress, between 0 and 1

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.