Skip to content

Commit

Permalink
#110: parameter support for getImageData (#111)
Browse files Browse the repository at this point in the history
* fix: support parameters for CanvasRenderingContext2D#getImageData

* #110: throw on zero size, allow negative size
  • Loading branch information
danielrentz authored May 18, 2023
1 parent 002c8db commit d73ec61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ canvas.toDataURL.mockReturnValueOnce(
- [@lekha](https://github.com/lekha)
- [@yonatankra](https://github.com/yonatankra)
- [@LitoMore](https://github.com/LitoMore)
- [@danielrentz](https://github.com/danielrentz)

## License

Expand Down
33 changes: 27 additions & 6 deletions __tests__/classes/CanvasRenderingContext2D.getImageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,39 @@ describe('getImageData', () => {
});

it('should be callable', () => {
ctx.getImageData();
ctx.getImageData(0, 0, 10, 20);
expect(ctx.getImageData).toBeCalled();
});

it('should throw if less than 4 parameters are given', () => {
expect(() => ctx.getImageData()).toThrow(TypeError);
expect(() => ctx.getImageData(0)).toThrow(TypeError);
expect(() => ctx.getImageData(0, 0)).toThrow(TypeError);
expect(() => ctx.getImageData(0, 0, 0)).toThrow(TypeError);
});

it('should return a image data from getImageData', () => {
expect(ctx.getImageData()).toBeInstanceOf(ImageData);
expect(ctx.getImageData(0, 0, 10, 20)).toBeInstanceOf(ImageData);
});

it('should return a image data from getImageData of proper size', () => {
const data = ctx.getImageData();
expect(data.width).toBe(400);
expect(data.height).toBe(300);
expect(data.data.length).toBe(400 * 300 * 4);
const data = ctx.getImageData(0, 0, 10, 20);
expect(data.width).toBe(10);
expect(data.height).toBe(20);
expect(data.data.length).toBe(10 * 20 * 4);
});

it('should return image data from getImageData if size is negative', () => {
const data = ctx.getImageData(0, 0, -10, -20);
expect(data.width).toBe(10);
expect(data.height).toBe(20);
expect(data.data.length).toBe(10 * 20 * 4);
});

it('should throw if width or height are zero', () => {
expect(() => ctx.getImageData(0, 0, 0, 1)).toThrow(DOMException);
expect(() => ctx.getImageData(0, 0, 0, 1)).toThrow('source width is 0');
expect(() => ctx.getImageData(0, 0, 1, 0)).toThrow('source height is 0');
expect(() => ctx.getImageData(0, 0, 0, 0)).toThrow('source width is 0');
});
});
5 changes: 0 additions & 5 deletions __tests__/vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
describe('vis', () => {
it('vis can pass', () => {
// const div = document.createElement('canvas');

// const line = new Line(div, {
// data: [
// { x: 'A', y: 10 },
Expand All @@ -17,13 +16,9 @@ describe('vis', () => {
// xField: 'x',
// yField: 'y',
// });

// line.render();

// expect(line.container.querySelector('canvas')).not.toBe(null);

// line.destroy();

// expect(line.container.querySelector('canvas')).toBe(null);
});
});
21 changes: 19 additions & 2 deletions src/classes/CanvasRenderingContext2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,8 +1053,25 @@ export default class CanvasRenderingContext2D {
return this._fontStack[this._stackIndex];
}

getImageData() {
return new ImageData(this._canvas.width, this._canvas.height);
getImageData(x, y, w, h) {
if (arguments.length < 4)
throw new TypeError(
"Failed to execute 'getImageData' on '" +
this.constructor.name +
"': 4 arguments required, but only " +
arguments.length +
' present.'
);
if (w == 0 || h == 0)
throw new DOMException(
"Failed to execute 'getImageData' on '" +
this.constructor.name +
"': The source " +
(w == 0 ? 'width' : 'height') +
' is 0.',
'IndexSizeError'
);
return new ImageData(Math.abs(w), Math.abs(h));
}

getLineDash() {
Expand Down

0 comments on commit d73ec61

Please sign in to comment.