generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-test.js
39 lines (29 loc) · 1.06 KB
/
simple-test.js
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
const { join } = require('path')
const { cwd } = require('process')
const { loadImage, createCanvas } = require('@napi-rs/canvas')
const { rgbaToThumbHash } = require('./index.js')
async function loadImageAndConvertToRgba() {
const maxSize = 100
const imgPath = join(cwd(), './benchmark/fixture/img.png')
const image = await loadImage(imgPath)
const width = image.width
const height = image.height
const scale = Math.min(maxSize / width, maxSize / height)
const resizedWidth = Math.round(width * scale)
const resizedHeight = Math.round(height * scale)
const canvas = createCanvas(resizedWidth, resizedHeight)
const ctx = canvas.getContext('2d')
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
const rgba = new Uint8Array(imageData.data.buffer)
return {
height: imageData.height,
width: imageData.width,
rgba,
}
}
const run = async () => {
const { height, width, rgba } = await loadImageAndConvertToRgba()
rgbaToThumbHash(width, height, rgba)
}
run()