Created
February 21, 2012 08:29
-
-
Save robnyman/1875132 to your computer and use it in GitHub Desktop.
Turn image into Data URL and save in localStorage
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
// Get a reference to the image element | |
var elephant = document.getElementById("elephant"); | |
// Take action when the image has loaded | |
elephant.addEventListener("load", function () { | |
var imgCanvas = document.createElement("canvas"), | |
imgContext = imgCanvas.getContext("2d"); | |
// Make sure canvas is as big as the picture | |
imgCanvas.width = elephant.width; | |
imgCanvas.height = elephant.height; | |
// Draw image into canvas element | |
imgContext.drawImage(elephant, 0, 0, elephant.width, elephant.height); | |
// Get canvas contents as a data URL | |
var imgAsDataURL = imgCanvas.toDataURL("image/png"); | |
// Save image into localStorage | |
try { | |
localStorage.setItem("elephant", imgAsDataURL); | |
} | |
catch (e) { | |
console.log("Storage failed: " + e); | |
} | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is a storage where data is saved. Like cookies, but it can be for local files too and has no expiry (unless you remove it).