generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathloadimage.spec.ts
55 lines (44 loc) · 1.75 KB
/
loadimage.spec.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
import { join } from 'path'
import test from 'ava'
import fs from 'fs'
import { createCanvas, Image, loadImage } from '../index'
import { snapshotImage } from './image-snapshot'
test('should load file src', async (t) => {
const img = await loadImage(join(__dirname, '../example/simple.png'))
t.is(img instanceof Image, true)
})
test('should load file stream', async (t) => {
const img = await loadImage(fs.createReadStream(join(__dirname, '../example/simple.png')))
t.is(img instanceof Image, true)
})
test('should load image with alt', async (t) => {
const img = await loadImage(join(__dirname, '../example/simple.png'), {
alt: 'demo-image',
})
t.is(img.alt, 'demo-image')
})
test('should load remote url', async (t) => {
const img = await loadImage(
'https://raw.githubusercontent.com/Brooooooklyn/canvas/462fce53afeaee6d6b4ae5d1b407c17e2359ff7e/example/anime-girl.png',
)
t.is(img instanceof Image, true)
})
test('should load data uri', async (t) => {
const img = await loadImage(
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII',
)
t.is(img instanceof Image, true)
})
test('should draw img', async (t) => {
const img = await loadImage(
'https://raw.githubusercontent.com/Brooooooklyn/canvas/462fce53afeaee6d6b4ae5d1b407c17e2359ff7e/example/anime-girl.png',
)
// create a canvas of the same size as the image
const canvas = createCanvas(img.width, img.height)
const ctx = canvas.getContext('2d')
// fill the canvas with the image
ctx.fillStyle = '#23eff0'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(img, 250, 250)
await snapshotImage(t, { canvas }, 'jpeg', process.arch === 'x64' ? 0.05 : 0.3)
})