JavaScript Image onload not working with img and Blob image onload firing,image.onload not working,image onload not

Here is the problem,

I need to create an image file with a .svg file. I have a function which should draw the svg, and then, i get it from a canvas to save it as an image file.

my draw function is :

function drawInlineSVG(ctx, rawSVG, callback) {
    var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"});
    var domURL = self.URL || self.webkitURL || self;
    var url = domURL.createObjectURL(svg);
    var img = new Image();
    img.src = url;
    console.log("URL : "+url);
    img.onLoad = function () {
        console.log("image onload");
        ctx.drawImage(this, 0, 0);     
        domURL.revokeObjectURL(url);
        callback(this);
    };
}

ctx is the canvas 2d context, rawSVG is the svg contents The console gives an url like this :

blob:http://myWebsite.net/521a72ea-3156-4290-ae16-025a8f8275bc

But the img ONLOAD never fires... so i have no callback and functions stop. I'm not working on local, so the problem does not come from getting local file...

Thanks for help!

Answer:1

Use addEventListener intead of the onLoad (what is typed incorrectly, see at the end) property:

function drawInlineSVG(ctx, rawSVG, callback) {
    var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"});
    var domURL = self.URL || self.webkitURL || self;
    var url = domURL.createObjectURL(svg);
    var img = new Image();
    img.src = url;
    img.addEventListener('load', function () {
        console.log("image onload");
        ctx.drawImage(this, 0, 0);     
        domURL.revokeObjectURL(url);
        callback(this);
    });
}

You could use use Data URL instead of blob, but that is up to you:

function drawInlineSVG(ctx, rawSVG, callback) {
    var img = new Image();
    img.src = 'data:image/svg+xml;utf8,' + rawSVG;
    img.addEventListener('load', function () {
        console.log("image onload");
        ctx.drawImage(this, 0, 0);     
        domURL.revokeObjectURL(url);
        callback(this);
    });
}

http://jsfiddle.net/smkuLrbu/

If it's not working the issue probably is your SVG file. It must contains at least the following property in the <svg> tag:

<svg xmlns="http://www.w3.org/2000/svg" ...

If you want to stick with the property, use the correct version which don't have uppercase letters: img.onload, but you really should use addEventListener.

Answer:2

You need to set the onload before setting the url.

Otherwise the url is probably already loaded by the time you set the callback.

Try this:

function drawInlineSVG(ctx, rawSVG, callback) {
    var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"});
    var domURL = self.URL || self.webkitURL || self;
    var url = domURL.createObjectURL(svg);
    var img = new Image();

    img.onLoad = function () {
        console.log("image onload");
        ctx.drawImage(this, 0, 0);     
        domURL.revokeObjectURL(url);
        callback(this);
    };

    img.src = url;
    console.log("URL : "+url);
}
Answer:3

I have an iframe that adjusts it's size dynamical. I solved this with a post message and a listener on the parent page, so everytime the content of the iframe changes the iframe size will too and ...

I have an iframe that adjusts it's size dynamical. I solved this with a post message and a listener on the parent page, so everytime the content of the iframe changes the iframe size will too and ...

  1. trigger function when div is visible
  2. trigger function when option is selected
  3. trigger function when checkbox is checked
  4. trigger function when div is loaded
  5. trigger function on page load
  6. trigger function on scroll javascript
  7. trigger function on checkbox click
  8. trigger function on click
  9. trigger function on oscilloscope
  10. trigger function on click jquery
  11. trigger function on enter key
  12. trigger function on window resize
  13. trigger function on select change
  14. trigger function on scroll jquery
  15. trigger function on state change react
  16. trigger function on click javascript
  17. trigger function on page load jquery
  18. trigger function on modal close
  19. trigger function on button click
  20. trigger function on load

is there a way to create a breeze predicate for a property that its type is Edm.Decimal? because the datatype in next expression is always double and I don't find a way to say to breeze that I just ...

is there a way to create a breeze predicate for a property that its type is Edm.Decimal? because the datatype in next expression is always double and I don't find a way to say to breeze that I just ...

I've tried to follow default Web API tutorial: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api Here's what I did: 1) I added Action Routing in my ...

I've tried to follow default Web API tutorial: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api Here's what I did: 1) I added Action Routing in my ...

guys! I want to ask you how can a make a function, that checks if the brackets in a string are put correctly. For example "(a + b).4,2 - )c + 5)" and I have to check the brackets. I tried something, ...

guys! I want to ask you how can a make a function, that checks if the brackets in a string are put correctly. For example "(a + b).4,2 - )c + 5)" and I have to check the brackets. I tried something, ...