Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to detect onload event of image which was loaded in custom object
    primarykey
    data
    text
    <p>I am creating a sample program to display an image on canvas but would like to enclose actual drawing process into a custom object.</p> <p>The following code won't show the image as <b>pictFrame.img.onload</b> can't catch onload event of image file. Chrome console says "TypeError: Cannot set property 'onload' of undefined" with the expression although it can correctly evaluate <b>pictFrame.img.src</b> or <b>pictFrame.img.height</b>.</p> <p>How should I detect image loading which is being initiated when the object is created?</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function pictFrame(context, imgsrc, pos_x, pos_y, size_x, size_y) { this.context = context; this.img = new Image(); this.img.src = imgsrc; this.pos_x = pos_x; this.pos_y = pos_y; this.size_x = size_x; this.size_y = size_y; this.draw = function() { this.context.drawImage(this.img, this.pos_x, this.pos_y, this.size_x, this.size_y); }; } // These variables must live while the page is being displayed. // Therefore, they can't be defined within window.onload function. var canvas; var context; var pictFrame; window.onload = function() { canvas = document.getElementById("canvas"); context = canvas.getContext("2d"); pictFrame = new pictFrame(context, "darwin.jpg", 10, 10, 200, 200); }; pictFrame.img.onload = function() // Can't catch onload event { pictFrame.draw(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;canvas id="canvas" width="500" height="300"&gt;&lt;/canvas&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload