css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <section>
   <p>Avec la console de Chrome (F12),vous pouvez simuler un réseau lent dans l'onglet network.</p> 
  <h1>Détection de Bande passante</h1>

  <p id="progress">Le JavaScript est en cours d'exécution (s'il n'ai pas désactivé).</p>
  <div>
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
  <div>
    <div class="canvas">
      <img class="content" src="https://67.media.tumblr.com/9f8045660737b66fdf90471c5a885829/tumblr_o238cgrXC61r3fubeo1_250.jpg" data-src="http://img15.deviantart.net/9bd1/i/2016/019/3/0/life_is_strange__together__by_sakimichan-d9okm1i.jpg" alt="" />
    </div>
  </div>
</section>
            
          
!
            
              /** Le CSS ici n'est pas interessant pour la compréhension de cet exemple. **/

/** La technique ici est simple, faire charger des fichiers de plus en plus gros quand le DOM est complètement chargé pour estimé plus ou moins précisément la bande passante.

Un bande passante précise ne peut se faire qu'avec un fichier gros, plus le réseau est rapide, plus il faut un fichier gros. Cependant :

Dans notre cas nous n'avons pas besoin de connaître la rapidité du réseau, juste de savoir si le réseau est « au moins assez rapide » pour l'autoload.

Les paramètres de cette démo sont : tant que le chargement du fichier s'effectue en moins de 1 sec et que l'on a pas atteind les 500ko/s, on essai avec un fichier plus volumineux pour tenter une meilleurs précision. Si on dépasse les 500 à force de précisions, on lance l'autoload. Si on dépasse la seconde, on estime que le réseau n'est pas assez rapide.
**/





/** -- N'est pas interessant pour la partie Lazy Load -- **/
.canvas {
	position: relative;
	width: 100%;
  overflow: hidden;
}
.canvas:before {
	content: "";
	display: block;
	padding-top: 100%;
}
.content {
	position: absolute;
	top: 0;
	height: 100%;
}

section {
  margin: 0 auto;
  max-width: 1070px;
  padding-bottom: 100px;
}
section:after {
content: "";
  display: block;
  clear: left;
}
div:not(.canvas) {
  width: calc(25% - 8px);
  float: left;
  padding: 2px;
  border: 1px solid #000;
  margin: 4px;
  box-sizing: border-box;
}
img[data-src-click] {
  cursor: pointer;
}
            
          
!
            
              function measureDownloadTime(image, size, callback) {
    var startTime = (new Date()).getTime(), 
        cacheBuster = "?nnn=" + startTime,
        download = new Image();
    
    download.addEventListener("load", function (event) {
        var endTime = (new Date()).getTime(),
            duration = (endTime - startTime) / 1000,
            bitsLoaded = size * 8,
            speedBps = (bitsLoaded / duration).toFixed(1),
            speedKbps = (speedBps / 1024).toFixed(1),
            speedMbps = (speedKbps / 1024).toFixed(1);

        if (callback.success) {
            callback.success(duration, speedMbps, speedKbps, speedBps);   
        }
    });
    
    download.addEventListener("error", function (err, msg) {
        if (callback.error) {
            callback.error(err, msg);   
        }
    });

    download.src = image + cacheBuster;
}

function asyncLoop(iterations, func, callback) {
    var index = 0,
        done = false,
        loop = {};
    
    loop.next = function() {
        if (done) {
            return;
        }

        if (index < iterations) {
            index++;
            func(loop);
        } else {
            done = true;
            if (callback) {
                callback();
            }
        }
    };

    loop.iteration = function() {
        return index - 1;
    };

    loop.break = function() {
        done = true;
        if (callback) {
            callback();
        }
    };
    
    loop.next();

    return loop;
}

function connectionSpeedTest(testSet, minDuration, minSpeed, callback) {
    asyncLoop(testSet.length, function(loop) {
        var current = loop.iteration();
        measureDownloadTime(testSet[current].url, testSet[current].size, {
            success: function(duration, speedMbps, speedKbps) {
                if (duration < minDuration && speedKbps < minSpeed) {
                    callback.moreAccurate(testSet[current], duration, speedKbps);
                    loop.next();
                } else if (speedKbps >= minSpeed) {
                    callback.success(testSet[current], duration, speedKbps);
                } else {
                    callback.failure(testSet[current], duration, speedKbps);
                }
            }
        });
    });
}

var progress = document.getElementById("progress"),
    testSet = [{
            url: "http://blog.haeresis.fr/upload/example/test-1.jpg",
            size: 19265
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-2.jpg",
            size: 39770
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-3.jpg",
            size: 101141
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-4.jpg",
            size: 282338
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-5.jpg",
            size: 857097
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-6.jpg",
            size: 2832209
        }, {
            url: "http://blog.haeresis.fr/upload/example/test-7.jpg",
            size: 6191055
        }];

window.addEventListener('DOMContentLoaded', function () {
    progress.innerHTML = "Vérification de la bande passante...<br>";
    window.setTimeout(function () {
        connectionSpeedTest(testSet, 1, 500, {
            moreAccurate: function (lastItem, duration, speedKbps) {
                progress.innerHTML = progress.innerHTML + 
                    "<br>" + "Test avec " + 
                    (lastItem.size / 1024).toFixed(1) + "Ko : au moins " + 
                    speedKbps + " Ko/s (" + 
                    duration + "s), moins d'une seconde, affinage avec plus gros...";
            },
            success: function (lastItem, duration, speedKbps) {
                progress.innerHTML = progress.innerHTML + "<br>" + "Test avec " + 
                    (lastItem.size / 1024).toFixed(1) + "Ko : au moins " + 
                    speedKbps + " Ko/s (" + 
                    duration + "s), dépasse les 500Ko/s, Autoload Lancé !";
                
                [].forEach.call(document.getElementsByClassName("content"), function(image) {
                    image.src = image.getAttribute("data-src");
                });
                
                progress.innerHTML = progress.innerHTML + "<br><br>" + "Test Précis en cours...";

                measureDownloadTime(
                    "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg",           
                    4995374, {
                    success: function(duration, speedMbps) {
                        progress.innerHTML = progress.innerHTML + "<br>..." + 
                            speedMbps + "Mo/s (" + duration + "s)";
                    }
                }); 
            },
            failure: function (lastItem, duration, speedKbps) {
                progress.innerHTML = progress.innerHTML + "<br>" + "Test avec " + 
                    (lastItem.size / 1024).toFixed(2) + "Ko : au moins " + 
                    speedKbps + " Ko/s (" + 
                    duration + "s), dépasse la seconde avec moins de 500Ko/s, débit trop faible...";
            }
        });        
    }, 1);
}, false);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console