Skip to content

Commit a032c41

Browse files
committed
Added readFromCanvas to ImageMagick.
1 parent 60c264e commit a032c41

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/image-magick.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ export class ImageMagick {
8080
}
8181
});
8282
}
83+
84+
static readFromCanvas(canvas: HTMLCanvasElement, func: (image: MagickImage) => void): void;
85+
static readFromCanvas(canvas: HTMLCanvasElement, func: (image: MagickImage) => Promise<void>): Promise<void>;
86+
static readFromCanvas(canvas: HTMLCanvasElement, func: (image: MagickImage) => void | Promise<void>): void | Promise<void> {
87+
return MagickImage._use(image => {
88+
image.readFromCanvas(canvas);
89+
return func(image);
90+
});
91+
}
8392
}
8493

8594
/** @internal */
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright Dirk Lemstra https://github.com/dlemstra/Magick.WASM */
2+
3+
import { ImageMagick } from '../../src/image-magick';
4+
import { JSDOM } from 'jsdom';
5+
import { MagickColors } from '../../src/magick-colors';
6+
import { colorAssert } from '../color-assert';
7+
8+
beforeEach(() => {
9+
ImageMagick._api = (global as any).native;
10+
});
11+
12+
describe('ImageMagick#readFromCanvas', () => {
13+
it('should read the image data from the canvas', () => {
14+
const window = new JSDOM().window;
15+
16+
const canvas = window.document.createElement('canvas');
17+
canvas.width = 1;
18+
canvas.height = 2;
19+
20+
window.HTMLCanvasElement.prototype.getContext = <any> jest.fn(function(contextId: string) {
21+
expect(contextId).toBe('2d');
22+
return {
23+
getImageData: function(x: number, y: number, width: number, height: number) {
24+
expect(x).toBe(0);
25+
expect(y).toBe(0);
26+
expect(width).toBe(canvas.width);
27+
expect(height).toBe(canvas.height);
28+
return {
29+
data: new Uint8ClampedArray([
30+
255, 0, 255, 255,
31+
255, 0, 0, 255
32+
])
33+
}
34+
}
35+
};
36+
});
37+
38+
ImageMagick.readFromCanvas(canvas, image => {
39+
expect(image.width).toBe(1);
40+
expect(image.height).toBe(2);
41+
42+
colorAssert(image, 0, 0, MagickColors.Magenta);
43+
colorAssert(image, 0, 1, MagickColors.Red);
44+
});
45+
});
46+
47+
it('should read the image data from the canvas async', async () => {
48+
const window = new JSDOM().window;
49+
50+
const canvas = window.document.createElement('canvas');
51+
canvas.width = 1;
52+
canvas.height = 2;
53+
54+
window.HTMLCanvasElement.prototype.getContext = <any> jest.fn(function(contextId: string) {
55+
expect(contextId).toBe('2d');
56+
return {
57+
getImageData: function(x: number, y: number, width: number, height: number) {
58+
expect(x).toBe(0);
59+
expect(y).toBe(0);
60+
expect(width).toBe(canvas.width);
61+
expect(height).toBe(canvas.height);
62+
return {
63+
data: new Uint8ClampedArray([
64+
255, 0, 255, 255,
65+
255, 0, 0, 255
66+
])
67+
}
68+
}
69+
};
70+
});
71+
72+
await ImageMagick.readFromCanvas(canvas, image => {
73+
expect(image.width).toBe(1);
74+
expect(image.height).toBe(2);
75+
76+
colorAssert(image, 0, 0, MagickColors.Magenta);
77+
colorAssert(image, 0, 1, MagickColors.Red);
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)