Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

144 wiersze
4.2 KiB

  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. if (CanvasPrototype.msToBlob) {
  107. CanvasPrototype.toBlob = function (callback, type, quality) {
  108. var self = this
  109. setTimeout(function () {
  110. if (
  111. ((type && type !== 'image/png') || quality) &&
  112. CanvasPrototype.toDataURL &&
  113. dataURLtoBlob
  114. ) {
  115. callback(dataURLtoBlob(self.toDataURL(type, quality)))
  116. } else {
  117. callback(self.msToBlob(type))
  118. }
  119. })
  120. }
  121. } else {
  122. CanvasPrototype.toBlob = function (callback, type, quality) {
  123. var self = this
  124. setTimeout(function () {
  125. callback(dataURLtoBlob(self.toDataURL(type, quality)))
  126. })
  127. }
  128. }
  129. }
  130. }
  131. if (typeof define === 'function' && define.amd) {
  132. define(function () {
  133. return dataURLtoBlob
  134. })
  135. } else if (typeof module === 'object' && module.exports) {
  136. module.exports = dataURLtoBlob
  137. } else {
  138. window.dataURLtoBlob = dataURLtoBlob
  139. }
  140. })(window)