cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <form>
  <fieldset>
    <label>
      <input name="site" type="radio" value="daverupert" checked> daverupert.com</label>
    <label>
      <input name="site" type="radio" value="cnn">cnn.com
    </label>
    <label>
      <input name="site" type="radio" value="lings">lingscars.com (top third)
    </label>
  </fieldset>
</form>

<canvas id="canvas"></canvas>
<div>
<p>
Ratio = <span id="loadpx"></span>:<span id="totalpx"></span>
</p>
<p>
Decimal = <span id="decpx"></span>
</p>
</div>
            
          
!
            
              div {
  position: absolute;
  color: red;
  font-weight: bold;
  top: 2em;
  right: 1em;
  background: white;
  padding: 1em;
}
            
          
!
            
              // From http://jsfiddle.net/rf8Xf/

var canvas = document.getElementById("canvas");
var loadpx = document.getElementById("loadpx");
var totalpx = document.getElementById("totalpx");
var decpx = document.getElementById("decpx");
var ctx = canvas.getContext("2d");
var form = document.querySelector('form');

function getRadioVal(name) {
    var val;
    // get list of radio buttons with specified name
    var radios = form.elements[name];
    
    // loop through list of radio buttons
    for (var i=0, len=radios.length; i<len; i++) {
        if ( radios[i].checked ) { // radio checked?
            val = radios[i].value; // if so, hold its value in val
            break; // and break out of for loop
        }
    }
    return val; // return value of checked radio or undefined if none checked
}

function getChecked() {
  var currentChecked = getRadioVal('site');
  var newSrc;
  switch( currentChecked ) {
      case 'daverupert':
        newSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/17/screenshot.png';
        break;
      case 'cnn':
        newSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/17/cnn.png';
        break;
      case 'lings':
        newSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/17/lings.png';
        break;
      default:
        newSrc = null;
  }
  var img = document.createElement('img');
  img.crossOrigin = 'anonymous';
  img.src = newSrc;
  img.onload = function() {
    console.log(img.src);
    
    canvas.height = img.height;
    canvas.width = img.width;
    // From http://jsfiddle.net/rf8Xf/21/
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

    var data = ctx.getImageData(0, 0, canvas.width, canvas.height),
        buffer = data.data,
        len = buffer.length,
        // threshold = 127,
        threshold = 191,
        lum,
        load = 0;
    
    totalpx.textContent = len;
    
    for(var i=0; i < len; i += 4) {
        lum = buffer[i] * 0.3 + buffer[i+1] * 0.59 + buffer[i+2] * 0.11;
        lum = lum < threshold ? 0 : 255;
        
        if(lum === 0) {
        	load += 1;
        }
        
        buffer[i] = lum;
        buffer[i+1] = lum;
        buffer[i+2] = lum;
    }
    
    loadpx.textContent = load;
    decpx.textContent = Math.round( (load/len) * 1000 ) / 1000;
    
    ctx.putImageData(data, 0, 0);    
    
    delete img;
  }
  
}

document.querySelector('form').addEventListener('change', getChecked);

getChecked();
            
          
!
999px
Loading ..................

Console