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

Ian Hickson ian at hixie.ch
Tue Dec 7 16:35:03 PST 2010


On Wed, 8 Sep 2010, 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). [...]

On Tue, 7 Sep 2010, Sam Weinig wrote:
>
> In 4.8.11.1 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."

Right. This was intentional; if I recall correctly it was based on the 
idea that "3D" code rendering to a 2D canvas would often find itself 
dealing with asymptotic behaviour and that it was better to silently 
ignore such edge cases than to bail entirely as used to happen.

There's actually a comment to that effect in the spec source:

  <p>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.</p>

  <!--
   Philip Taylor wrote:
   > My experience with some 3d canvas code is that infinities come up in
   > naturally harmless places, e.g. having a function that scales by x then
   > translates by 1/x and wanting it to work when x=0 (which ought to draw
   > nothing, since anything it draws is zero pixels wide), and it's a bit
   > annoying to track down and fix those issues, so I'd probably like it if
   > they were harmless in canvas methods. Opera appears to silently not draw
   > anything if the transformation matrix is not finite, but Firefox throws
   > exceptions when passing in non-finite arguments.
  -->

I don't personally have a strong opinion on this, and will likely just 
update the spec to match what the majority of implementations do in due 
course, if that changes. Currently it seems to be a bit of a mixed bag.

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


More information about the whatwg mailing list