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

Simon Sarris simon.sarris at gmail.com
Tue Aug 20 11:52:11 PDT 2013


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


More information about the whatwg mailing list