Re: [whatwg] Canvas in Workers

On Sat, Dec 1, 2012 at 2:44 AM, Ian Hickson <ian@hixie.ch> wrote:

> On Fri, 30 Nov 2012, Gregg Tavares (社ç~T¨) wrote:
> >
> > on ImageBitmap should zero size canvases just work (and create a 0 sized
> > ImageBitmap)?
> >
> > My personal preference is for APIs that just work with zero sizes so I
> > don't have to write lots of special cases for handling zero.
> >
> > For example [1,2,3].slice(0,0) returns []. It doesn't throw.
> > "abc".substring(0,0) returns "" it doesn't throw. fillRect(x, y, 0, 0)
> > doesn't throw. etc...
> >
> > It just makes life a lot easier
>
> The main reason 0-sized canvases have always thrown in drawImage() is that
> I couldn't work out what you would paint, nor why you'd have a zero-sized
> canvas, and throwing seemed like it'd be the best way to help the author
> figure out where the problem was, rather than just ignoring the call and
> having the author scratch their head about why nothing was happening.
>
> If there's cases where you would legitimately end up with zero-sized
> canvases that you'd try to draw from, though, I'm happy to change it.
>

I don't see how zero sized canvases are any different than zero sized
arrays or empty strings. It's not a matter of use case. It's a matter of
not having to write checks everywhere for 0.  If I'm writing some app that
takes a user supplied size (say a photo editing app where the user can
select a rectangle and copy and paste), why do I want to have to check for
zero?

    var x = Math.min(x1, x2);
    var y = Math.min(y1, y2);
    var width = Math.abs(x1 - x2);
    var height = Math.abs(y1 - y2);

    // Do something with rect defined by x,y,width,height

This seems no different from malloc(0) in C or the other cases I've
mentioned (array size 0 and empty string).  Lots of programming becomes
easier when size = 0 works. Maybe I'm animating

   function draw() {
      var scale = Math.sin() * 0.5 + 0.5;
      var width = realWidth * scale;
      var height = realHeight * scale;

      // do something with width, height
   }

Why do I want to have to check for zero and special case it?

You could argue that I'd have to check of negative values but that's still
nicer than checking for 0

   function draw() {
      var scale = Math.sin() * 0.5 + 0.5;
      var width = Math.max(0, realWidth * scale);
      var height = Math.max(0, realHeight * scale);

      // do something with width, height
   }

vs

   function draw() {
      var scale = Math.sin() * 0.5 + 0.5;
      var width = realWidth * scale;
      var height = realHeight * scale;

      if (width <= 0 || height <= 0) {
         // skip this step
      } else {
        // do something with width, height
      }
   }

I'm just making the case it seems like 0 should always work. That includes
ImageBitmap, Canvas and ImageData



>
> --
> Ian Hickson               U+1047E                )\._.,--....,'``.    fL
> http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
> Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
>

Received on Monday, 3 December 2012 08:44:22 UTC