Skip to content

Instantly share code, notes, and snippets.

@tyrone-sudeium
Last active June 8, 2018 06:39
Show Gist options
  • Save tyrone-sudeium/81ba8119c7b5cb20a9815315c9e89f49 to your computer and use it in GitHub Desktop.
Save tyrone-sudeium/81ba8119c7b5cb20a9815315c9e89f49 to your computer and use it in GitHub Desktop.
A first attempt at a Typescript Type Definition for https://github.com/Automattic/node-canvas
/// <reference types="node" />
declare module "canvas" {
import { Readable } from "stream"
type CanvasType = "pdf" | "svg"
interface NodeCanvasRenderingContext2D extends CanvasRenderingContext2D {
patternQuality: "fast" | "good" | "best" | "nearest" | "bilinear"
textDrawingMode: "path" | "glyph"
filter: "fast" | "good" | "best" | "nearest" | "bilinear"
antialias: "default" | "none" | "gray" | "subpixel"
drawImage(image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap | Image, dstX: number, dstY: number): void
drawImage(image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap | Image, dstX: number, dstY: number, dstW: number, dstH: number): void
drawImage(image: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap | Image, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number): void
}
interface Canvas {
getContext(contextId: "2d", contextAttributes?: Canvas2DContextAttributes): NodeCanvasRenderingContext2D | null;
toBuffer(type: "raw" | undefined): Buffer
toBuffer(type: "png", compressionLevel?: 0|1|2|3|4|5|6|7|8|9, filter?: number): Buffer
toBuffer(type: "raw" | undefined, callback: (err: any, buf: Buffer) => void): void
toBuffer(type: "png", callback: (err: any, buf: Buffer) => void): void
toBuffer(type: "png", compressionLevel: 0|1|2|3|4|5|6|7|8|9, callback: (err: any, buf: Buffer) => void): void
toBuffer(type: "png", compressionLevel: 0|1|2|3|4|5|6|7|8|9, filter: number, callback: (err: any, buf: Buffer) => void): void
pngStream(options?: PNGStreamOptions): PNGStream
jpegStream(options?: JPEGStreamOptions): JPEGStream
pdfStream(): PDFStream
readonly PNG_NO_FILTERS: number
readonly PNG_FILTER_NONE: number
readonly PNG_FILTER_SUB: number
readonly PNG_FILTER_UP: number
readonly PNG_FILTER_AVG: number
readonly PNG_FILTER_PAETH: number
readonly PNG_ALL_FILTERS: number
}
interface PDFCanvas extends Canvas {
getContext(contextId: "2d", contextAttributes?: Canvas2DContextAttributes): PDFCanvasRenderingContext2D | null;
}
interface PDFCanvasRenderingContext2D extends NodeCanvasRenderingContext2D {
addPage(): void
}
export class Image {
constructor(src?: Buffer | string)
src: Buffer | string
dataMode: number
readonly MODE_IMAGE: number
readonly MODE_MIME: number
}
interface PNGStreamOptions {
palette?: Uint8ClampedArray
backgroundIndex?: number
}
export class PNGStream extends Readable {
constructor(canvas: Canvas, options?: PNGStreamOptions)
}
interface JPEGStreamOptions {
bufsize?: number
quality?: number
progressive?: boolean
disableChromaSampling?: boolean
}
export class JPEGStream extends Readable {
constructor(canvas: Canvas, options?: JPEGStreamOptions)
}
export class PDFStream extends Readable {
constructor(canvas: Canvas)
}
export function createCanvas(width: number, height: number): Canvas
export function createCanvas(width: number, height: number, type: "pdf"): PDFCanvas
export function registerFont(path: string, options: { family: string }): void
type DataURLMIMEType = "image/png" | "image/jpeg"
type DataURLJPEGOptions = {
quality: number
}
export function toDataURL(type: "image/jpeg", quality: undefined | number, callback: (url: string) => void): void
export function toDataURL(type: "image/jpeg", callback: (url: string) => void): void
export function toDataURL(type: "image/jpeg", options: DataURLJPEGOptions, callback: (url: string) => void): void
export function toDataURL(type: "image/png", quality?: number): string
export function toDataURL(type: "image/png", quality: undefined | number, callback: (url: string) => void): void
export function toDataURL(callback: (url: string) => void): void
export function toDataURL(type: undefined, callback: (url: string) => void): void
export function loadImage(src: string): Promise<Image>
export const version: string
export const cairoVersion: string
export const jpegVersion: string
export const gifVersion: string | undefined
export const freetypeVersion: string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment