Created
March 12, 2014 08:30
-
-
Save gfcarvalho/9502976 to your computer and use it in GitHub Desktop.
Loads an image using XHR2 and creates an image object from it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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", 'myImage.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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment