Skip to content

Commit 751e64c

Browse files
committed
Fix mjs export
1 parent 6bee1a4 commit 751e64c

6 files changed

Lines changed: 116 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ or submit a PR, read below.
4040
## Developing
4141

4242
- Install latest `Rust`. Suggest using [rustup](https://rustup.rs/). If on
43-
Windows, use WSL for an easier time.
43+
Windows, use WSL for an easier time.
4444
- Install `NodeJS@18+`. LTS versions suggested.
4545
- Install `yarn@1.x`.
4646
- Install dependencies with `yarn`.
4747

4848
You can then compile the rust code with `yarn build`.
4949

5050
After running `yarn build`, you will see a
51-
`napi-gif-encoder.[win32|linux].node` file in the project root.
51+
`napi-gif-encoder.<PLATFORM>.node` file in the project root.
5252
This is the native addon built from [lib.rs](./src/lib.rs).
5353

5454
## Try out using sample project

index.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { loadBinding } from '@node-rs/helper'
22

3-
export default loadBinding(__dirname, 'napi-gif-encoder', '@gomander/napi-gif-encoder')
3+
export const { GIFEncoder } = loadBinding(
4+
import.meta.dirname,
5+
'napi-gif-encoder',
6+
'@gomander/napi-gif-encoder'
7+
)

sample/mjs/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* eslint-disable no-console */
2+
import { createReadStream } from 'node:fs'
3+
import { join } from 'node:path'
4+
import { argv } from 'node:process'
5+
import JsGifEncoder from 'gif-encoder-2'
6+
import { PNG } from 'pngjs'
7+
import { GIFEncoder } from '../../index.mjs'
8+
9+
const imagePaths = [...Array(46).keys()].map((i) => `../BBB${i + 1580}.png`)
10+
11+
async function loadImage(path) {
12+
return new Promise((resolve, reject) => {
13+
createReadStream(path)
14+
.pipe(new PNG())
15+
.on('parsed', function () {
16+
resolve({
17+
buffer: this.data,
18+
width: this.width,
19+
height: this.height,
20+
})
21+
})
22+
.on('error', reject)
23+
})
24+
}
25+
26+
async function loadImages() {
27+
const promises = imagePaths.map(loadImage)
28+
return await Promise.all(promises)
29+
}
30+
31+
async function main() {
32+
const images = await loadImages()
33+
try {
34+
const encoder = new GIFEncoder(images[0].width, images[0].height, join(import.meta.dirname, 'output.gif'))
35+
encoder.setFrameRate(30)
36+
encoder.setSampleFactor(2)
37+
// encoder.setRepeat(0)
38+
for (const image of images) {
39+
encoder.addFrame(image.buffer)
40+
}
41+
console.log('Encoding with Rust GIF encoder')
42+
const start = new Date().getTime()
43+
await encoder.finish()
44+
const end = new Date().getTime()
45+
console.log(`Encode time: ${end - start}ms`)
46+
} catch (error) {
47+
console.error(`Unexpected error: ${JSON.stringify(error)}`)
48+
}
49+
}
50+
51+
class ContextLike {
52+
constructor(buffer) {
53+
this.buffer = buffer
54+
}
55+
56+
getImageData(sx, sy, sw, sh) {
57+
return { data: this.buffer }
58+
}
59+
}
60+
61+
async function mainJs() {
62+
const images = await loadImages()
63+
const gif = new JsGifEncoder(images[0].width, images[1].height, 'neuquant', true, 46)
64+
gif.setFrameRate(30)
65+
gif.setRepeat(1)
66+
console.log('Encoding with JavaScript GIF encoder')
67+
const start = new Date().getTime()
68+
gif.start()
69+
for (const image of images) {
70+
gif.addFrame(new ContextLike(image))
71+
}
72+
gif.finish()
73+
const end = new Date().getTime()
74+
console.log(`Encode time: ${end - start}ms`)
75+
}
76+
77+
if (argv.includes('--js')) {
78+
mainJs()
79+
} else {
80+
main()
81+
}

sample/mjs/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "napi-gif-encoder-sample-mjs",
3+
"version": "0.0.7",
4+
"description": "Mjs sample for napi-gif-encoder",
5+
"type": "module",
6+
"module": "index.js",
7+
"repository": "git@github.com:gomander/napi-gif-encoder.git",
8+
"author": "Tyrone Trevorrow <tyrone@sudeium.com>",
9+
"license": "MIT",
10+
"private": false,
11+
"dependencies": {
12+
"gif-encoder-2": "tyrone-sudeium/gif-encoder-2#feature/type_definitions",
13+
"pngjs": "^6.0.0"
14+
}
15+
}

sample/mjs/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
gif-encoder-2@tyrone-sudeium/gif-encoder-2#feature/type_definitions:
6+
version "1.0.5"
7+
resolved "https://codeload.github.com/tyrone-sudeium/gif-encoder-2/tar.gz/a7aa7760d2af8a7c4dfbce788f4767233f8fe99e"
8+
9+
pngjs@^6.0.0:
10+
version "6.0.0"
11+
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-6.0.0.tgz#ca9e5d2aa48db0228a52c419c3308e87720da821"
12+
integrity sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg==

sample/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "napi-gif-encoder-sample",
3-
"version": "0.0.4",
3+
"version": "0.0.7",
44
"description": "Sample for napi-gif-encoder",
55
"main": "index.js",
66
"repository": "git@github.com:gomander/napi-gif-encoder.git",

0 commit comments

Comments
 (0)