test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * JavaScript Canvas to Blob Test
  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. /* global describe, it, Blob */
  12. ;(function (expect) {
  13. 'use strict'
  14. // 80x60px GIF image (color black, base64 data):
  15. var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
  16. 'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
  17. 'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7'
  18. var imageUrl = 'data:image/gif;base64,' + b64Data
  19. var blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl)
  20. describe('canvas.toBlob', function () {
  21. it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
  22. window.loadImage(blob, function (canvas) {
  23. canvas.toBlob(
  24. function (newBlob) {
  25. expect(newBlob).to.be.a.instanceOf(Blob)
  26. done()
  27. }
  28. )
  29. }, {canvas: true})
  30. })
  31. it('Converts a canvas element to a PNG blob', function (done) {
  32. window.loadImage(blob, function (canvas) {
  33. canvas.toBlob(
  34. function (newBlob) {
  35. expect(newBlob.type).to.equal('image/png')
  36. done()
  37. },
  38. 'image/png'
  39. )
  40. }, {canvas: true})
  41. })
  42. it('Converts a canvas element to a JPG blob', function (done) {
  43. window.loadImage(blob, function (canvas) {
  44. canvas.toBlob(
  45. function (newBlob) {
  46. expect(newBlob.type).to.equal('image/jpeg')
  47. done()
  48. },
  49. 'image/jpeg'
  50. )
  51. }, {canvas: true})
  52. })
  53. it('Keeps the aspect ratio of the canvas image', function (done) {
  54. window.loadImage(blob, function (canvas) {
  55. canvas.toBlob(
  56. function (newBlob) {
  57. window.loadImage(newBlob, function (img) {
  58. expect(img.width).to.equal(canvas.width)
  59. expect(img.height).to.equal(canvas.height)
  60. done()
  61. })
  62. }
  63. )
  64. }, {canvas: true})
  65. })
  66. it('Keeps the image data of the canvas image', function (done) {
  67. window.loadImage(blob, function (canvas) {
  68. canvas.toBlob(
  69. function (newBlob) {
  70. window.loadImage(newBlob, function (newCanvas) {
  71. var canvasData = canvas.getContext('2d')
  72. .getImageData(0, 0, canvas.width, canvas.height)
  73. var newCanvasData = newCanvas.getContext('2d')
  74. .getImageData(0, 0, newCanvas.width, newCanvas.height)
  75. expect(canvasData.width).to.equal(newCanvasData.width)
  76. expect(canvasData.height).to.equal(newCanvasData.height)
  77. done()
  78. }, {canvas: true})
  79. }
  80. )
  81. }, {canvas: true})
  82. })
  83. })
  84. }(this.chai.expect))