Skip to content

Instantly share code, notes, and snippets.

@aztack
Created November 29, 2022 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/94992a6d8d9a682b38ebd49ab7f403de to your computer and use it in GitHub Desktop.
Save aztack/94992a6d8d9a682b38ebd49ab7f403de to your computer and use it in GitHub Desktop.
captureSequences
import JSZip from "jszip"; //https://github.com/Stuk/jszip
export function captureSequences(canvas: HTMLCanvasElement, filename: string, onCapture: (frame: number) => string) {
let frame = 0;
var zip = new JSZip();
const capture = () => {
const uri = canvas.toDataURL('png', 1);
var idx = uri.indexOf('base64,') + 'base64,'.length;
var content = uri.substring(idx);
const frameName = onCapture(frame++);
zip.file(frameName, content, { base64: true });
};
capture();
let timer = setInterval(capture, 40);
return (done?: (totalFrame: number) => void) => {
clearInterval(timer);
zip.generateAsync({ type: "blob" }).then(contentBlob => {
done && done(frame);
downloadBlob(contentBlob, filename)
});
};
}
export function downloadBlob(blob: Blob, filename: string) {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment