|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -> A WebP animation package for NodeJS using Rust |
| 5 | +> A fully typed WebP animation package for NodeJS using Rust |
| 6 | +
|
| 7 | +## ⚠️ This project is a work-in-progress! |
| 8 | + |
| 9 | +This package is a N-API wrapper for the [`webp-animation`](https://crates.io/crates/webp-animation) Rust crate, but does not yet support all its features. |
| 10 | +By virtue of being built with NAPI-RS, this package is fully typed, and compatible with ESM and CJS. |
6 | 11 |
|
7 | 12 | ## Install |
8 | 13 |
|
9 | 14 | ```sh |
10 | 15 | npm install napi-webp-animation |
11 | 16 | ``` |
12 | 17 |
|
| 18 | +## Usage |
| 19 | + |
| 20 | +```js |
| 21 | +import { WebpEncoder } from 'napi-webp-animation' |
| 22 | + |
| 23 | +// Create an encoder instance with width and height |
| 24 | +const encoder = new WebpEncoder(100, 100) |
| 25 | + |
| 26 | +// Set the frame rate of the animated WebP - default 30 |
| 27 | +encoder.setFrameRate(24) |
| 28 | + |
| 29 | +// Add your frames as buffers |
| 30 | +for (const frameBuffer of frameBuffers) { |
| 31 | + encoder.addFrame(frameBuffer) |
| 32 | +} |
| 33 | + |
| 34 | +// Encode the animated WebP and write it to file! |
| 35 | +const data = await encoder.writeToFile('output.webp') |
| 36 | + |
| 37 | +// Or get the data as a buffer without writing it to file |
| 38 | +const data = await encoder.getBuffer() |
| 39 | + |
| 40 | +// Output options can be set on `writeToFile`, `getBuffer` |
| 41 | +await encoder.writeToFile('output.webp', { |
| 42 | + lossless: false, // default true |
| 43 | + quality: 75, // 0 (fast) - 100 (slower) - default 1 |
| 44 | + method: 3, // 0 (fast) - 6 (slower) - default 4 |
| 45 | + loopCount: 1 // 0 = infinite, n > 0 = play n times - default 0 |
| 46 | +}) |
| 47 | + |
| 48 | +// Synchronous methods are also available, and also support options |
| 49 | +encoder.writeToFileSync('output.webp') |
| 50 | +encoder.getBufferSync() |
| 51 | + |
| 52 | +// Clear the encoder's frames, set the dimensions, or set the options |
| 53 | +encoder.clearFrames() |
| 54 | +encoder.setDimensions(480, 270) |
| 55 | +``` |
| 56 | + |
13 | 57 | ## Support matrix |
14 | 58 |
|
15 | 59 | | | node18 | node20 | node22 | |
16 | 60 | | ---------------- | ------ | ------ | ------ | |
17 | | -| Windows x64 | ✓ | ✓ | ✓ | |
18 | 61 | | Linux x64 gnu | ✓ | ✓ | ✓ | |
19 | 62 | | macOS x64 | ✓ | ✓ | ✓ | |
20 | 63 | | macOS aarch64 | ✓ | ✓ | ✓ | |
| 64 | +| Windows x64 | ✓ | ✓ | ✓ | |
| 65 | + |
| 66 | +## Contributing |
21 | 67 |
|
| 68 | +Issues and pull requests are welcome! |
| 69 | + |
| 70 | +Particularly, I would love help setting up the CI to build for more platforms. |
22 | 71 |
|
23 | 72 | ## Developing |
24 | 73 |
|
25 | | -- Install latest [Rust](https://rustup.rs/). If on Windows, use WSL for an easier time. |
26 | | -- Install `NodeJS@18+`. LTS versions suggested. |
27 | | -- Install `pnpm` and dependencies with `pnpm i`. |
| 74 | +- If on Windows, recommend using [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) |
| 75 | +- Install latest [Rust](https://rustup.rs/) |
| 76 | +- Install latest LTS [Node](https://nodejs.org/en/download) |
| 77 | +- Install pnpm with `npm i -g pnpm` |
| 78 | +- Install dependencies with `pnpm i` |
| 79 | + |
| 80 | +Make your changes, and run the tests with `pnpm test`. |
28 | 81 |
|
29 | 82 | You can then compile the rust code with `pnpm build`. |
30 | 83 |
|
|
0 commit comments