Skip to content

Commit aa04eb7

Browse files
authored
Merge pull request #1330 from zbjornson/zb/1303
Add typescript definitions
2 parents 2c24269 + fabfac9 commit aa04eb7

File tree

13 files changed

+418
-122
lines changed

13 files changed

+418
-122
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ addons:
1919
env:
2020
- CXX=g++-4.9
2121
sudo: false
22+
script:
23+
- node_version=$(node -v); if [ ${node_version:1:2} = 10 ]; then npm run dtslint; fi
24+
- npm test

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
1111
### Added
1212
* (Actually) added `resolution` option for `canvas.toBuffer("image/png")` and
1313
`canvas.createPNGStream()`. This was documented since 2.0.0 but not working.
14+
* Add typescript definitions.
1415
### Fixed
1516
* PDF metadata (added in 2.3.0) wasn't being set with `canvas.createPDFStream()`
1617
* Fix custom "inspect" function deprecation warnings (#1326)

Readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ This is a standard API, but several non-standard calls are supported. The full l
382382
```js
383383
dataUrl = canvas.toDataURL() // defaults to PNG
384384
dataUrl = canvas.toDataURL('image/png')
385+
dataUrl = canvas.toDataURL('image/jpeg')
386+
dataUrl = canvas.toDataURL('image/jpeg', quality) // quality from 0 to 1
385387
canvas.toDataURL((err, png) => { }) // defaults to PNG
386388
canvas.toDataURL('image/png', (err, png) => { })
387389
canvas.toDataURL('image/jpeg', (err, jpeg) => { }) // sync JPEG is not supported
@@ -423,9 +425,9 @@ In `glyph` mode, `ctx.strokeText()` and `ctx.fillText()` behave the same (aside
423425
424426
This property is tracked as part of the canvas state in save/restore.
425427
426-
### CanvasRenderingContext2D#globalCompositeOperator = 'saturate'
428+
### CanvasRenderingContext2D#globalCompositeOperation = 'saturate'
427429
428-
In addition to all of the standard global composite operators defined by the Canvas specification, the ['saturate'](https://www.cairographics.org/operators/#saturate) operator is also available.
430+
In addition to all of the standard global composite operations defined by the Canvas specification, the ['saturate'](https://www.cairographics.org/operators/#saturate) operation is also available.
429431
430432
### CanvasRenderingContext2D#antialias
431433

lib/canvas.js

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
* MIT Licensed
77
*/
88

9-
/**
10-
* Module dependencies.
11-
*/
12-
139
const bindings = require('./bindings')
1410
const Canvas = module.exports = bindings.Canvas
1511
const Context2d = require('./context2d')
@@ -24,15 +20,6 @@ Canvas.prototype[util.inspect.custom || 'inspect'] = function () {
2420
return `[Canvas ${this.width}x${this.height}]`
2521
}
2622

27-
/**
28-
* Get a context object.
29-
*
30-
* @param {String} contextType must be "2d"
31-
* @param {Object {alpha: boolean, pixelFormat: PIXEL_FORMAT} } contextAttributes Optional
32-
* @return {Context2d}
33-
* @api public
34-
*/
35-
3623
Canvas.prototype.getContext = function (contextType, contextAttributes) {
3724
if ('2d' == contextType) {
3825
var ctx = this._context2d || (this._context2d = new Context2d(this, contextAttributes));
@@ -42,77 +29,21 @@ Canvas.prototype.getContext = function (contextType, contextAttributes) {
4229
}
4330
};
4431

45-
/**
46-
* Create a `PNGStream` for `this` canvas.
47-
*
48-
* @param {Object} [options]
49-
* @param {number} [options.compressionLevel] Number from 0 to 9 corresponding
50-
* to the ZLIB compression level. Defaults to 6.
51-
* @param {number} [options.filters] Any bitwise combination of
52-
* `PNG_FILTER_NONE`, `PNG_FITLER_SUB`, `PNG_FILTER_UP`, `PNG_FILTER_AVG`,
53-
* `PNG_FILTER_PATETH`; or one of `PNG_ALL_FILTERS` or `PNG_NO_FILTERS`. These
54-
* specify which filters *may* be used by libpng. During encoding, libpng will
55-
* select the best filter from this list of allowed filters.
56-
* @param {Uint8ClampedArray} [options.palette] Provide for indexed PNG
57-
* encoding. Entries should be R-G-B-A values.
58-
* @param {number} [options.backgroundIndex] Optional index of background color
59-
* for indexed PNGs. Defaults to 0.
60-
* @return {PNGStream}
61-
* @public
62-
*/
6332
Canvas.prototype.pngStream =
6433
Canvas.prototype.createPNGStream = function(options){
6534
return new PNGStream(this, options);
6635
};
6736

68-
/**
69-
* Create a `PDFStream` for `this` canvas.
70-
*
71-
* @param {object} [options]
72-
* @param {string} [options.title]
73-
* @param {string} [options.author]
74-
* @param {string} [options.subject]
75-
* @param {string} [options.keywords]
76-
* @param {string} [options.creator]
77-
* @param {Date} [options.creationDate]
78-
* @param {Date} [options.modDate]
79-
* @return {PDFStream}
80-
* @public
81-
*/
8237
Canvas.prototype.pdfStream =
8338
Canvas.prototype.createPDFStream = function(options){
8439
return new PDFStream(this, options);
8540
};
8641

87-
/**
88-
* Create a `JPEGStream` for `this` canvas.
89-
*
90-
* @param {Object} [options]
91-
* @param {number} [options.quality] Number from 0 to 1. Defaults to 0.75.
92-
* @param {boolean|1|2} [options.chromaSubsampling] Enables 2x2 chroma
93-
* subsampling. `true` is equivalent to `2`, `false` is equivalent to `1`.
94-
* @param {boolean} [options.progressive] Enables progressive encoding. Defautls
95-
* to false.
96-
* @return {JPEGStream}
97-
* @public
98-
*/
9942
Canvas.prototype.jpegStream =
10043
Canvas.prototype.createJPEGStream = function(options){
10144
return new JPEGStream(this, options);
10245
};
10346

104-
/**
105-
* Returns a data URI. Pass a function for async operation (non-standard).
106-
*
107-
* @param {"image/png"|"image/jpeg"} [type="image/png"] Type.
108-
* @param {Object|Number} [encoderOptions] A number between 0 and 1 indicating
109-
* image quality if the requested type is image/jpeg (standard), or an options
110-
* object for image encoding (see documentation for Canvas#toBuffer)
111-
* (non-standard).
112-
* @param {Function} [fn] Callback for asynchronous operation (non-standard).
113-
* @return {String} data URL if synchronous (callback omitted)
114-
* @api public
115-
*/
11647
Canvas.prototype.toDataURL = function(a1, a2, a3){
11748
// valid arg patterns (args -> [type, opts, fn]):
11849
// [] -> ['image/png', null, null]

lib/jpegstream.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,9 @@
66
* MIT Licensed
77
*/
88

9-
/**
10-
* Module dependencies.
11-
*/
12-
139
var Readable = require('stream').Readable;
1410
var util = require('util');
1511

16-
/**
17-
* Initialize a `JPEGStream` with the given `canvas`.
18-
*
19-
* "data" events are emitted with `Buffer` chunks, once complete the
20-
* "end" event is emitted. The following example will stream to a file
21-
* named "./my.jpeg".
22-
*
23-
* var out = fs.createWriteStream(__dirname + '/my.jpeg')
24-
* , stream = canvas.createJPEGStream();
25-
*
26-
* stream.pipe(out);
27-
*
28-
* @param {Canvas} canvas
29-
* @private
30-
*/
31-
3212
var JPEGStream = module.exports = function JPEGStream(canvas, options) {
3313
if (!(this instanceof JPEGStream)) {
3414
throw new TypeError("Class constructors cannot be invoked without 'new'");

lib/pdfstream.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,9 @@
44
* Canvas - PDFStream
55
*/
66

7-
/**
8-
* Module dependencies.
9-
*/
10-
117
var Readable = require('stream').Readable;
128
var util = require('util');
139

14-
/**
15-
* Initialize a `PDFStream` with the given `canvas`.
16-
*
17-
* "data" events are emitted with `Buffer` chunks, once complete the
18-
* "end" event is emitted. The following example will stream to a file
19-
* named "./my.pdf".
20-
*
21-
* var out = fs.createWriteStream(__dirname + '/my.pdf')
22-
* , stream = canvas.createPDFStream();
23-
*
24-
* stream.pipe(out);
25-
*
26-
* @param {Canvas} canvas
27-
* @api public
28-
*/
29-
3010
var PDFStream = module.exports = function PDFStream(canvas, options) {
3111
if (!(this instanceof PDFStream)) {
3212
throw new TypeError("Class constructors cannot be invoked without 'new'");

lib/pngstream.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,9 @@
66
* MIT Licensed
77
*/
88

9-
/**
10-
* Module dependencies.
11-
*/
12-
139
var Readable = require('stream').Readable;
1410
var util = require('util');
1511

16-
/**
17-
* @param {Canvas} canvas
18-
* @param {Object} options
19-
* @private
20-
*/
2112
var PNGStream = module.exports = function PNGStream(canvas, options) {
2213
if (!(this instanceof PNGStream)) {
2314
throw new TypeError("Class constructors cannot be invoked without 'new'");

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"test": "mocha test/*.test.js",
3030
"pretest-server": "node-gyp build",
3131
"test-server": "node test/server.js",
32-
"install": "node-pre-gyp install --fallback-to-build"
32+
"install": "node-pre-gyp install --fallback-to-build",
33+
"dtslint": "dtslint types"
3334
},
3435
"binary": {
3536
"module_name": "canvas",
@@ -42,14 +43,18 @@
4243
"binding.gyp",
4344
"lib/",
4445
"src/",
45-
"util/"
46+
"util/",
47+
"types/index.d.ts"
4648
],
49+
"types": "types",
4750
"dependencies": {
4851
"nan": "^2.12.1",
4952
"node-pre-gyp": "^0.11.0"
5053
},
5154
"devDependencies": {
55+
"@types/node": "^10.12.18",
5256
"assert-rejects": "^1.0.0",
57+
"dtslint": "^0.5.3",
5358
"express": "^4.16.3",
5459
"mocha": "^5.2.0",
5560
"pixelmatch": "^4.0.2",

types/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Notes:
2+
3+
* `"unified-signatures": false` because of https://github.com/Microsoft/dtslint/issues/183

0 commit comments

Comments
 (0)