Skip to content

Commit

Permalink
Add createImageData() implementation (#16)
Browse files Browse the repository at this point in the history
* Add createImageData implementation #15

* Add createImageData implementation #15
  • Loading branch information
robinelvin authored and hustcc committed Jan 24, 2019
1 parent b734559 commit c02f8c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ describe('canvas', () => {
expect(canvas.toDataURL).toHaveBeenLastCalledWith('image/jpeg', 1.0);
});

test('canvas.createImageData()', () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const imageData = ctx.createImageData(100, 50);
expect(imageData.width).toBe(100);
expect(imageData.height).toBe(50);
expect(imageData.data).toBeDefined();
expect(imageData.data).toHaveLength(20000);

const imageData2 = ctx.createImageData({width: 120, height: 100});
expect(imageData2.data).toHaveLength(48000);
});

test('ctx.functions', () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
Expand Down
12 changes: 11 additions & 1 deletion src/classes/context2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@ const context2d = {
height: 14,
}),
putImageData: () => {},
createImageData: () => [],
createImageData: (w = 1, h = 1) => {
if ('object' === typeof w) {
h = w.height;
w = w.width;
}
return {
width: w,
height: h,
data: new Array((w * h * 4))
}
},
setTransform: () => {},
resetTransform: () => {},
drawImage: () => {},
Expand Down

0 comments on commit c02f8c8

Please sign in to comment.