Code Monkey home page Code Monkey logo

pngy's Introduction

Pngy 2.0 documentation

Pngy is a bit of jQuery that allows you to load images of different file sizes based on a page’s load speed. So, if the load speed is slow, Pngy can load your small image. If it’s fast, it can load a hi-res image. It’s kinda like a ping test in Javascript.

*Note* Pngy does not seem to want to run locally right now. Upload to a server to test the files.

In your HTML, just put the path to the smallest image as the src or background-image of any appropriate element. Pngy will use AJAX to test load the first image or background-image on your page (defined in the 'selector' option), and determine the user’s load speed. 

This speed will not reflect the user’s connection speed, as their connection, the server’s latency, the browser’s compression settings, and the device’s processing speed all are factors in delivering an image. Therefore, in loading the image, we can only look at the aggregate load time from all these factors. This does, though, map directly to what all the images on your page will go through, and the new process only adds one extra HTTP request.

Pngy relies on jQuery for now because I am too lazy to convert it to regular JS (but will do soon). To start pngy, just add this line of JS to the bottom of your HTML:

	pngy();


I have tried to build this to be as customizable as possible. Here are your options:

*Global Options* 
- much less here than in the previous version -

Option			Description					Default

selector		Which elements do you want pngy on? - Selector	'.pngy'

display_results		Element to display speed results. - Selector	false

apply_to_all_imgs	Apply pngy to all IMG tags. - True/False	false

					
*Limits*

A limit in Pngy is the top threshold (number) of a defined speed (in Kbs). So, for “normal”, the limit is 280, which is the speed result above which the speed would no longer be “normal“. You can set whatever thresholds and speed names that you like. Here are the defaults:

	fast: 40, // (kbs)
	normal: 10, // (kbs)
	slow: 0

You write limits in Pngy like so:

	pngy({
		limits: {
			fastest: 50,
			fast: 42,
			normal: 12,
			slow: 5,
			slowest: 0
		}
	});

Use '0' for your slowest speed, so that if the connection is slow no higher-res images will be loaded. '0' is basically your catch-all for anything that is not “fast” or “normal” in the example above.

*Overrides*

You can force Pngy to always load a higher-res image by feeding it loadspeed and loadsize properties. The loadspeed reflects the milliseconds it takes to load an image, and the loadsize is the size of the file (in kb). If you want to force high-res to load in the background, just make an educated guess as to a fast loadspeed for a given file size (the Net panel in Firebug can help with this). An override would look like so:

	pngy({
		loadspeed: 35, // 35 milliseconds...
		loadsize: 9 // ...to load 9 kb
	});


*Image Naming*

To make Pngy work with your images, you just need to name them based on the corresponding limits key, preceded by a '-' (dash). So, your 'fast' image becomes 'whatever-fast.jpg'. I am looking at how to quickly make sure that the image exists on the server before replacing anything. Any suggestions would be welcome.

*Example Implementation*

	$(function () {

		pngy({
			selector: '.pngy', // for backgrounds
			apply_to_all_imgs: true, // for IMG tags
			limits: {
				fast: 30,
				normal: 10,
				slow: 0
			}
		});

	});

*** What’s new in this version ***
* No more need to wrap in a DOM-ready function.
* Pngy will now only use ONE HTTP request to measure speed. No more need for setting 'test_repeat'.
* Pngy uses the first image that is found by the 'selector' option to test loading speed. No need for a test image (previously 1k.gif) or it’s path.
* Combined it all to one function to better contain variables.
* Pngy now calculates Kilobytes per second to measure speed.
* Added a minimized version.

*Details*

Pngy was created by Nathan Ford. Feel free to interrupt him any time with questions, concerns, complaints, etc.

[email protected]
@nathan_ford on Twitter

pngy's People

Watchers

James Cloos 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.