Blame view

bower_components/blueimp-canvas-to-blob/test/test.js 2.79 KB
f6e211e4   Administrator   finish work part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  /*
   * JavaScript Canvas to Blob Test
   * https://github.com/blueimp/JavaScript-Canvas-to-Blob
   *
   * Copyright 2012, Sebastian Tschan
   * https://blueimp.net
   *
   * Licensed under the MIT license:
   * http://www.opensource.org/licenses/MIT
   */
  
  /*global window, describe, it, Blob */
  
  ;(function (expect) {
    'use strict'
  
    // 80x60px GIF image (color black, base64 data):
    var b64Data = 'R0lGODdhUAA8AIABAAAAAP///ywAAAAAUAA8AAACS4SPqcvtD6' +
        'OctNqLs968+w+G4kiW5omm6sq27gvH8kzX9o3n+s73/g8MCofE' +
        'ovGITCqXzKbzCY1Kp9Sq9YrNarfcrvcLDovH5PKsAAA7'
    var imageUrl = 'data:image/gif;base64,' + b64Data
    var blob = window.dataURLtoBlob && window.dataURLtoBlob(imageUrl)
  
    describe('canvas.toBlob', function () {
      it('Converts a canvas element to a blob and passes it to the callback function', function (done) {
        window.loadImage(blob, function (canvas) {
          canvas.toBlob(
            function (newBlob) {
              expect(newBlob).to.be.a(Blob)
              done()
            }
          )
        }, {canvas: true})
      })
  
      it('Converts a canvas element to a PNG blob', function (done) {
        window.loadImage(blob, function (canvas) {
          canvas.toBlob(
            function (newBlob) {
              expect(newBlob.type).to.be('image/png')
              done()
            },
            'image/png'
          )
        }, {canvas: true})
      })
  
      it('Converts a canvas element to a JPG blob', function (done) {
        window.loadImage(blob, function (canvas) {
          canvas.toBlob(
            function (newBlob) {
              expect(newBlob.type).to.be('image/jpeg')
              done()
            },
            'image/jpeg'
          )
        }, {canvas: true})
      })
  
      it('Keeps the aspect ratio of the canvas image', function (done) {
        window.loadImage(blob, function (canvas) {
          canvas.toBlob(
            function (newBlob) {
              window.loadImage(newBlob, function (img) {
                expect(img.width).to.be(canvas.width)
                expect(img.height).to.be(canvas.height)
                done()
              })
            }
          )
        }, {canvas: true})
      })
  
      it('Keeps the image data of the canvas image', function (done) {
        window.loadImage(blob, function (canvas) {
          canvas.toBlob(
            function (newBlob) {
              window.loadImage(newBlob, function (newCanvas) {
                var canvasData = canvas.getContext('2d')
                    .getImageData(0, 0, canvas.width, canvas.height)
                var newCanvasData = newCanvas.getContext('2d')
                    .getImageData(0, 0, newCanvas.width, newCanvas.height)
                expect(canvasData.width).to.be(newCanvasData.width)
                expect(canvasData.height).to.be(newCanvasData.height)
                done()
              }, {canvas: true})
            }
          )
        }, {canvas: true})
      })
    })
  }(this.expect))