Last active
June 23, 2017 16:19
-
-
Save seank-com/3306120 to your computer and use it in GitHub Desktop.
Create a composite image of all the visible img tags in the DOM and save it to the app data directory of a Windows 8 Application
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
function SaveSplash() { | |
window.msWriteProfilerMark("start-render"); | |
var body = document.body, | |
appdata = Windows.Storage.ApplicationData.current, | |
localfolder = appdata.localFolder, | |
canvas = document.createElement("canvas"), | |
ctx = canvas.getContext("2d"), | |
imgs = document.body.getElementsByTagName("img"), | |
size = imgs.length, | |
i = 0, box, img, blob; | |
canvas.width = body.clientWidth; | |
canvas.height = body.clientHeight; | |
for (var i = 0; i < size; i++) { | |
img = imgs.item(i); | |
try { | |
box = img.getBoundingClientRect(); | |
} catch (e) { | |
} | |
if (!!box && | |
box.left <= canvas.width && | |
box.top <= canvas.height && | |
box.left + box.width >= 0 && | |
box.top + box.height >= 0) { | |
ctx.drawImage(img, box.left, box.top, box.width, box.height); | |
} | |
} | |
window.msWriteProfilerMark("start-encode"); | |
blob = canvas.msToBlob(); | |
img = blob.msDetachStream(); | |
window.msWriteProfilerMark("start-write"); | |
return localfolder.createFileAsync("splash.png", Windows.Storage.CreationCollisionOption.openIfExists).then(function (file) { | |
return file.openAsync(Windows.Storage.FileAccessMode.readWrite); | |
}).then(function (ras) { | |
var os = ras.getOutputStreamAt(0), | |
is = img.getInputStreamAt(0); | |
return Windows.Storage.Streams.RandomAccessStream.copyAndCloseAsync(is, os); | |
}); | |
} | |
// ... | |
app.oncheckpoint = function (args) { | |
window.msWriteProfilerMark("start-suspend"); | |
args.setPromise(SaveSplash().then(function () { | |
window.msWriteProfilerMark("stop-suspend"); | |
})); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment