Jump to content

Can you get an image out of xhr.response?


rich
 Share

Recommended Posts

So far I've always used a standard image loader method in my games. I.e. I'll create a new Image() and use the src and onload to load it from a list. This works fine and I'm happy with it, but have wondered if you can do the same via xhr.

 

Writing an xhr loader is fine and I've got it working, loading the PNGs, but am stuck in how to get the image data out of the xhr.response in a format that I can then use in a canvas drawImage call (or even putImageData). Is it easily possible? Is there a way to maybe populate an Image() from the xhr loaded data? Is there anything you need to do to the responseType? Note for this I'm only targeting IE9 and above.

 

Cheers,

 

Rich

Link to comment
Share on other sites

The only possible way needs a serverside script that prepares your image before being sent over a XHR request. You need to base64 encode your image (makes it quite a lot bigger) and send it through the XHR request.

 

On the client-side, you can take that base64 encoded response, create a new image object via javascript and set its src property to: 'data:image/png;base64,[YOUR RESPONSE]' while replacing [YOUR RESPONSE] with your base64 data.

 

I am still searching for the reason why you want to do this via XHR instead of using an image object and listening to the onload event...

Link to comment
Share on other sites

not sure to understand what you are trying to do...

this code loads an image using XHR2 and create an image object from it
 

 

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body>    <canvas id="cnv" width="100" height="100"></canvas><script>    var ctx = document.getElementById('cnv').getContext('2d');    var xhr2 = new XMLHttpRequest();    xhr2.open("GET", 'cell2.png');    xhr2.responseType = "blob"    xhr2.send(null);    xhr2.onload = function () {        var img = new Image();        img.src = window.URL.createObjectURL(xhr2.response);        img.onload = function() {            ctx.drawImage(img, 0, 0);        }    }</script></body></html> 

is this what you are looking for ?

Link to comment
Share on other sites

Don't forget to call revokeObjectURL after the image loads to free it up: http://www.html5rocks.com/en/tutorials/file/xhr2/

 

I implemented blob image loading the other week for Flambe, getting progress events for image downloads is pretty nice! One iOS6 thing I ran into, blobs are supported, but not the blob XHR responseType, so I had to load it up as an arraybuffer and create a blob manually from that. That hackery is down in sendRequest() at https://github.com/aduros/flambe/blob/master/src/flambe/platform/html/HtmlAssetPackLoader.hx.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...