canvas-to-blob.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * JavaScript Canvas to Blob
  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. * https://opensource.org/licenses/MIT
  10. *
  11. * Based on stackoverflow user Stoive's code snippet:
  12. * http://stackoverflow.com/q/4998908
  13. */
  14. /* global define, Uint8Array, ArrayBuffer, module */
  15. ;(function(window) {
  16. 'use strict'
  17. var CanvasPrototype =
  18. window.HTMLCanvasElement && window.HTMLCanvasElement.prototype
  19. var hasBlobConstructor =
  20. window.Blob &&
  21. (function() {
  22. try {
  23. return Boolean(new Blob())
  24. } catch (e) {
  25. return false
  26. }
  27. })()
  28. var hasArrayBufferViewSupport =
  29. hasBlobConstructor &&
  30. window.Uint8Array &&
  31. (function() {
  32. try {
  33. return new Blob([new Uint8Array(100)]).size === 100
  34. } catch (e) {
  35. return false
  36. }
  37. })()
  38. var BlobBuilder =
  39. window.BlobBuilder ||
  40. window.WebKitBlobBuilder ||
  41. window.MozBlobBuilder ||
  42. window.MSBlobBuilder
  43. var dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/
  44. var dataURLtoBlob =
  45. (hasBlobConstructor || BlobBuilder) &&
  46. window.atob &&
  47. window.ArrayBuffer &&
  48. window.Uint8Array &&
  49. function(dataURI) {
  50. var matches,
  51. mediaType,
  52. isBase64,
  53. dataString,
  54. byteString,
  55. arrayBuffer,
  56. intArray,
  57. i,
  58. bb
  59. // Parse the dataURI components as per RFC 2397
  60. matches = dataURI.match(dataURIPattern)
  61. if (!matches) {
  62. throw new Error('invalid data URI')
  63. }
  64. // Default to text/plain;charset=US-ASCII
  65. mediaType = matches[2]
  66. ? matches[1]
  67. : 'text/plain' + (matches[3] || ';charset=US-ASCII')
  68. isBase64 = !!matches[4]
  69. dataString = dataURI.slice(matches[0].length)
  70. if (isBase64) {
  71. // Convert base64 to raw binary data held in a string:
  72. byteString = atob(dataString)
  73. } else {
  74. // Convert base64/URLEncoded data component to raw binary:
  75. byteString = decodeURIComponent(dataString)
  76. }
  77. // Write the bytes of the string to an ArrayBuffer:
  78. arrayBuffer = new ArrayBuffer(byteString.length)
  79. intArray = new Uint8Array(arrayBuffer)
  80. for (i = 0; i < byteString.length; i += 1) {
  81. intArray[i] = byteString.charCodeAt(i)
  82. }
  83. // Write the ArrayBuffer (or ArrayBufferView) to a blob:
  84. if (hasBlobConstructor) {
  85. return new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
  86. type: mediaType
  87. })
  88. }
  89. bb = new BlobBuilder()
  90. bb.append(arrayBuffer)
  91. return bb.getBlob(mediaType)
  92. }
  93. if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
  94. if (CanvasPrototype.mozGetAsFile) {
  95. CanvasPrototype.toBlob = function(callback, type, quality) {
  96. var self = this
  97. setTimeout(function() {
  98. if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
  99. callback(dataURLtoBlob(self.toDataURL(type, quality)))
  100. } else {
  101. callback(self.mozGetAsFile('blob', type))
  102. }
  103. })
  104. }
  105. } else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
  106. CanvasPrototype.toBlob = function(callback, type, quality) {
  107. var self = this
  108. setTimeout(function() {
  109. callback(dataURLtoBlob(self.toDataURL(type, quality)))
  110. })
  111. }
  112. }
  113. }
  114. if (typeof define === 'function' && define.amd) {
  115. define(function() {
  116. return dataURLtoBlob
  117. })
  118. } else if (typeof module === 'object' && module.exports) {
  119. module.exports = dataURLtoBlob
  120. } else {
  121. window.dataURLtoBlob = dataURLtoBlob
  122. }
  123. })(window)