Permalink
Browse files

2.0 with an all new load test

  • Loading branch information...
1 parent 8dc2734 commit 1eacb208552d7156bfa68f811d14ee42e42f517a Nathan Ford committed Mar 13, 2012
Showing with 115 additions and 121 deletions.
  1. +36 −22 README
  2. +5 −20 index.html
  3. +2 −0 js/pngy-min.js
  4. +72 −79 js/pngy.js
View
@@ -1,53 +1,61 @@
-Pngy documentation
+Pngy 2.0 documentation
-Pngy is a bit of jQuery that allows you to load images of different file sizes based on the user’s current connection speed. So, if the connection is slow, pngy will load your small image. If its fast, it can load a hi-res image. It’s kinda like a ping test in Javascript.
+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. On DOM-ready, Pngy will load a test image (1k.gif, or whatever you want to use) a defined amount of times and average the speed at which it loaded. From this, it will determine a connection speed and load the appropriate replacement image. When it has been loaded, it will be displayed on the page.
+In your HTML, just put the path to the smallest image as the src or background-image of any appropriate element. On DOM-ready, Pngy will use an AJAX 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). So, anywhere in your JS or HTML file that you want to apply Pngy, just add this line within a DOM-ready function:
+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*
+*Global Options*
+- much less here than in the previous version -
Option Description Default
-file The path to the test file. - File path '1k.gif'
-
-test_repeat Number of times to load test file. - Number 3
-
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
-
-*Note* The higher test_repeat is, the slower the overall response time of the script.
*Limits*
A limit in Pngy is the top threshold (number) of a defined speed. 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: 180,
- normal: 300,
- slow: 'all'
+ fast: 40,
+ normal: 10,
+ slow: 0
You write limits in Pngy like so:
pngy({
limits: {
- fast: 180,
- normal: 300,
- slow: 'all'
+ fastest: 50,
+ fast: 42,
+ normal: 12,
+ slow: 5,
+ slowest: 0
}
});
-Use 'all' for your slowest speed, so that if the connection is slow no higher-res images will be loaded. 'all' is basically your catch-all for anything that is not “fast” or “normal” in the example above.
+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*
@@ -58,18 +66,24 @@ To make Pngy work with your images, you just need to name them based on the corr
$(function () {
pngy({
- file: '/images/1k.gif',
selector: '.pngy', // for backgrounds
apply_to_all_imgs: true, // for IMG tags
limits: {
- fast: 250,
- normal: 350,
- slow: 'all'
+ 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.
+
*Details*
Pngy was created by Nathan Ford. Feel free to interrupt him any time with questions, concerns, complaints, etc.
View
@@ -69,35 +69,20 @@
<h1>Pngy</h1>
- <h2 id="pingspeed">Ping Speed: <b></b></h2>
+ <h2 id="pingspeed">Load Speed: <b></b></h2>
<p><em>IMG example</em></p>
- <img class="pngy" src="./img/saxmax-sm.jpg" />
+ <img class="pngy" src="./img/saxmax-slow.jpg" />
<p><em>background-image example</em></p>
- <div class="pngy" style="background-image: url(./img/saxmax-sm.jpg); height: 667px;"></div>
+ <div class="pngy" style="background-image: url(./img/saxmax-slow.jpg); height: 667px;"></div>
</div>
<script>
-
- $(function () {
-
- pngy({
- file: './img/1k.gif',
- avg_limit: 3,
- selector: '.pngy',
- display_results: '#pingspeed b',
- limits: {
- fastest: 80,
- fast: 180,
- normal: 300,
- slow: 'all'
- },
- apply_to_all_imgs: true
- });
-
+ pngy({
+ display_results: '#pingspeed b'
});
</script>
View
@@ -0,0 +1,2 @@
+// pngy by Nathan Ford - pngy.artequalswork.com
+eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('G=h(e){9 f={l:m,n:m,r:\'.G\',H:m,I:m,5:{U:V,W:10,J:0},s:\'J\'},p=o X(),u;6(e)K(k L f)p[k]=(e[k]!=Y)?e[k]:f[k];M p=f;6(p.I)p.r+=\', Z\';p.7=$(p.r);6($(p.7).v){9 g=(p.7[0].N==\'O\')?$(p.7[0]).w(\'x\'):$(p.7[0]).y(\'B-C\').8(\'q("\',\'\').8(\'")\',\'\');$.11({q:g+\'?\'+P.12(),13:h(){9 a=o Q();u=a.R()},14:h(d){9 c=o Q(),5=[],i=0,j=0;6(!p.n)p.n=d.v/15;6(!p.l)p.l=(c.R()-u)/16;p.D=P.17((p.n/p.l)*S)/S;$(p.H).18(p.D+\' 19\');K(k L p.5)5.1a([k,p.5[k]]);5.1b(h(a,b){1c a[1]-b[1]});5.1d();T(5[i]){p.E=5[i][0];6(5[i][1]<p.D)1e;i++}p.s=5[5.v-1][0];T(p.7[j]){9 t=p.7[j],F=o 1f("(\\-"+p.s+"\\.([A-1g-z]{3,4})\\"*)$");6(t.N==\'O\')$(t).w(\'x\',$(t).w(\'x\').8(F,\'-\'+p.E+".$2"));M $(t).y(\'B-C\',\'q(\'+$(t).y(\'B-C\').8(\'q("\',\'\').8(\'")\',\'\').8(F,\'-\'+p.E+".$2")+\')\');j++}}})}};',62,79,'|||||limits|if|imgs|replace|var||||||||function||||loadspeed|false|loadsize|new||url|selector|base||inittime|length|attr|src|css|||background|image|mbs|speed|reg|pngy|display_results|apply_to_all_imgs|slow|for|in|else|tagName|IMG|Math|Date|getTime|100|while|fast|40|normal|Object|undefined|img||ajax|random|beforeSend|success|1024|1000|floor|html|Kbs|push|sort|return|reverse|break|RegExp|Za'.split('|'),0,{}))
View
@@ -1,101 +1,94 @@
-// Pngy by Nathan Ford - pngy.artequalswork.com
+// pngy by Nathan Ford - pngy.artequalswork.com
pngy = function (ob) {
var defaults = {
- fast: 180,
- normal: 300,
- slow: 'all'
- },
- p = {
- file : (ob && ob.file != undefined) ? ob.file : '1k.gif',
- test_repeat : (ob && ob.test_repeat != undefined) ? ob.test_repeat : 3,
- selector : (ob && ob.selector != undefined) ? ob.selector : '.pngy',
- display_results: (ob && ob.display_results != undefined) ? ob.display_results : false,
- limits : (ob && ob.limits != undefined) ? ob.limits : defaults,
- i : 0,
- nm : 0
- };
+ loadspeed : false,
+ loadsize : false,
+ selector : '.pngy',
+ display_results : false,
+ apply_to_all_imgs: false,
+ limits : {
+ fast : 40,
+ normal : 10,
+ slow : 0
+ },
+ base: 'slow'
+ }, p = new Object(), inittime;
- if (ob.apply_to_all_imgs) p.selector += ', img';
+ // pull in custom setting or use defaults
+ if (ob) for (k in defaults) p[k] = (ob[k] != undefined) ? ob[k] : defaults[k];
+ else p = defaults;
- p.imgs = $(p.selector);
-
- results( p );
-
-};
-
-results = function ( p ) {
+ // add all IMGs to p.selector if apply_to_all_imgs is true
+ if (p.apply_to_all_imgs) p.selector += ', img';
- if (p.file) {
+ // get all elements that match the selector
+ p.imgs = $(p.selector);
- var sd = new Date();
- var sm = sd.getTime();
+ if ($(p.imgs).length) { // only run the rest if there are elements that match p.selector
+
+ var pngy1 = (p.imgs[0].tagName == 'IMG') ? $(p.imgs[0]).attr('src') : $(p.imgs[0]).css('background-image').replace('url("','').replace('")','');
- $("<img />").attr('src', p.file + '?' + sm).load(function () {
+ $.ajax({
+ url: pngy1 + '?' + Math.random(), // pngy1
+ beforeSend: function () {
+
+ // get time before ajax request
+ var initdate = new Date();
+ inittime = initdate.getTime();
+
+ },
+ success: function (d) {
- if (this.complete && typeof this.naturalWidth != "undefined" && this.naturalWidth != 0){
+ var loaddate = new Date(), limits = [], i = 0, j = 0;
- var time = new Date().getTime() - sm;
- p.nm += time;
+ // get image size in Kb
+ if (!p.loadsize) p.loadsize = d.length / 1024;
- p.i++;
+ // get loadspeed in seconds
+ if (!p.loadspeed) p.loadspeed = (loaddate.getTime() - inittime) / 1000;
- if (p.i < p.test_repeat) results( p );
- else {
+ // get Kbs
+ p.mbs = Math.floor((p.loadsize / p.loadspeed) * 100) / 100;
- if (p.nm != 0) {
-
- p.avg = Math.round(p.nm / p.test_repeat);
-
- for (k in p.limits) {
-
- if (!k.match('_suff')) {
-
- var sp = p.limits[k];
-
- if (sp > p.avg) {
- p.speed = k;
- break;
- }
- else p.speed = k;
+ // output speed if display_results has a selector
+ $(p.display_results).html(p.mbs + ' Kbs');
+
+ // uncomment to get a log of your load speed
+ //console.log('Load speed: ' + p.mbs + ' Kbs');
+
+ // reorder limits to highest to lowest
+ for (k in p.limits) limits.push([k, p.limits[k]]);
+ limits.sort(function(a, b) {return a[1] - b[1]});
+ limits.reverse();
+
+ // find the limit
+ while (limits[i]) {
+ p.speed = limits[i][0];
+ if (limits[i][1] < p.mbs) break;
+ i++;
+ }
+
+ p.base = limits[limits.length-1][0];
+
+ // edit image paths
+ while( p.imgs[j] ) {
+
+ var t = p.imgs[j], reg = new RegExp("(\-" + p.base + "\.([A-Za-z]{3,4})\"*)$");
+
+ if (t.tagName == 'IMG')
+ $(t).attr('src', $(t).attr('src').replace(reg, '-' + p.speed + ".$2"));
+ else
+ $(t).css('background-image', 'url(' + $(t).css('background-image').replace('url("','').replace('")','').replace(reg, '-' + p.speed + ".$2") + ')');
- }
-
- }
-
- // change suffix for speed
- if (p.limits[p.speed] !== 'all') appendimgs( p );
-
- // place speed results in selector
- $(p.display_results).html(p.speed + ' (' + p.avg + ')');
-
- }
+ j++;
}
-
+
}
-
- });
- }
-
-};
-
-appendimgs = function (p) {
-
- var s = p.speed, j = 0, reg = new RegExp("((\-[^\-]+)\.([A-Za-z]{3,4})\"*)$");
-
- while( p.imgs[j] ) {
-
- var t = p.imgs[j];
-
- if (t.tagName == 'IMG')
- $(t).attr('src', $(t).attr('src').replace(reg, '-' + s + ".$3"));
- else
- $(t).css('background-image', 'url(' + $(t).css('background-image').replace('url("','').replace('")','').replace(reg, '-' + s + ".$3") + ')');
-
- j++;
+ });
}

0 comments on commit 1eacb20

Please sign in to comment.