Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type defintions #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.5",
"description": "GIF generator for Node.js",
"main": "index.js",
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/benjaminadk/gif-encoder-2.git"
Expand All @@ -20,6 +21,7 @@
},
"license": "UNLICENSE",
"devDependencies": {
"@types/node": "^10.17.14",
"canvas": "2.5.0"
}
}
84 changes: 84 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// <reference types="node" />

interface ByteArray {
/**
* Get the data as a Node.js `Buffer`.
*/
getData(): Buffer
}

interface ImageDataLike {
readonly data: Uint8ClampedArray
}

interface CanvasLike {
getImageData(sx: number, sy: number, sw: number, sh: number): ImageDataLike
}

declare class GIFEncoder {
/**
* Create a new GIFEncoder
* @param width the width of the images in pixels.
* @param height the height of images in pixels.
* @param algorithm `neuquant` or `octree`. `neuquant` if undefined.
* @param useOptimizer enables/disables optimizer. `false` if undefined.
* @param totalFrames total number of images. `0` if undefined.
*/
constructor(width: number,
height: number,
algorithm?: "neuquant" | "octree",
useOptimizer?: boolean,
totalFrames?: number)

/**
* Starts the encoder.
*/
start(): void

/**
* Adds a frame to the GIF.
* @param context Node-canvas or DOM canvas context.
*/
addFrame(context: CanvasLike): void

/**
* Sets delay between frames. Can be set once per frame.
* @param delay Number of milliseconds to display frame
*/
setDelay(delay: number): void

/**
* Sets gif framerate. Alternative to `setDelay`.
* @param fps Number of frames per second to display.
*/
setFrameRate(fps: number): void

/**
* Neuquant quality.
* @param quality Neuquant quality. `1` to `30`. `1` is best/slowest.
*/
setQuality(quality: number): void

/**
* Optimizer threshold percentage
* @param threshold Optimizer threshold percentage, `0` to `100`.
*/
setThreshold(threshold: number): void

/**
* Number of loops GIF does
* @param repeat 0 is forever, anything else is literal number of loops
*/
setRepeat(repeat: number): void

/**
* Stops the encoder. Call after all frames are added.
*/
finish(): void

/**
* The raw bytes representing the resulting GIF.
*/
out: ByteArray
}
export = GIFEncoder
18 changes: 18 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GIFEncoder = require("gif-encoder-2")
import * as Canvas from "canvas"

const canvas = Canvas.createCanvas(1, 1)
const context = canvas.getContext("2d")
const gif = new GIFEncoder(1, 1, "neuquant")
gif.start()
gif.setDelay(1)
gif.setFrameRate(30)
gif.setQuality(1)
gif.setRepeat(0)
gif.setThreshold(0)
gif.addFrame(context)
gif.finish()
const image = gif.out.getData()

const gif2 = new GIFEncoder(1, 1, "octree", true, 1)
const gif3 = new GIFEncoder(1, 1)
13 changes: 13 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"baseUrl": ".",
"paths": { "gif-encoder-2": ["."] }
}
}