Skip to content

Commit

Permalink
Fix mjs export
Browse files Browse the repository at this point in the history
  • Loading branch information
gomander committed Nov 25, 2024
1 parent 6bee1a4 commit 751e64c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ or submit a PR, read below.
## Developing

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

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

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

## Try out using sample project
Expand Down
6 changes: 5 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { loadBinding } from '@node-rs/helper'

export default loadBinding(__dirname, 'napi-gif-encoder', '@gomander/napi-gif-encoder')
export const { GIFEncoder } = loadBinding(
import.meta.dirname,
'napi-gif-encoder',
'@gomander/napi-gif-encoder'
)
81 changes: 81 additions & 0 deletions sample/mjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable no-console */
import { createReadStream } from 'node:fs'
import { join } from 'node:path'
import { argv } from 'node:process'
import JsGifEncoder from 'gif-encoder-2'
import { PNG } from 'pngjs'
import { GIFEncoder } from '../../index.mjs'

const imagePaths = [...Array(46).keys()].map((i) => `../BBB${i + 1580}.png`)

async function loadImage(path) {
return new Promise((resolve, reject) => {
createReadStream(path)
.pipe(new PNG())
.on('parsed', function () {
resolve({
buffer: this.data,
width: this.width,
height: this.height,
})
})
.on('error', reject)
})
}

async function loadImages() {
const promises = imagePaths.map(loadImage)
return await Promise.all(promises)
}

async function main() {
const images = await loadImages()
try {
const encoder = new GIFEncoder(images[0].width, images[0].height, join(import.meta.dirname, 'output.gif'))
encoder.setFrameRate(30)
encoder.setSampleFactor(2)
// encoder.setRepeat(0)
for (const image of images) {
encoder.addFrame(image.buffer)
}
console.log('Encoding with Rust GIF encoder')
const start = new Date().getTime()
await encoder.finish()
const end = new Date().getTime()
console.log(`Encode time: ${end - start}ms`)
} catch (error) {
console.error(`Unexpected error: ${JSON.stringify(error)}`)
}
}

class ContextLike {
constructor(buffer) {
this.buffer = buffer
}

getImageData(sx, sy, sw, sh) {
return { data: this.buffer }
}
}

async function mainJs() {
const images = await loadImages()
const gif = new JsGifEncoder(images[0].width, images[1].height, 'neuquant', true, 46)
gif.setFrameRate(30)
gif.setRepeat(1)
console.log('Encoding with JavaScript GIF encoder')
const start = new Date().getTime()
gif.start()
for (const image of images) {
gif.addFrame(new ContextLike(image))
}
gif.finish()
const end = new Date().getTime()
console.log(`Encode time: ${end - start}ms`)
}

if (argv.includes('--js')) {
mainJs()
} else {
main()
}
15 changes: 15 additions & 0 deletions sample/mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "napi-gif-encoder-sample-mjs",
"version": "0.0.7",
"description": "Mjs sample for napi-gif-encoder",
"type": "module",
"module": "index.js",
"repository": "git@github.com:gomander/napi-gif-encoder.git",
"author": "Tyrone Trevorrow <tyrone@sudeium.com>",
"license": "MIT",
"private": false,
"dependencies": {
"gif-encoder-2": "tyrone-sudeium/gif-encoder-2#feature/type_definitions",
"pngjs": "^6.0.0"
}
}
12 changes: 12 additions & 0 deletions sample/mjs/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


gif-encoder-2@tyrone-sudeium/gif-encoder-2#feature/type_definitions:
version "1.0.5"
resolved "https://codeload.github.com/tyrone-sudeium/gif-encoder-2/tar.gz/a7aa7760d2af8a7c4dfbce788f4767233f8fe99e"

pngjs@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-6.0.0.tgz#ca9e5d2aa48db0228a52c419c3308e87720da821"
integrity sha512-TRzzuFRRmEoSW/p1KVAmiOgPco2Irlah+bGFCeNfJXxxYGwSw7YwAOAcd7X28K/m5bjBWKsC29KyoMfHbypayg==
2 changes: 1 addition & 1 deletion sample/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "napi-gif-encoder-sample",
"version": "0.0.4",
"version": "0.0.7",
"description": "Sample for napi-gif-encoder",
"main": "index.js",
"repository": "git@github.com:gomander/napi-gif-encoder.git",
Expand Down

0 comments on commit 751e64c

Please sign in to comment.