W3C home > Mailing lists > Public > whatwg@whatwg.org > March 2015

Re: [whatwg] HTMLCanvasElement toBlob Promise

From: Garrett Smith <dhtmlkitchen@gmail.com>
Date: Tue, 17 Mar 2015 13:02:47 -0700
Message-ID: <CABZUbM31YKk0SRwui8=d+EYzgRqSUgq=ffy8+x2yktDfz1RRZg@mail.gmail.com>
To: "acmesquares ." <acmesquares@gmail.com>
Cc: whatwg@whatwg.org
On 3/14/15, acmesquares . <acmesquares@gmail.com> wrote:
> It would be great if there was a promise-based version of toBlob. Same
> parameters as toDataURL, but return a Promise to a blob.
> I use toBlob heavily with other promise APIs, and this one really stands
> out as in need of modernization.
Hi Adria,

toBlob: https://html.spec.whatwg.org/multipage/scripting.html#dom-canvas-toblob

The disparity between toDataURL  - sync - and toBlob  - async - can be
awkward to handle. How would you write it as a promise? I don't agree
that a promise would make that better; IMO it would just move the
place of the callback. Maybe you can offer more insight on it. You can
use a callback in the enclosing scope for both, as an adapter for
toBlob. For example, (I helped anonymously get this working): -


function getBlobFromCanvas(canvas, data, callback) {
  if (canvas.toBlob) { //canvas.blob() supported. Store blob.
  // (GS) This assignment is still a mistake; should result `undefined`
    var blob = canvas.toBlob(function(blob){
      data.photo = blob;
    }, 'image/jpeg');
  } else { // get Base64 dataurl from canvas, then convert it to Blob
    var dataUrl = canvas.toDataURL('image/jpeg');
    data.photo = util.dataUrlToBlob(dataUrl);
    if(data.photo == null) {
      console.log('Storing DataURL instead.');
      isBlobSupported = false;
      data.photo = canvas.toDataURL('image/jpeg');

A comment on comments:
I generally initial my code review comments so that once all the
issues have been resolved, the resultant code has as few explanatory
comments as needed (deliberate omission of copyright notice is
tantamount to professional fraud). This strategy leads to asking if
there are any comments left, and helps motivate the removal of
comments which leads to authoring code that is self-explanatory and
well-named. It also avoids misleading comments, or situations where
the code comments fall out of sync with what the code is actually
doing, such as in the example above:- "//canvas.blob() supported.
Store blob."

This code comment review practice can be replaced by repo commit
comments (stash/gitlab/github), but comments work great in email or
other mediums such as this (despite contradicting articles

But to get back to your `toBlob` promise idea, would you want it as a
new method like `getBlob(mimeType, quality)`? But then what about
toDataURL? Would you have that as a promise, too, or would you leave
it alone?

canvas.getBlob(mimeType, quality).then(callback, errback);
 ~ vs ~
var dataUrl = canvas.getDataURL(mimeType, quality);
util.dataUrlToBlob(dataUrl, callback, errback);

Received on Tuesday, 17 March 2015 20:03:11 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 17:00:29 UTC