Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. function canvas2Blob(canvas) {
  2. var dataURI = canvas.toDataURL();
  3.  
  4. // convert base64 to raw binary data held in a string
  5. // doesn't handle URLEncoded DataURIs
  6. var byteString = atob(dataURI.split(',')[1]);
  7.  
  8. // separate out the mime component
  9. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  10.  
  11. // write the bytes of the string to an ArrayBuffer
  12. var ab = new ArrayBuffer(byteString.length);
  13. var ia = new Uint8Array(ab);
  14. for (var i = 0; i < byteString.length; i++) {
  15. ia[i] = byteString.charCodeAt(i);
  16. }
  17.  
  18. // create a blob for writing to a file
  19. var blob = new Blob([ab], {type: mimeString});
  20. return blob;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement