aboutsummaryrefslogtreecommitdiffstats
path: root/library/cropperjs/test/specs/methods/getCroppedCanvas.spec.js
blob: 16441773f75a9a0b961753f71c144d10cb2e8739 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
describe('getCroppedCanvas (method)', () => {
  it('should get a canvas with the whole image drew when it is not cropped', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      autoCrop: false,

      ready() {
        const canvasData = cropper.getCanvasData();
        const canvas = cropper.getCroppedCanvas();

        expect(canvas).to.be.an.instanceof(HTMLCanvasElement);
        expect(canvas.width).to.equal(canvasData.naturalWidth);
        expect(canvas.height).to.equal(canvasData.naturalHeight);
        done();
      },
    });
  });

  it('should get a canvas with the cropped area of the image drew', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const data = cropper.getData();
        const canvas = cropper.getCroppedCanvas();

        expect(canvas).to.be.an.instanceof(HTMLCanvasElement);
        expect(canvas.width).to.equal(data.width);
        expect(canvas.height).to.equal(data.height);
        done();
      },
    });
  });

  it('should match the given width', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const width = 160;
        const canvas = cropper.getCroppedCanvas({
          width,
        });

        expect(canvas.width).to.equal(width);
        done();
      },
    });
  });

  it('should match the given height', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const height = 90;
        const canvas = cropper.getCroppedCanvas({
          height,
        });

        expect(canvas.height).to.equal(height);
        done();
      },
    });
  });

  it('should be contained when both width and height are given', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const width = 160;
        const height = 90;
        const canvas = cropper.getCroppedCanvas({
          width,
          height,
        });

        expect(canvas.width).to.be.most(width);
        expect(canvas.height).to.be.most(height);
        done();
      },
    });
  });

  it('should not be greater than the maximum width', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const maxWidth = 160;
        const canvas = cropper.getCroppedCanvas({
          maxWidth,
        });

        expect(canvas.width).to.be.most(maxWidth);
        done();
      },
    });
  });

  it('should not be greater than the maximum height', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const maxHeight = 90;
        const canvas = cropper.getCroppedCanvas({
          maxHeight,
        });

        expect(canvas.height).to.be.most(maxHeight);
        done();
      },
    });
  });

  it('should not be greater than both the maximum width and maximum height', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const maxWidth = 160;
        const maxHeight = 90;
        const canvas = cropper.getCroppedCanvas({
          maxWidth,
          maxHeight,
        });

        expect(canvas.width).to.be.most(maxWidth);
        expect(canvas.height).to.be.most(maxHeight);
        done();
      },
    });
  });

  it('should not be less than the minimum width', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const minWidth = 1600;
        const canvas = cropper.getCroppedCanvas({
          minWidth,
        });

        expect(canvas.width).to.be.least(minWidth);
        done();
      },
    });
  });

  it('should not be less than the minimum height', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const minHeight = 900;
        const canvas = cropper.getCroppedCanvas({
          minHeight,
        });

        expect(canvas.height).to.be.least(minHeight);
        done();
      },
    });
  });

  it('should not be less than both the minimum width and minimum height', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const minWidth = 1600;
        const minHeight = 900;
        const canvas = cropper.getCroppedCanvas({
          minWidth,
          minHeight,
        });

        expect(canvas.width).to.be.least(minWidth);
        expect(canvas.height).to.be.least(minHeight);
        done();
      },
    });
  });

  it('should match the given fill color', (done) => {
    const image = window.createImage();
    const cropper = new Cropper(image, {
      ready() {
        const canvas = cropper.zoomTo(0.1).getCroppedCanvas({
          fillColor: '#010101',
        });
        const pixelData = canvas.getContext('2d').getImageData(0, 0, 1, 1).data;

        expect(pixelData[0]).to.equal(1, 'red is 1');
        expect(pixelData[1]).to.equal(1, 'green is 1');
        expect(pixelData[2]).to.equal(1, 'blue is 1');
        expect(pixelData[3]).to.equal(255, 'color is opaque');
        done();
      },
    });
  });
});