W3C home > Mailing lists > Public > whatwg@whatwg.org > May 2007

[whatwg] toDataURL() and unsupported formats

From: Ian Hickson <ian@hixie.ch>
Date: Tue, 8 May 2007 21:40:13 +0000 (UTC)
Message-ID: <Pine.LNX.4.62.0705082012290.15437@dhalsim.dreamhost.com>
On Sun, 21 May 2006, Anne van Kesteren wrote:
> > >
> > > http://whatwg.org/specs/web-apps/current-work/#todataurl0 defines 
> > > that for unsupported values UAs must return PNG. How about returning 
> > > null instead for unsupported types? Where there any reasons to go 
> > > with something else?
> > 
> > If you want a data: URL, you primarily want a data: URL. The type is 
> > secondary. Thus getting null is never useful.
> 
> If you want data: URL of a specific type, you primarily want a data: URL 
> of a specific type. (Otherwise you would just do toDataURL() without 
> mentioning the type at all and get a nice PNG back.) Doing string checks 
> to see if that succeeded is more error prone than just doing 
> `if(c.toDataURL(test))` or a try/catch(e) statement.

Well, as I see it the alternatives are:

   // return PNG as fallback
   function savePhotoEdits() {
      var data = canvas.toDataURL('image/jpeg');
      if (data.match(/^data:image\/png[;,]/))
        warnUser(messages.savedAsPNG);
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = updateSavingStatus;
      xhr.open("POST", "save");
      xhr.setRequestHeader("Content-Type", "text/plain");
      xhr.send(data);
   }

   // return nothing as fallback
   function savePhotoEdits() {
      var data = canvas.toDataURL('image/jpeg');
      if (!data) {
        warnUser(messages.savedAsPNG);
        data = canvas.toDataURL();
      }
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = updateSavingStatus;
      xhr.open("POST", "save");
      xhr.setRequestHeader("Content-Type", "text/plain");
      xhr.send(data);
   }

Since the warning might not be necessary anyway (e.g. because the server 
might do the conversion silently), I don't think it's a big deal.

It could become more important in the future if the author wanted to 
convert to HD Photo by preference, with fallback to JPEG before fallback 
to PNG; but should that occur, we can always extend the method to support 
multiple formats.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
Received on Tuesday, 8 May 2007 14:40:13 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:58:55 UTC