npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

picloader

v0.0.4

Published

Simple image loading queue manager

Downloads

7

Readme

picLoader

ImageQueueLoader is a little module for creating a queue of images to pre-load, and be able to switch order of images fast. It is not dependant on any library and should work in all browsers,...in theory. Tests results welcome, if you want to help (see "testing", below).

Usage

Here is an example of usage (it uses jQuery for convenience):

	<html><head><title>Testing</title></head><body>
	<div id="Images">
		<div data-src="some-image.jpg" class="image"></div>
		<div data-src="some-image2.jpg" class="image"></div>
	</div>
	</body></html>

	var images = [];

	//collecting all the images
	var $images = $('.image').each(function(){
		images.push($(this).data('src'));
	});

	var loader = new PicLoader(images)
		.limit(3) //3 concurrent images will be downloaded
		.on(PicLoader.events.LOADED,function(image){
			if(!image){return false;}
			var src = image.src.replace('http://localhost/','')
			$('data-src="'+src+'"').addClass('loaded').css('background-image','url('+image.src+')');
		})
	;

	//bump images that are in view
	$images.inView(function(){ //(using an hypothetical inView plugin...)
		loader.add($(this).data('src'))
	});

	loader.start();

or, if you want more callbacks:


	var loader = new PicLoader().limit(3);

	//collecting all the images
	var $images = $('.image').each(function(){
		var $el = $(this)
		,	src = $el.data('src')
		;
		loader.add(src,function(image){
			if(image){
				$el.addClass('loaded').css('background-image','url('+src+')')
			}else{
				$el.addClass('error')
			}
		});
	});

	//bump images that are in view
	$images.inView(function(){ //(using an hypothetical inView plugin...)
		loader.add($(this).data('src'))
	});

	loader.start();

Install

PicLoader exposes an UMD interface, so it should work with require(), define(), or whatever. It exposes the global PicLoader when used as a regular javascript file. So use bower, use npm, use browserify, or include the js file...Feel free.

API

You will find more info in the test suite (/test/test.js), but here below are the important points

METHODS

start([fn])

starts the queue. Optionally calls the passed callback.

add(source,[source,...])

prepends one or more items to the queue. "source" can be an array or a string.

	loader.add('source','source',['source','source'])

It can also be used to add a source to load and fire a function when loaded. You can pass multiple couples:

	loader.add('source',fn,'source',fn)
	//or
	loader.add(['source',fn],['source',fn])
	//or a mix
	loader.add(['source',fn,'source',fn],'source',fn,'source',fn)

finally, it can be used to bump an image to top

	loader.add('a.jpg','b.jpg','c.jpg','d.jpg')
	loader.add('c.jpg') //c will now load before everything else

add() can also be fed objects, but make sure they have a "src" property

	loader.add({name:'my image',src:'my_image.jpg'})

queue()

does the same as add(), but added items are queued instead of pre-pended

limit()

Sets the maximum number of concurrent downloads. Defaults to 1.

on(event,func)

(aliased to addEventListener) Run the given function when event is triggered

once(event,func)

runs the function once then removes it

off(event,[func])

Stops listening to the event. If you do not pass the function that was used when on() was called, all listeners for a given event are removed.

EVENTS

  • Loader.events.LOADED = 'loaded' called after each image load. Receives the DOM img element that has loaded
  • Loader.events.LOADING = 'loading' called before loading an image. Receives the source
  • Loader.events.COMPLETE = 'complete' called when all images in queue have been loaded.
  • Loader.events.ERROR = 'error' called when an image fails to load
  • Loader.events.PROMOTED = 'promoted' called when an image is promoted to the top of the queue

TESTING

Run

Tests run in the browser and require Chai and Mocha. Run:

npm install

Then

npm test

which will open a server on port 7357 Open http://localhost:7357 in your browser to run the tests.

LICENSE

MIT

DISCLAIMER

Some random images have been included in the repo for testing. They have not been verified as royalty-free. If there is any problem on copyright, leave an issue and we'll remove the image presto.