Jump to content

Recommended Posts

Edit: after re-thinking and reading my post: Short question is: how do you add an Event listener on the img loading inside a canvas? $("canvas img") doesn't work for example...

 

 

Hey guys, using jQuery and JavaScript... I have a function that adds a line, when this line is added, another function is run that adds a Canvas and loads an image within it like so:

 

Within addCanvas an image is loaded and I can trigger a function upon the image loading.. Cool. But how do I get the previous function to know when this image is loaded? I tried adding return true; within addCanvas and then in the previous function doing an if(addCanvas()){ } but it seems to get to that line and if the image hasn't loaded, it doesn't wait

 

function addText(){
    // Add text code
    addCanvas();
}

function addCanvas(){
    // add canvas, load image
    var $img = new Image();
    $img.src = 'image.png';
    $img.onload = function(){
           // do stuff
    }
}

 

I wanted the previous function to know when it has loaded so I can continue with all the variables I currently have... If I can't check for it in the previous function then I'll have to pass them all when they're only needed in certain conditions and its a little messy.

It's a canvas but can't really do

$("canvas").load(function(){
  // loaded
});

 

because the canvas is already loaded... its the image inside it I want to check that has loaded

Cracked it.

 

return $img within addCanvas and then

 

$img = addCanvas
$($img).load(function(){
  // do stuff
});

 

Can't use  = onload again though as its already been added as a trigger function so overwrites the first one

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.