[whatwg] Canvas API: What should happen if non-finite floats are used

Eric Uhrhane ericu at google.com
Wed Sep 8 10:20:23 PDT 2010

On Wed, Sep 8, 2010 at 9:45 AM, Oliver Hunt <oliver at apple.com> wrote:
> The problem with throwing an exception is that it's fairly common for code to end up accidentally producing a NaN or Infinite value, and throwing an exception would prevent all subsequent drawing from occurring.

I believe that was the point: you throw an exception, the bug becomes
obvious, and you fix it.  Without the exception, you draw the wrong
thing, and it's much harder to find the problem.

> I suggested this behaviour a long time ago after running into yet another piece of code that hit this case in webkit (back when the spec said to throw an exception) yet firefox and opera did not throw.  In some cases firefox does throw, and in others it doesn't (or maybe didn't? has ffx behaviour changed?) and we came to the conclusion that as much as possible the canvas should silently ignore NaN/Infinite values.
> --Oliver
> On Sep 7, 2010, at 10:36 PM, Jonas Sicking wrote:
>> This seems like a strange choice of behavior. Given that this is very
>> likely a bug in the program, wouldn't it make more sense to throw an
>> exception as to make it easier to debug? Similar to for example
>> Node.appendChild when called with a null argument.
>> / Jonas
>> On Tue, Sep 7, 2010 at 10:32 PM, Sam Weinig <weinig at apple.com> wrote:
>>> In the spec does state:
>>> "Except where otherwise specified, for the 2D context interface, any method call with a numeric argument whose value is infinite or a NaN value must be ignored."
>>> -Sam
>>> On Sep 7, 2010, at 9:41 PM, Boris Zbarsky wrote:
>>>> Consider this testcase:
>>>> <!doctype html>
>>>> <html>
>>>>  <body>
>>>>    <canvas id="c" width="200" height="200"></canvas>
>>>>    <script>
>>>>    try {
>>>>      var c = document.getElementById("c"),
>>>>      t = c.getContext("2d");
>>>>      t.moveTo(100, 100);
>>>>      t.lineTo(NaN, NaN);
>>>>      t.lineTo(50, 25);
>>>>      t.stroke();
>>>>    } catch (e) {alert(e); }
>>>>    </script>
>>>>  </body>
>>>> </html>
>>>> Behavior in the spec seems to be undefined (in particular, no mention is made as to what the canvas API functions are supposed to do if non-finite values are passed in).  Behavior in browsers is:
>>>> Presto: Throws NOT_SUPPORTED_ERR on that lineTo(NaN, NaN) call.
>>>> Gecko: Throws DOM_SYNTAX_ERR on that lineTo(NaN, NaN) call.
>>>> Webkit: Silently ignores the lineTo(NaN, NaN) call, and then
>>>>        draws a line from (100,100) to (50, 25).
>>>> Seems like the spec needs to define this.
>>>> -Boris
>>>> P.S.  This isn't a hypothetical issue; this came up in a page that was trying to graph things using canvas and ending up with divide-by-0 all over the place.  It "worked" in webkit (though not drawing the right thing, so much).  It failed to draw anything in Presto or Gecko.

More information about the whatwg mailing list