W3C home > Mailing lists > Public > whatwg@whatwg.org > August 2013

[whatwg] Canvas - Should setLineDash be a nullable sequence?

From: Simon Sarris <simon.sarris@gmail.com>
Date: Tue, 20 Aug 2013 14:52:11 -0400
Message-ID: <CAA5isUZHQQ+zf2CzNOG6NL96c-RkiqF_z0aFjx82_Lh=Xb0hhg@mail.gmail.com>
To: whatwg@lists.whatwg.org
This is minor, but it did recently break formerly-working functionality in
Google Chrome, so maybe its worth a discussion.

The specification reads:

  void setLineDash(sequence<unrestricted double> segments); // default empty
  sequence<unrestricted double> getLineDash();

This means we *cannot* use:

ctx.setLineDash(null);

In Chrome 28 and previous (for at least 6 months) null was an allowed
value, but in Chrome 30 (at least) it switched to throwing a TypeError.
This is technically more correct, though lots of existing JavaScript code
will now stop working because of their change. This doubtless kills some
current JS libraries that were using null, such as mine, and now my
customers must upgrade or get errors when their clients use Chrome.

It's possible that Chrome did not mean to break functionality here, but to
their credit they are now following the spec to the letter. I wrote to them
about that here:

https://code.google.com/p/chromium/issues/detail?id=266948

But the Chrome team seems "shy" about using their bugtracker, so I don't
know if we'll get a response as to whether or not it was an intentional
change.

In any case, I think it would be better if setLineDash was defined in the
spec as a nullable sequence, so that when setting it back to nothing to
return to normal not-dashed path stroking (which could happen thousands of
times in a an animation frame) less stuff gets allocated in the draw loop.
JS authors can use a static empty array to avoid extra memory allocation,
but a brief look at the webkit code suggests (and I may be totally
incorrect here) that the array gets copied anyway, and that's no fun:

https://github.com/WebKit/webkit/blob/master/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp#L515

I think part of the issue is that setLineDash does two things. It sets the
dashing properties, but it also acts as the only way to enable/disable
dashing, I think that null seems appropriate.

For the record, AFAIK no other browser has implemented setLineDash yet,
though ctx.mozDash defaults to null and does accept null.

Simon Sarris
Received on Tuesday, 20 August 2013 18:52:39 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:09:23 UTC