canvas-toblob.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* canvas-toBlob.js
  2. * A canvas.toBlob() implementation.
  3. * 2011-07-13
  4. *
  5. * By Eli Grey, http://eligrey.com and Devin Samarin, https://github.com/eboyjr
  6. * License: X11/MIT
  7. * See LICENSE.md
  8. */
  9. /*global self */
  10. /*jslint bitwise: true, regexp: true, confusion: true, es5: true, vars: true, white: true,
  11. plusplus: true */
  12. /*! @source http://purl.eligrey.com/github/canvas-toBlob.js/blob/master/canvas-toBlob.js */
  13. (function(view) {
  14. "use strict";
  15. var
  16. Uint8Array = view.Uint8Array
  17. , HTMLCanvasElement = view.HTMLCanvasElement
  18. , is_base64_regex = /\s*;\s*base64\s*(?:;|$)/i
  19. , base64_ranks
  20. , decode_base64 = function(base64) {
  21. var
  22. len = base64.length
  23. , buffer = new Uint8Array(len / 4 * 3 | 0)
  24. , i = 0
  25. , outptr = 0
  26. , last = [0, 0]
  27. , state = 0
  28. , save = 0
  29. , rank
  30. , code
  31. , undef
  32. ;
  33. while (len--) {
  34. code = base64.charCodeAt(i++);
  35. rank = base64_ranks[code-43];
  36. if (rank !== 255 && rank !== undef) {
  37. last[1] = last[0];
  38. last[0] = code;
  39. save = (save << 6) | rank;
  40. state++;
  41. if (state === 4) {
  42. buffer[outptr++] = save >>> 16;
  43. if (last[1] !== 61 /* padding character */) {
  44. buffer[outptr++] = save >>> 8;
  45. }
  46. if (last[0] !== 61 /* padding character */) {
  47. buffer[outptr++] = save;
  48. }
  49. state = 0;
  50. }
  51. }
  52. }
  53. // 2/3 chance there's going to be some null bytes at the end, but that
  54. // doesn't really matter with most image formats.
  55. // If it somehow matters for you, truncate the buffer up outptr.
  56. return buffer.buffer;
  57. }
  58. ;
  59. if (Uint8Array) {
  60. base64_ranks = new Uint8Array([
  61. 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1
  62. , -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  63. , 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
  64. , -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
  65. , 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
  66. ]);
  67. }
  68. if (HTMLCanvasElement && !HTMLCanvasElement.prototype.toBlob) {
  69. HTMLCanvasElement.prototype.toBlob = function(callback, type /*, ...args*/) {
  70. if (!type) {
  71. type = "image/png";
  72. } if (this.mozGetAsFile) {
  73. callback(this.mozGetAsFile("canvas", type));
  74. return;
  75. }
  76. var
  77. args = Array.prototype.slice.call(arguments, 1)
  78. , dataURI = this.toDataURL.apply(this, args)
  79. , header_end = dataURI.indexOf(",")
  80. , data = dataURI.substring(header_end + 1)
  81. , is_base64 = is_base64_regex.test(dataURI.substring(0, header_end))
  82. , blob
  83. ;
  84. if (Blob.fake) {
  85. // no reason to decode a data: URI that's just going to become a data URI again
  86. blob = new Blob
  87. if (is_base64) {
  88. blob.encoding = "base64";
  89. } else {
  90. blob.encoding = "URI";
  91. }
  92. blob.data = data;
  93. blob.size = data.length;
  94. } else if (Uint8Array) {
  95. if (is_base64) {
  96. blob = new Blob([decode_base64(data)], {type: type});
  97. } else {
  98. blob = new Blob([decodeURIComponent(data)], {type: type});
  99. }
  100. }
  101. callback(blob);
  102. };
  103. }
  104. }(self));