Aug 22

Jacob Seidelin of nihilogic fame (remember his Super Mario in JavaScript solution) is one of my unsung heroes of JavaScript. His solutions have that Dean Edwards “genius bordering on the bat-sh*t-crazy” touch that make you shake your head in disbelief when they come out but later on become very interesting.

One of his posts from 2008 entitled “Compression using Canvas and PNG-embedded data” had a good idea: if you want to compress JavaScript and CSS you could reverse engineer a packing algorithm in JavaScript or you could use a lossless packing system that is already in use and supported in browsers. In this case the packed format is PNG and the way to unpack it is by using the canvas API’s getImageData() method:

JAVASCRIPT:

var x = function(z, m, ix ) { // image, callback, chunk index
  var o = new Image();
  o.onload = function() {
    var s = “”,
        c = d.createElement(“canvas”),
        t = c.getContext(“2d”),
        w = o.width,
        h = o.height;
    c.width = c.style.width = w;
    c.height = c.style.height = h;
    t.drawImage(o, 0, 0);
    var b = t.getImageData( 0, 0, w, h ).data; //b : bucket of data
    for(var i= 0; i <b.length; i += 4) {
      if( b[i]> 0 )
        s += String.fromCharCode(b[i]);
    }
    m(s, ix);
  }
  o.src = z;
}

As there are quite some interesting competitions going on that need really small JavaScript solutions Alex Le took up Jacob’s work and wrapped it in a build script that concatenates, packs and converts to a PNG and unpacks it for the 10K competition with a JavaScript. In the process Alex also found some bug in Internet Explorer 9′s canvas implementation as it only reads the first 8192 bytes of a PNG and returns 0 for the others :(.

It is pretty amazing how efficient this way of packing is. What we need to test now is when and if it is worth while to have the unpacking done on the client. Imagine adding your JS and CSS to the end of an image and cropping it in CSS to have all the info you need in an app in a single HTTP request. Let the games begin.

Continue reading »

Tagged with:
May 26

There is a lot of solid support for cross-domain Ajax in modern web browsers, yet most developers are still unaware of this powerful capability. Usage requires just a little bit of extra JavaScript work and a little extra server-side work to ensure that the correct headers are being sent. IE8’s implementation lags a bit behind the others in terms of allowing advanced requests and credentialed requests, but hopefully support for CORS will continue to improve.

Nicholas C. Zakas concludes the above in his post on cross domain with cross origin resource sharing (and XDR for IE).

He shares some simple code:

JAVASCRIPT:

function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest();
    if (“withCredentials” in xhr){
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != “undefined”){
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

var request = createCORSRequest(“get”, “http://www.nczonline.net/”);
if (request){
    request.onload = function(){
        //do something with request.responseText
    };
    request.send();
}
 

to do the work of cross domain.

Read the detail for the various headers and tweaks, and then check out the CORS demo page by Arun of Mozilla.

Continue reading »

Tagged with:
Apr 29

Dean Hachamovitch took an opportunity to talk about Microsoft’s point of view on HTML5 video.

Namely, the view that they will only support H.264.

Why do that post now? Get it out before Google I/O and VP8 gets released and opened? Follow Steve Jobs’ attack on Adobe (which talks about the openness of H.264?).

Dean says:

H.264 is an industry standard

Not an open standard.

Robert O’Callahan of Mozilla comments:

Dean, I think it’s quite dangerous for you to say “developers can rely on the H.264 codec and hardware acceleration support of the underlying operating system, like Windows 7, without paying any additional royalty.”

when the Windows 7 H.264 codec is licensed only for non-commercial use. See

http://download.microsoft.com/Documents/UseTerms/Windows%207_Ultimate_English_c44ca3df-8338-4a2f-a176-39d2e68986c4.pdf

THIS PRODUCT IS LICENSED UNDER THE AVC, THE VC-1, THE MPEG-4 PART 2 VISUAL, AND THE MPEG-2 VIDEO PATENT PORTFOLIO LICENSES FOR THE PERSONAL AND NON-COMMERCIAL USE OF A CONSUMER TO (i) ENCODE VIDEO IN COMPLIANCE WITH THE ABOVE STANDARDS (“VIDEO

STANDARDS”) AND/OR (ii) DECODE AVC, VC-1, MPEG-4 PART 2 AND MPEG-2 VIDEO THAT WAS ENCODED BY A CONSUMER ENGAGED IN A PERSONAL AND NON-COMMERCIAL ACTIVITY OR WAS OBTAINED FROM A VIDEO PROVIDER LICENSED TO PROVIDE SUCH VIDEO. NONE OF THE

LICENSES EXTEND TO ANY OTHER PRODUCT REGARDLESS OF WHETHER SUCH PRODUCT IS INCLUDED WITH THIS PRODUCT IN A SINGLE ARTICLE.

Now that Dean has started the conversation, I hope that it is two way…. and a conversation. Will they truly ONLY support H.264? Regardless of codes installed on the system? Of a potential VP8? Never Ogg?

The IE9 preview has been very promising. The Web is the platform, and Microsoft knows they need to be the game. There is very much room for them at the Open Web table, I just hope we all share the bread in a constructive way.

Continue reading »

Tagged with:
preload preload preload