generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
house.ts
61 lines (47 loc) · 1.17 KB
/
house.ts
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
import b from 'benny'
import { createCanvas, Canvas } from 'canvas'
// @ts-expect-error
import { Canvas as SkiaCanvas } from 'skia-canvas'
import { createCanvas as skiaCreateCanvas } from '../index'
function drawHouse(factory: (width: number, height: number) => Canvas) {
const canvas = factory(1024, 768)
const ctx = canvas.getContext('2d')!
ctx.lineWidth = 10
ctx.strokeStyle = '#03a9f4'
ctx.fillStyle = '#03a9f4'
// Wall
ctx.strokeRect(75, 140, 150, 110)
// Door
ctx.fillRect(130, 190, 40, 60)
// Roof
ctx.beginPath()
ctx.moveTo(50, 140)
ctx.lineTo(150, 60)
ctx.lineTo(250, 140)
ctx.closePath()
ctx.stroke()
if (canvas instanceof SkiaCanvas) {
canvas.toBufferSync('image/png')
} else {
// @ts-expect-error
canvas.async = false
canvas.toBuffer('image/png')
}
}
export function house() {
return b.suite(
'Draw house',
b.add('skia-canvas', () => {
drawHouse((w, h) => new SkiaCanvas(w, h))
}),
b.add('node-canvas', () => {
drawHouse(createCanvas)
}),
b.add('@napi-rs/skia', () => {
// @ts-expect-error
drawHouse(skiaCreateCanvas)
}),
b.cycle(),
b.complete(),
)
}