Created
February 21, 2012 08:39
-
-
Save robnyman/1875176 to your computer and use it in GitHub Desktop.
Use localStorage and canvas to same image as a Data URL
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
// localStorage with image | |
var storageFiles = JSON.parse(localStorage.getItem("storageFiles")) || {}, | |
elephant = document.getElementById("elephant"), | |
storageFilesDate = storageFiles.date, | |
date = new Date(), | |
todaysDate = (date.getMonth() + 1).toString() + date.getDate().toString(); | |
// Compare date and create localStorage if it's not existing/too old | |
if (typeof storageFilesDate === "undefined" || storageFilesDate < todaysDate) { | |
// 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); | |
// Save image as a data URL | |
storageFiles.elephant = imgCanvas.toDataURL("image/png"); | |
// Set date for localStorage | |
storageFiles.date = todaysDate; | |
// Save as JSON in localStorage | |
try { | |
localStorage.setItem("storageFiles", JSON.stringify(storageFiles)); | |
} | |
catch (e) { | |
console.log("Storage failed: " + e); | |
} | |
}, false); | |
// Set initial image src | |
elephant.setAttribute("src", "elephant.png"); | |
} | |
else { | |
// Use image from localStorage | |
elephant.setAttribute("src", storageFiles.elephant); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment