@napi-rs/canvas 0.1.91 → 0.1.93
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/node-canvas.d.ts +216 -0
- package/node-canvas.js +379 -0
- package/package.json +16 -13
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# `skr canvas`
|
|
2
2
|
|
|
3
3
|
[](https://github.com/Brooooooklyn/canvas/actions/workflows/CI.yaml)
|
|
4
|
-

|
|
5
5
|
[](https://packagephobia.com/result?p=@napi-rs/canvas)
|
|
6
6
|
[](https://npmcharts.com/compare/@napi-rs/canvas?minimal=true)
|
|
7
7
|
|
package/node-canvas.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* node-canvas compatibility layer for @napi-rs/canvas.
|
|
3
|
+
*
|
|
4
|
+
* Provides a drop-in replacement API matching the `canvas` (node-canvas) npm package.
|
|
5
|
+
* Quality values for JPEG/WebP use the node-canvas 0-1 scale and are automatically
|
|
6
|
+
* converted to @napi-rs/canvas's 0-100 scale internally.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // Drop-in replacement: change this import
|
|
11
|
+
* // import { createCanvas, registerFont, loadImage } from 'canvas'
|
|
12
|
+
* // to this:
|
|
13
|
+
* import { createCanvas, registerFont, loadImage } from '@napi-rs/canvas/node-canvas'
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Readable } from 'node:stream'
|
|
18
|
+
|
|
19
|
+
// Re-export types from @napi-rs/canvas that are API-compatible
|
|
20
|
+
import {
|
|
21
|
+
Image as NapiImage,
|
|
22
|
+
ImageData as NapiImageData,
|
|
23
|
+
Path2D as NapiPath2D,
|
|
24
|
+
DOMPoint as NapiDOMPoint,
|
|
25
|
+
DOMMatrix as NapiDOMMatrix,
|
|
26
|
+
DOMRect as NapiDOMRect,
|
|
27
|
+
SKRSContext2D,
|
|
28
|
+
Canvas as NapiCanvas,
|
|
29
|
+
SvgCanvas,
|
|
30
|
+
SvgExportFlag,
|
|
31
|
+
IGlobalFonts,
|
|
32
|
+
PDFDocument,
|
|
33
|
+
} from './index'
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Config interfaces (node-canvas conventions)
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
export interface PngConfig {
|
|
40
|
+
/** ZLIB compression level (0-9). Accepted for compatibility; Skia uses its own encoder. */
|
|
41
|
+
compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
|
|
42
|
+
/** PNG filter flags. Accepted for compatibility; Skia uses its own encoder. */
|
|
43
|
+
filters?: number
|
|
44
|
+
/** Palette for indexed PNGs. Not supported by Skia backend. */
|
|
45
|
+
palette?: Uint8ClampedArray
|
|
46
|
+
/** Background color index for indexed PNGs. Not supported by Skia backend. */
|
|
47
|
+
backgroundIndex?: number
|
|
48
|
+
/** Pixels per inch. Accepted for compatibility. */
|
|
49
|
+
resolution?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface JpegConfig {
|
|
53
|
+
/** Quality between 0 and 1. Defaults to 0.75. Converted to 0-100 for Skia. */
|
|
54
|
+
quality?: number
|
|
55
|
+
/** Progressive encoding. Accepted for compatibility; not supported by Skia backend. */
|
|
56
|
+
progressive?: boolean
|
|
57
|
+
/** 2x2 chroma subsampling. Accepted for compatibility; not supported by Skia backend. */
|
|
58
|
+
chromaSubsampling?: boolean
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Canvas class (with node-canvas compatible methods)
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
export interface Canvas extends Omit<NapiCanvas, 'toBuffer' | 'toDataURL'> {
|
|
66
|
+
/**
|
|
67
|
+
* Encode the canvas as a PNG and return a Node.js Readable stream.
|
|
68
|
+
* @param config PNG encoding options (accepted for compatibility)
|
|
69
|
+
*/
|
|
70
|
+
createPNGStream(config?: PngConfig): PNGStream
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Encode the canvas as a JPEG and return a Node.js Readable stream.
|
|
74
|
+
* @param config JPEG encoding options. Quality is 0-1 (default 0.75).
|
|
75
|
+
*/
|
|
76
|
+
createJPEGStream(config?: JpegConfig): JPEGStream
|
|
77
|
+
|
|
78
|
+
// --- toBuffer overloads (node-canvas conventions) ---
|
|
79
|
+
|
|
80
|
+
/** Encode as PNG (default). */
|
|
81
|
+
toBuffer(): Buffer
|
|
82
|
+
toBuffer(mimeType: 'image/png', config?: PngConfig): Buffer
|
|
83
|
+
/** Encode as JPEG. Quality in config is 0-1 (default 0.75). */
|
|
84
|
+
toBuffer(mimeType: 'image/jpeg', config?: JpegConfig): Buffer
|
|
85
|
+
/** Get raw unencoded pixel data. */
|
|
86
|
+
toBuffer(mimeType: 'raw'): Buffer
|
|
87
|
+
|
|
88
|
+
/** Async: encode as PNG (default) via callback. */
|
|
89
|
+
toBuffer(cb: (err: Error | null, result: Buffer) => void): void
|
|
90
|
+
toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/png', config?: PngConfig): void
|
|
91
|
+
/** Async: encode as JPEG via callback. Quality in config is 0-1 (default 0.75). */
|
|
92
|
+
toBuffer(cb: (err: Error | null, result: Buffer) => void, mimeType: 'image/jpeg', config?: JpegConfig): void
|
|
93
|
+
|
|
94
|
+
// --- toDataURL overloads (node-canvas conventions) ---
|
|
95
|
+
|
|
96
|
+
/** Encode as PNG data URL (default). */
|
|
97
|
+
toDataURL(): string
|
|
98
|
+
toDataURL(mimeType: 'image/png'): string
|
|
99
|
+
/** Encode as JPEG data URL. Quality is 0-1. */
|
|
100
|
+
toDataURL(mimeType: 'image/jpeg', quality?: number): string
|
|
101
|
+
/** Async: encode as data URL via callback. */
|
|
102
|
+
toDataURL(cb: (err: Error | null, result: string) => void): void
|
|
103
|
+
toDataURL(mimeType: 'image/png', cb: (err: Error | null, result: string) => void): void
|
|
104
|
+
toDataURL(mimeType: 'image/jpeg', cb: (err: Error | null, result: string) => void): void
|
|
105
|
+
toDataURL(mimeType: 'image/jpeg', quality: number, cb: (err: Error | null, result: string) => void): void
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// Stream classes
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
/** Readable stream that emits PNG-encoded data. */
|
|
113
|
+
export class PNGStream extends Readable {}
|
|
114
|
+
|
|
115
|
+
/** Readable stream that emits JPEG-encoded data. */
|
|
116
|
+
export class JPEGStream extends Readable {}
|
|
117
|
+
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
// Factory functions
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create a new canvas with node-canvas compatible API.
|
|
124
|
+
* Canvas instances created through this function have `createPNGStream()`,
|
|
125
|
+
* `createJPEGStream()`, and node-canvas compatible `toBuffer()` overloads.
|
|
126
|
+
*
|
|
127
|
+
* When `type` is `'svg'`, returns a `SvgCanvas` which produces vector output
|
|
128
|
+
* via `getContent()` instead of raster encoding methods.
|
|
129
|
+
*
|
|
130
|
+
* @param width Canvas width in pixels
|
|
131
|
+
* @param height Canvas height in pixels
|
|
132
|
+
* @param type Canvas type: 'image' (default) or 'svg'
|
|
133
|
+
*/
|
|
134
|
+
export function createCanvas(width: number, height: number, type: 'svg'): SvgCanvas
|
|
135
|
+
export function createCanvas(width: number, height: number, type?: 'image'): Canvas
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Create an ImageData instance.
|
|
139
|
+
* @param data Pixel data array
|
|
140
|
+
* @param width Width in pixels
|
|
141
|
+
* @param height Height in pixels (calculated from data length if omitted)
|
|
142
|
+
*/
|
|
143
|
+
export function createImageData(data: Uint8ClampedArray, width: number, height?: number): NapiImageData
|
|
144
|
+
export function createImageData(width: number, height: number): NapiImageData
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Load an image from a file path, URL, Buffer, or other source.
|
|
148
|
+
* Returns a Promise that resolves to an Image instance.
|
|
149
|
+
*/
|
|
150
|
+
export function loadImage(
|
|
151
|
+
source: string | URL | Buffer | ArrayBufferLike | Uint8Array | NapiImage | import('stream').Readable,
|
|
152
|
+
options?: { alt?: string; maxRedirects?: number; requestOptions?: import('http').RequestOptions },
|
|
153
|
+
): Promise<NapiImage>
|
|
154
|
+
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
// Font registration (node-canvas convention)
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Register a font file for use in canvas text rendering.
|
|
161
|
+
* Compatible with node-canvas's `registerFont()`.
|
|
162
|
+
*
|
|
163
|
+
* Note: @napi-rs/canvas auto-detects weight and style from font file metadata
|
|
164
|
+
* (matching browser behavior). The `weight` and `style` properties in fontFace
|
|
165
|
+
* are accepted for API compatibility.
|
|
166
|
+
*
|
|
167
|
+
* @param path Absolute path to the font file (.ttf, .otf, etc.)
|
|
168
|
+
* @param fontFace Font face properties matching CSS @font-face descriptors
|
|
169
|
+
*/
|
|
170
|
+
export function registerFont(path: string, fontFace: { family: string; weight?: string; style?: string }): void
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Deregister all previously registered fonts.
|
|
174
|
+
* Compatible with node-canvas's `deregisterAllFonts()`.
|
|
175
|
+
*/
|
|
176
|
+
export function deregisterAllFonts(): void
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// Re-exported classes & values
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
// Canvas is exported both as a type (the compat interface above) and as a
|
|
183
|
+
// value (constructor that returns compat Canvas instances at runtime).
|
|
184
|
+
export declare const Canvas: {
|
|
185
|
+
new (width: number, height: number): Canvas
|
|
186
|
+
new (width: number, height: number, flag: SvgExportFlag): SvgCanvas
|
|
187
|
+
prototype: NapiCanvas
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export { NapiImage as Image }
|
|
191
|
+
export { NapiImageData as ImageData }
|
|
192
|
+
export { NapiPath2D as Path2D }
|
|
193
|
+
export { NapiDOMPoint as DOMPoint }
|
|
194
|
+
export { NapiDOMMatrix as DOMMatrix }
|
|
195
|
+
export { NapiDOMRect as DOMRect }
|
|
196
|
+
|
|
197
|
+
// SKRSContext2D is an interface in index.d.ts but a class value at runtime.
|
|
198
|
+
// Declare as const to export the value for instanceof checks and re-export the type.
|
|
199
|
+
export type CanvasRenderingContext2D = SKRSContext2D
|
|
200
|
+
export declare const CanvasRenderingContext2D: { prototype: SKRSContext2D }
|
|
201
|
+
/** Legacy alias for CanvasRenderingContext2D. */
|
|
202
|
+
export type Context2d = SKRSContext2D
|
|
203
|
+
export declare const Context2d: { prototype: SKRSContext2D }
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// @napi-rs/canvas extras (not part of node-canvas, but useful)
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
|
|
209
|
+
export { IGlobalFonts }
|
|
210
|
+
export declare const GlobalFonts: IGlobalFonts & {
|
|
211
|
+
/** Reload system fonts. Available at runtime but not declared in IGlobalFonts. */
|
|
212
|
+
loadSystemFonts(): number
|
|
213
|
+
}
|
|
214
|
+
export { PDFDocument }
|
|
215
|
+
export { NapiCanvas as CanvasElement }
|
|
216
|
+
export { SvgCanvas as SVGCanvas }
|
package/node-canvas.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { Readable } = require('node:stream')
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
createCanvas: _createCanvas,
|
|
7
|
+
Canvas,
|
|
8
|
+
CanvasElement,
|
|
9
|
+
SVGCanvas,
|
|
10
|
+
GlobalFonts,
|
|
11
|
+
Image,
|
|
12
|
+
ImageData,
|
|
13
|
+
Path2D,
|
|
14
|
+
DOMPoint,
|
|
15
|
+
DOMMatrix,
|
|
16
|
+
DOMRect,
|
|
17
|
+
loadImage,
|
|
18
|
+
PDFDocument,
|
|
19
|
+
SvgExportFlag,
|
|
20
|
+
} = require('./index.js')
|
|
21
|
+
|
|
22
|
+
// CanvasRenderingContext2D is not re-exported by index.js, grab from native bindings
|
|
23
|
+
const { CanvasRenderingContext2D } = require('./js-binding')
|
|
24
|
+
|
|
25
|
+
// node-canvas defaults JPEG quality to 0.75 across all paths.
|
|
26
|
+
// @napi-rs/canvas native default is 0.92 (matching Blink/Chrome).
|
|
27
|
+
const NODE_CANVAS_DEFAULT_QUALITY = 0.75
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Stream classes (node-canvas returns Node.js Readable streams)
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
class PNGStream extends Readable {
|
|
34
|
+
constructor(canvas, options) {
|
|
35
|
+
super()
|
|
36
|
+
this._canvas = canvas
|
|
37
|
+
this._options = options || {}
|
|
38
|
+
this._done = false
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_read() {
|
|
42
|
+
if (this._done) return
|
|
43
|
+
this._done = true
|
|
44
|
+
this._canvas
|
|
45
|
+
.encode('png')
|
|
46
|
+
.then((buf) => {
|
|
47
|
+
this.push(buf)
|
|
48
|
+
this.push(null)
|
|
49
|
+
})
|
|
50
|
+
.catch((err) => {
|
|
51
|
+
this.destroy(err)
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class JPEGStream extends Readable {
|
|
57
|
+
constructor(canvas, options) {
|
|
58
|
+
super()
|
|
59
|
+
this._canvas = canvas
|
|
60
|
+
const opts = options || {}
|
|
61
|
+
// node-canvas quality: 0-1 (default 0.75), encode() expects 0-100
|
|
62
|
+
this._quality = Math.round((opts.quality != null ? opts.quality : NODE_CANVAS_DEFAULT_QUALITY) * 100)
|
|
63
|
+
this._done = false
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
_read() {
|
|
67
|
+
if (this._done) return
|
|
68
|
+
this._done = true
|
|
69
|
+
this._canvas
|
|
70
|
+
.encode('jpeg', this._quality)
|
|
71
|
+
.then((buf) => {
|
|
72
|
+
this.push(buf)
|
|
73
|
+
this.push(null)
|
|
74
|
+
})
|
|
75
|
+
.catch((err) => {
|
|
76
|
+
this.destroy(err)
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Quality normalization helpers
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
const MIME_FORMAT_MAP = {
|
|
86
|
+
'image/png': 'png',
|
|
87
|
+
'image/jpeg': 'jpeg',
|
|
88
|
+
'image/webp': 'webp',
|
|
89
|
+
'image/avif': 'avif',
|
|
90
|
+
'image/gif': 'gif',
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Extract quality from a node-canvas config object or number.
|
|
95
|
+
* Returns the raw 0-1 quality value, defaulting to NODE_CANVAS_DEFAULT_QUALITY
|
|
96
|
+
* for JPEG/WebP when not specified.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} mime
|
|
99
|
+
* @param {number|object|undefined} configOrQuality
|
|
100
|
+
* @returns {number|undefined} 0-1 scale for JPEG/WebP, undefined for other mimes
|
|
101
|
+
*/
|
|
102
|
+
function _extractQuality(mime, configOrQuality) {
|
|
103
|
+
if (mime !== 'image/jpeg' && mime !== 'image/webp') return undefined
|
|
104
|
+
if (configOrQuality == null) return NODE_CANVAS_DEFAULT_QUALITY
|
|
105
|
+
if (typeof configOrQuality === 'number') return configOrQuality
|
|
106
|
+
if (typeof configOrQuality === 'object' && configOrQuality.quality != null) {
|
|
107
|
+
return configOrQuality.quality
|
|
108
|
+
}
|
|
109
|
+
return NODE_CANVAS_DEFAULT_QUALITY
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// Compat methods added to each canvas instance created via this module
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
// Keep references to the original prototype methods
|
|
117
|
+
const _origToBuffer = CanvasElement.prototype.toBuffer
|
|
118
|
+
const _origToDataURL = CanvasElement.prototype.toDataURL
|
|
119
|
+
|
|
120
|
+
function _compatCreatePNGStream(options) {
|
|
121
|
+
return new PNGStream(this, options)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function _compatCreateJPEGStream(options) {
|
|
125
|
+
return new JPEGStream(this, options)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* node-canvas compatible toBuffer:
|
|
130
|
+
* - toBuffer() → PNG (sync)
|
|
131
|
+
* - toBuffer('image/png', config?) → PNG (sync)
|
|
132
|
+
* - toBuffer('image/jpeg', config?) → JPEG (sync, quality 0-1)
|
|
133
|
+
* - toBuffer('raw') → raw pixel data (sync)
|
|
134
|
+
* - toBuffer(callback) → PNG (async)
|
|
135
|
+
* - toBuffer(callback, mime, config?) → specified format (async)
|
|
136
|
+
*/
|
|
137
|
+
function _compatToBuffer(mimeOrCallback, configOrQuality) {
|
|
138
|
+
// Callback form: toBuffer(callback) or toBuffer(callback, mime, config)
|
|
139
|
+
if (typeof mimeOrCallback === 'function') {
|
|
140
|
+
const callback = mimeOrCallback
|
|
141
|
+
const mime = typeof configOrQuality === 'string' ? configOrQuality : 'image/png'
|
|
142
|
+
const config = arguments[2]
|
|
143
|
+
|
|
144
|
+
if (mime === 'raw') {
|
|
145
|
+
let buf
|
|
146
|
+
try {
|
|
147
|
+
buf = this.data()
|
|
148
|
+
} catch (err) {
|
|
149
|
+
callback(err)
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
callback(null, buf)
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const format = MIME_FORMAT_MAP[mime]
|
|
157
|
+
if (!format) {
|
|
158
|
+
callback(new TypeError(`Unsupported MIME type "${mime}". Supported: ${Object.keys(MIME_FORMAT_MAP).join(', ')}`))
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
const q = _extractQuality(mime, config)
|
|
162
|
+
this.encode(format, q != null ? Math.round(q * 100) : undefined).then(
|
|
163
|
+
(buf) => callback(null, buf),
|
|
164
|
+
(err) => callback(err),
|
|
165
|
+
)
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Sync: no args → PNG
|
|
170
|
+
if (mimeOrCallback === undefined) {
|
|
171
|
+
return _origToBuffer.call(this, 'image/png')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Sync: raw pixel data
|
|
175
|
+
if (mimeOrCallback === 'raw') {
|
|
176
|
+
return this.data()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Sync: extract quality (0-1) and scale to 0-100 for native toBuffer
|
|
180
|
+
const q = _extractQuality(mimeOrCallback, configOrQuality)
|
|
181
|
+
return _origToBuffer.call(this, mimeOrCallback, q != null ? Math.round(q * 100) : undefined)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* node-canvas compatible toDataURL:
|
|
186
|
+
* - toDataURL() → PNG data URL (sync)
|
|
187
|
+
* - toDataURL('image/jpeg', quality) → JPEG data URL, quality 0-1 (sync)
|
|
188
|
+
* - toDataURL(callback) → PNG data URL (async)
|
|
189
|
+
* - toDataURL(mime, callback) → data URL (async)
|
|
190
|
+
* - toDataURL(mime, quality, callback) → data URL with quality (async)
|
|
191
|
+
*/
|
|
192
|
+
function _compatToDataURL(mimeOrCallback, qualityOrCallback) {
|
|
193
|
+
// toDataURL(callback)
|
|
194
|
+
if (typeof mimeOrCallback === 'function') {
|
|
195
|
+
this.toDataURLAsync('image/png').then(
|
|
196
|
+
(url) => mimeOrCallback(null, url),
|
|
197
|
+
(err) => mimeOrCallback(err),
|
|
198
|
+
)
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
// toDataURL(mime, callback)
|
|
202
|
+
if (typeof qualityOrCallback === 'function') {
|
|
203
|
+
this.toDataURLAsync(mimeOrCallback, _extractQuality(mimeOrCallback, undefined)).then(
|
|
204
|
+
(url) => qualityOrCallback(null, url),
|
|
205
|
+
(err) => qualityOrCallback(err),
|
|
206
|
+
)
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
// toDataURL(mime, quality, callback)
|
|
210
|
+
const cb = arguments[2]
|
|
211
|
+
if (typeof cb === 'function') {
|
|
212
|
+
// Native toDataURLAsync expects 0-1 (f64) — pass quality through as-is
|
|
213
|
+
const quality = _extractQuality(mimeOrCallback, qualityOrCallback)
|
|
214
|
+
this.toDataURLAsync(mimeOrCallback, quality).then(
|
|
215
|
+
(url) => cb(null, url),
|
|
216
|
+
(err) => cb(err),
|
|
217
|
+
)
|
|
218
|
+
return
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Sync form: native toDataURL expects 0-1 (f64) — pass quality through as-is
|
|
222
|
+
const quality = _extractQuality(mimeOrCallback, qualityOrCallback)
|
|
223
|
+
return _origToDataURL.call(this, mimeOrCallback, quality)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Attach node-canvas compatible methods to a CanvasElement instance.
|
|
228
|
+
*/
|
|
229
|
+
function _addCompatMethods(canvas) {
|
|
230
|
+
canvas.createPNGStream = _compatCreatePNGStream
|
|
231
|
+
canvas.createJPEGStream = _compatCreateJPEGStream
|
|
232
|
+
canvas.toBuffer = _compatToBuffer
|
|
233
|
+
canvas.toDataURL = _compatToDataURL
|
|
234
|
+
return canvas
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ---------------------------------------------------------------------------
|
|
238
|
+
// Public API: registerFont / deregisterAllFonts
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
|
|
241
|
+
// Track FontKeys from registerFont() so deregisterAllFonts() can remove
|
|
242
|
+
// only user-registered fonts (matching node-canvas behavior which leaves
|
|
243
|
+
// system fonts untouched).
|
|
244
|
+
const _registeredFontKeys = []
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Register a font file with the specified font face properties.
|
|
248
|
+
* Compatible with node-canvas's registerFont(path, { family, weight?, style? }).
|
|
249
|
+
*
|
|
250
|
+
* Note: @napi-rs/canvas auto-detects weight and style from font file metadata
|
|
251
|
+
* (matching browser behavior). The weight and style properties in fontFace are
|
|
252
|
+
* accepted for API compatibility but the actual values are read from the font.
|
|
253
|
+
*
|
|
254
|
+
* @param {string} path Absolute path to the font file (.ttf, .otf, etc.)
|
|
255
|
+
* @param {{ family: string, weight?: string, style?: string }} fontFace
|
|
256
|
+
*/
|
|
257
|
+
function registerFont(path, fontFace) {
|
|
258
|
+
if (!fontFace || typeof fontFace.family !== 'string') {
|
|
259
|
+
throw new TypeError('registerFont requires a fontFace with a "family" property')
|
|
260
|
+
}
|
|
261
|
+
const key = GlobalFonts.registerFromPath(path, fontFace.family)
|
|
262
|
+
if (!key) {
|
|
263
|
+
throw new Error(`Failed to register font from "${path}" with family "${fontFace.family}"`)
|
|
264
|
+
}
|
|
265
|
+
_registeredFontKeys.push(key)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Deregister all fonts previously registered via registerFont().
|
|
270
|
+
* Compatible with node-canvas's deregisterAllFonts() which only removes
|
|
271
|
+
* user-registered fonts and leaves system fonts untouched.
|
|
272
|
+
*/
|
|
273
|
+
function deregisterAllFonts() {
|
|
274
|
+
if (_registeredFontKeys.length > 0) {
|
|
275
|
+
GlobalFonts.removeBatch(_registeredFontKeys)
|
|
276
|
+
_registeredFontKeys.length = 0
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ---------------------------------------------------------------------------
|
|
281
|
+
// Public API: createCanvas / createImageData
|
|
282
|
+
// ---------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Create a new canvas instance with node-canvas compatible API.
|
|
286
|
+
*
|
|
287
|
+
* @param {number} width
|
|
288
|
+
* @param {number} height
|
|
289
|
+
* @param {'image'|'svg'} [type='image']
|
|
290
|
+
* @returns {Canvas}
|
|
291
|
+
*/
|
|
292
|
+
function createCanvas(width, height, type) {
|
|
293
|
+
if (type === 'svg') {
|
|
294
|
+
// SvgExportFlag enum requires a valid variant; NoPrettyXML (0x02) is the
|
|
295
|
+
// least impactful on rendering behavior (only affects XML whitespace).
|
|
296
|
+
return _createCanvas(width, height, SvgExportFlag.NoPrettyXML)
|
|
297
|
+
}
|
|
298
|
+
if (type === 'pdf') {
|
|
299
|
+
throw new Error(
|
|
300
|
+
'createCanvas with type "pdf" is not supported. Use the PDFDocument class from @napi-rs/canvas directly.',
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
if (type != null && type !== 'image') {
|
|
304
|
+
throw new TypeError(`createCanvas: unknown type "${type}". Supported types: "image", "svg".`)
|
|
305
|
+
}
|
|
306
|
+
return _addCompatMethods(_createCanvas(width, height))
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Create an ImageData instance.
|
|
311
|
+
* Compatible with node-canvas's createImageData().
|
|
312
|
+
*
|
|
313
|
+
* @param {Uint8ClampedArray|number} dataOrWidth
|
|
314
|
+
* @param {number} widthOrHeight
|
|
315
|
+
* @param {number} [height]
|
|
316
|
+
* @returns {ImageData}
|
|
317
|
+
*/
|
|
318
|
+
function createImageData(dataOrWidth, widthOrHeight, height) {
|
|
319
|
+
if (typeof dataOrWidth === 'number') {
|
|
320
|
+
return new ImageData(dataOrWidth, widthOrHeight)
|
|
321
|
+
}
|
|
322
|
+
if (height != null) {
|
|
323
|
+
return new ImageData(dataOrWidth, widthOrHeight, height)
|
|
324
|
+
}
|
|
325
|
+
return new ImageData(dataOrWidth, widthOrHeight)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ---------------------------------------------------------------------------
|
|
329
|
+
// Canvas constructor wrapper
|
|
330
|
+
// ---------------------------------------------------------------------------
|
|
331
|
+
|
|
332
|
+
// In node-canvas, `new Canvas(w, h)` and `createCanvas(w, h)` produce
|
|
333
|
+
// equivalent canvases. Wrap the native Canvas constructor so that
|
|
334
|
+
// `new Canvas(w, h)` also gets compat methods, without polluting the
|
|
335
|
+
// prototype (which would affect @napi-rs/canvas users who don't use
|
|
336
|
+
// this compat layer).
|
|
337
|
+
const CompatCanvas = new Proxy(Canvas, {
|
|
338
|
+
construct(target, args) {
|
|
339
|
+
const canvas = new target(...args)
|
|
340
|
+
if (canvas instanceof CanvasElement) {
|
|
341
|
+
return _addCompatMethods(canvas)
|
|
342
|
+
}
|
|
343
|
+
return canvas
|
|
344
|
+
},
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
// ---------------------------------------------------------------------------
|
|
348
|
+
// Exports (matches node-canvas export shape)
|
|
349
|
+
// ---------------------------------------------------------------------------
|
|
350
|
+
|
|
351
|
+
module.exports = {
|
|
352
|
+
// Factory functions
|
|
353
|
+
Canvas: CompatCanvas,
|
|
354
|
+
createCanvas,
|
|
355
|
+
createImageData,
|
|
356
|
+
loadImage,
|
|
357
|
+
registerFont,
|
|
358
|
+
deregisterAllFonts,
|
|
359
|
+
|
|
360
|
+
// Classes
|
|
361
|
+
Image,
|
|
362
|
+
ImageData,
|
|
363
|
+
CanvasRenderingContext2D,
|
|
364
|
+
Context2d: CanvasRenderingContext2D,
|
|
365
|
+
PNGStream,
|
|
366
|
+
JPEGStream,
|
|
367
|
+
Path2D,
|
|
368
|
+
|
|
369
|
+
// Geometry
|
|
370
|
+
DOMPoint,
|
|
371
|
+
DOMMatrix,
|
|
372
|
+
DOMRect,
|
|
373
|
+
|
|
374
|
+
// @napi-rs/canvas extras (available but not part of node-canvas)
|
|
375
|
+
GlobalFonts,
|
|
376
|
+
PDFDocument,
|
|
377
|
+
CanvasElement,
|
|
378
|
+
SVGCanvas,
|
|
379
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napi-rs/canvas",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.93",
|
|
4
4
|
"description": "Canvas for Node.js with skia backend",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
"index.js",
|
|
30
30
|
"geometry.js",
|
|
31
31
|
"js-binding.js",
|
|
32
|
-
"load-image.js"
|
|
32
|
+
"load-image.js",
|
|
33
|
+
"node-canvas.js",
|
|
34
|
+
"node-canvas.d.ts"
|
|
33
35
|
],
|
|
34
36
|
"napi": {
|
|
35
37
|
"binaryName": "skia",
|
|
@@ -99,6 +101,7 @@
|
|
|
99
101
|
"lodash": "^4.17.21",
|
|
100
102
|
"npm-run-all2": "^8.0.4",
|
|
101
103
|
"oxlint": "^1.34.0",
|
|
104
|
+
"oxlint-tsgolint": "^0.14.0",
|
|
102
105
|
"pinst": "^3.0.0",
|
|
103
106
|
"png.js": "^0.2.1",
|
|
104
107
|
"prettier": "^3.7.4",
|
|
@@ -150,16 +153,16 @@
|
|
|
150
153
|
},
|
|
151
154
|
"packageManager": "yarn@4.12.0",
|
|
152
155
|
"optionalDependencies": {
|
|
153
|
-
"@napi-rs/canvas-linux-x64-gnu": "0.1.
|
|
154
|
-
"@napi-rs/canvas-darwin-x64": "0.1.
|
|
155
|
-
"@napi-rs/canvas-win32-x64-msvc": "0.1.
|
|
156
|
-
"@napi-rs/canvas-win32-arm64-msvc": "0.1.
|
|
157
|
-
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.
|
|
158
|
-
"@napi-rs/canvas-linux-x64-musl": "0.1.
|
|
159
|
-
"@napi-rs/canvas-linux-arm64-gnu": "0.1.
|
|
160
|
-
"@napi-rs/canvas-linux-arm64-musl": "0.1.
|
|
161
|
-
"@napi-rs/canvas-darwin-arm64": "0.1.
|
|
162
|
-
"@napi-rs/canvas-android-arm64": "0.1.
|
|
163
|
-
"@napi-rs/canvas-linux-riscv64-gnu": "0.1.
|
|
156
|
+
"@napi-rs/canvas-linux-x64-gnu": "0.1.93",
|
|
157
|
+
"@napi-rs/canvas-darwin-x64": "0.1.93",
|
|
158
|
+
"@napi-rs/canvas-win32-x64-msvc": "0.1.93",
|
|
159
|
+
"@napi-rs/canvas-win32-arm64-msvc": "0.1.93",
|
|
160
|
+
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.93",
|
|
161
|
+
"@napi-rs/canvas-linux-x64-musl": "0.1.93",
|
|
162
|
+
"@napi-rs/canvas-linux-arm64-gnu": "0.1.93",
|
|
163
|
+
"@napi-rs/canvas-linux-arm64-musl": "0.1.93",
|
|
164
|
+
"@napi-rs/canvas-darwin-arm64": "0.1.93",
|
|
165
|
+
"@napi-rs/canvas-android-arm64": "0.1.93",
|
|
166
|
+
"@napi-rs/canvas-linux-riscv64-gnu": "0.1.93"
|
|
164
167
|
}
|
|
165
168
|
}
|