canvas-to-blob.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * JavaScript Canvas to Blob 2.0.5
  3. * https://github.com/blueimp/JavaScript-Canvas-to-Blob
  4. *
  5. * Copyright 2012, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * http://www.opensource.org/licenses/MIT
  10. *
  11. * Based on stackoverflow user Stoive's code snippet:
  12. * http://stackoverflow.com/q/4998908
  13. */
  14. /*jslint nomen: true, regexp: true */
  15. /*global window, atob, Blob, ArrayBuffer, Uint8Array, define */
  16. (function (window) {
  17. 'use strict';
  18. var CanvasPrototype = window.HTMLCanvasElement &&
  19. window.HTMLCanvasElement.prototype,
  20. hasBlobConstructor = window.Blob && (function () {
  21. try {
  22. return Boolean(new Blob());
  23. } catch (e) {
  24. return false;
  25. }
  26. }()),
  27. hasArrayBufferViewSupport = hasBlobConstructor && window.Uint8Array &&
  28. (function () {
  29. try {
  30. return new Blob([new Uint8Array(100)]).size === 100;
  31. } catch (e) {
  32. return false;
  33. }
  34. }()),
  35. BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder ||
  36. window.MozBlobBuilder || window.MSBlobBuilder,
  37. dataURLtoBlob = (hasBlobConstructor || BlobBuilder) && window.atob &&
  38. window.ArrayBuffer && window.Uint8Array && function (dataURI) {
  39. var byteString,
  40. arrayBuffer,
  41. intArray,
  42. i,
  43. mimeString,
  44. bb;
  45. if (dataURI.split(',')[0].indexOf('base64') >= 0) {
  46. // Convert base64 to raw binary data held in a string:
  47. byteString = atob(dataURI.split(',')[1]);
  48. } else {
  49. // Convert base64/URLEncoded data component to raw binary data:
  50. byteString = decodeURIComponent(dataURI.split(',')[1]);
  51. }
  52. // Write the bytes of the string to an ArrayBuffer:
  53. arrayBuffer = new ArrayBuffer(byteString.length);
  54. intArray = new Uint8Array(arrayBuffer);
  55. for (i = 0; i < byteString.length; i += 1) {
  56. intArray[i] = byteString.charCodeAt(i);
  57. }
  58. // Separate out the mime component:
  59. mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  60. // Write the ArrayBuffer (or ArrayBufferView) to a blob:
  61. if (hasBlobConstructor) {
  62. return new Blob(
  63. [hasArrayBufferViewSupport ? intArray : arrayBuffer],
  64. {type: mimeString}
  65. );
  66. }
  67. bb = new BlobBuilder();
  68. bb.append(arrayBuffer);
  69. return bb.getBlob(mimeString);
  70. };
  71. if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
  72. if (CanvasPrototype.mozGetAsFile) {
  73. CanvasPrototype.toBlob = function (callback, type, quality) {
  74. if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
  75. callback(dataURLtoBlob(this.toDataURL(type, quality)));
  76. } else {
  77. callback(this.mozGetAsFile('blob', type));
  78. }
  79. };
  80. } else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
  81. CanvasPrototype.toBlob = function (callback, type, quality) {
  82. callback(dataURLtoBlob(this.toDataURL(type, quality)));
  83. };
  84. }
  85. }
  86. if (typeof define === 'function' && define.amd) {
  87. define(function () {
  88. return dataURLtoBlob;
  89. });
  90. } else {
  91. window.dataURLtoBlob = dataURLtoBlob;
  92. }
  93. }(window));