[whatwg] A question about the drawimage() canvas function

Kang-Hao (Kenny) Lu kanghaol at oupeng.com
Fri Mar 1 05:49:55 PST 2013

(13/02/06 14:45), 王铁套 wrote:
> Hi, there:
> This is about the drawimage() canvas function in html5.
> What should the following code ouput?
> ///////////////
> <html> <head>
> <title>drawImage with an incorrect type for the image argument
> (part two)</title>
> <style> canvas { display:none } </style>
> <script>
>   window.onload = function(){
>   var r = document.getElementById('r');
>   ctx = document.getElementsByTagName('canvas')[0].getContext('2d');
>   var passed = false;
>   var message = "";
>   try{
>     ctx.drawImage((new Image()), 0, 0, 150, 150);
>     message = "No exception thrown"
>   }catch(e){
>     passed = e.code === e.INDEX_SIZE_ERR;
>     if (!passed) {
>       message = "Got exception code " + e.code +
>                 " expected 1 (INDEX_SIZE_ERR)"}    }
>     r.textContent = passed ? "PASS" : "FAIL";
>     if (message) { r.textContent += " (" + message + ")" }
>   }
> </script> </head> <body>
> <p id="r">FAIL (Script did not run.)</p>
> <canvas></canvas>
> </body></html>
> ///////////////
> In Chrome(24.0.1312.57) and Firefox(18.0.1), the output is:PASS
> while in Opera(12.12) and IE(10.0.9200.16439) it is:FAIL (No
> exception thrown)
> So, what should the result be? and what is the standard for this?

The spec currently says

  # 1. If the image argument is an HTMLImageElement object that is not
  #    fully decodable, or if the image argument is an HTMLVideoElement
  #    object whose readyState attribute is either HAVE_NOTHING or
  #    HAVE_METADATA, then return bad and abort these steps.

An <img> without @src isn't fully decodable by definition and therefore
no exception should be thrown.

However, I worry that WebKit would not be willing to change this to to
reflect the spec as I think this behavior has been existing for a long
time (correct me if I am wrong here). Firefox has a bug for this[1] but
I can't find one in WebKit yet.

Also, the rationale[2] that made this change[3] in 2009 had

  # I expect authors to just draw a video on a canvas when they see
  # fit, test locally or on a stable network, see that it works fine.
  # Then the script will stop working completely for some users because
  # of the uncaught exception when the script tries to drawImage() a
  # video that's not ready yet.
  # Moreover, Opera has lazy loading of images (only loading images
  # that are rendered or have some event handlers or were created with
  # new Image() etc), so we'd probably want to just load the image when
  # the script tries  to draw it instead of throwing.

which seems like a very uncommon situation in contemporary usage pattern
of <canvas> (starting drawImage() calls after all images are fully
loaded) and so I think this rationale shouldn't trump the benefits of
raising exceptions when Web developers make typos in @src or something.
Again, my main worry is that no WebKit people joined that discussion at
that time ....

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=691186
[2] http://lists.w3.org/Archives/Public/public-html/2009Aug/0299
[3] http://html5.org/tools/web-apps-tracker?from=3684&to=3685

Web Specialist, Oupeng Browser, Beijing
Try Oupeng: http://www.oupeng.com/

More information about the whatwg mailing list