|
1 |
| -import { importSkia, surface } from "../setup"; |
| 1 | +import { surface } from "../setup"; |
2 | 2 | import { checkImage } from "../../../__tests__/setup";
|
3 | 3 | import { BlendMode } from "../../../skia/types";
|
4 | 4 |
|
5 | 5 | describe("Pictures", () => {
|
6 | 6 | it("Should draw a simple picture", async () => {
|
7 |
| - const { Skia } = importSkia(); |
8 |
| - const str = await surface.eval((Sk) => { |
9 |
| - const size = 256; |
10 |
| - const surf = Sk.Surface.MakeOffscreen(size, size)!; |
11 |
| - const canvas = surf.getCanvas(); |
12 |
| - const recorder = Sk.PictureRecorder(); |
13 |
| - const canvas2 = recorder.beginRecording(Sk.XYWHRect(0, 0, size, size)); |
14 |
| - canvas2.drawColor(Sk.Color("cyan")); |
15 |
| - const picture = recorder.finishRecordingAsPicture(); |
16 |
| - canvas.drawPicture(picture); |
17 |
| - return surf.makeImageSnapshot().encodeToBase64(); |
18 |
| - }); |
19 |
| - const image = Skia.Image.MakeImageFromEncoded(Skia.Data.fromBase64(str))!; |
| 7 | + const image = await surface.drawOffscreen( |
| 8 | + (Skia, canvas, { size }) => { |
| 9 | + const recorder = Skia.PictureRecorder(); |
| 10 | + const canvas2 = recorder.beginRecording( |
| 11 | + Skia.XYWHRect(0, 0, size, size) |
| 12 | + ); |
| 13 | + canvas2.drawColor(Skia.Color("cyan")); |
| 14 | + const picture = recorder.finishRecordingAsPicture(); |
| 15 | + canvas.drawPicture(picture); |
| 16 | + }, |
| 17 | + { size: surface.width } |
| 18 | + ); |
20 | 19 | checkImage(image, "snapshots/pictures/simple-picture.png");
|
21 | 20 | });
|
22 | 21 | it("Should draw the hello world example as picture", async () => {
|
23 |
| - const { Skia } = importSkia(); |
24 |
| - const str = await surface.eval( |
25 |
| - (Sk, ctx) => { |
| 22 | + const image = await surface.drawOffscreen( |
| 23 | + (Skia, canvas, ctx) => { |
26 | 24 | const { size } = ctx;
|
27 | 25 | const r = size * 0.33;
|
28 |
| - const surf = Sk.Surface.MakeOffscreen(size, size)!; |
29 |
| - const canvas = surf.getCanvas(); |
30 |
| - const recorder = Sk.PictureRecorder(); |
31 |
| - const canvas2 = recorder.beginRecording(Sk.XYWHRect(0, 0, size, size)); |
32 |
| - const paint = Sk.Paint(); |
| 26 | + const recorder = Skia.PictureRecorder(); |
| 27 | + const canvas2 = recorder.beginRecording( |
| 28 | + Skia.XYWHRect(0, 0, size, size) |
| 29 | + ); |
| 30 | + const paint = Skia.Paint(); |
33 | 31 | paint.setBlendMode(ctx.BlendMode);
|
34 |
| - paint.setColor(Sk.Color("cyan")); |
| 32 | + paint.setColor(Skia.Color("cyan")); |
35 | 33 | canvas2.drawCircle(r, r, r, paint);
|
36 |
| - paint.setColor(Sk.Color("magenta")); |
| 34 | + paint.setColor(Skia.Color("magenta")); |
37 | 35 | canvas2.drawCircle(size - r, r, r, paint);
|
38 |
| - paint.setColor(Sk.Color("yellow")); |
| 36 | + paint.setColor(Skia.Color("yellow")); |
39 | 37 | canvas2.drawCircle(size / 2, size - r, r, paint);
|
40 | 38 | const picture = recorder.finishRecordingAsPicture();
|
41 | 39 | canvas.drawPicture(picture);
|
42 |
| - surf.flush(); |
43 |
| - return surf.makeImageSnapshot().encodeToBase64(); |
44 | 40 | },
|
45 | 41 | { BlendMode: BlendMode.Multiply, size: surface.width }
|
46 | 42 | );
|
47 |
| - const image = Skia.Image.MakeImageFromEncoded(Skia.Data.fromBase64(str))!; |
48 | 43 | checkImage(image, "snapshots/pictures/hello-world.png");
|
49 | 44 | });
|
50 | 45 | });
|
0 commit comments